question : select distinct and sum sql syntax
hi inkubis,…
thanks for reading my message. Below is the table structure and sample data.
| CODE | AMOUNT |
| aaa | 100 |
| ccc | 200 |
| bbb | 50 |
| bbb | 100 |
| aaa | 150 |
| aaa | 100 |
I would like to know how i can select distinct and sum at the same time from the above sample data ? I am using “SELECT DISTINCT CODE,SUM(AMOUNT) FROM TABLE” as the query but it is not accepted. Thanks in advance. Best regards.
owala maksud dari pertanyaannya itu adalah gimana cara menggabungkan dan menjumlah dari tabel diatas. jadi yang code di gabung yang sama (dsitinct) trus yang amount di jumlah berdasarkan code yang sama.
with your problem, we can solve with nested select, which select in your select.
select distinct code, (select sum(amount) from nametable as nestedtable where nestedtable.code=nametable.code) from nametable
result;
aaa —– 350
bbb —– 150
ccc —– 200
If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.







Wewh, mantab jaya jawabane……
gw coba dengan kasus serupa tapi ko gagal ya? pada kasus ini menjumlah AMOUNT yg disaring melalui CODE. pada kasus saya, saya menjumlah SKS yg disaring melalui NPM dan KD_MK. bisa tolong dibantu?
@girls : boleh tau tabelnya kayak apa, trus datanya apa saja. boleh dikirim di email
inkubis@ymail.com
duh.. gw jg dicoba ga bisa kenapa yaaa.
bener ga sih kalo gw cobanya di sql trus gw tulis di formulanya…..??