0

I want to ask about SQL Server 2005 about date grouping with other sum field.

| tanggal             | nama                    | qty  |
|---------------------|-------------------------|------|
| 2017-07-11 06:25:00 | APEL                    | 4.00 |
| 2017-07-11 06:25:00 | SUSU KENTAL MANIS PUTIH | 4.00 |
| 2017-07-11 06:29:00 | APEL                    | 4.00 |
| 2017-07-11 06:29:00 | SUSU KENTAL MANIS PUTIH | 4.00 |
| 2017-07-10 06:29:00 | APEL                    | 2.00 |
| 2017-07-10 06:29:00 | SUSU KENTAL MANIS PUTIH | 2.00 |
| 2017-07-11 00:00:00 | APEL                    | 1.00 |
| 2017-07-11 00:00:00 | SUSU KENTAL MANIS PUTIH | 1.00 |

and I need result like this

| Tanggal    | Barang                  | Jumlah |
|------------|-------------------------|--------|
| 2017-07-10 | APEL                    | 2.00   |
| 2017-07-10 | SUSU KENTAL MANIS PUTIH | 2.00   |
| 2017-07-11 | APEL                    | 9.00   |
| 2017-07-11 | SUSU KENTAL MANIS PUTIH | 9.00   |

Here's the code

select j.tanggal [Tanggal],p.nama [Barang],sum(p.qty) [Jumlah] 
from proses p
left join jual j on j.kode=p.kode 
group by j.tanggal,p.kodeItem,p.nama,p.unit,p.kode

2 Answers 2

0

Try this:

select j.tanggal [Tanggal],p.nama [Barang],sum(p.qty) [Jumlah] 

from proses p
left join jual j on j.kode=p.kode 

order by date

If this does not work, you may try using union instead of join.

0
0

use CAST to change the first column from a datetime to a date, there were some unnecessary columns in group by which I have removed too:

select 
cast(j.tanggal as date) [Tanggal],
p.nama [Barang],
sum(p.qty) [Jumlah] 
from proses p
left join jual j on j.kode=p.kode 
group by cast(j.tanggal as date),p.nama
1
  • It still not work to group date, but thanks for answer. j.tanggal as smalldatetime
    – Davis
    Commented Jul 18, 2017 at 21:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.