Wednesday, March 28, 2012

How to consolidate the count?

Reg code Cntry code Area code Count
--- ---- --- --
AF AO CAB 15
AF AO LAD 20
AF BF OUA 23
AF BI BJM 11
AF BW GBE 72
AF CD FIH 30
AF CD MNB 8

I need the result like this

Reg code cntry code Total

AF AO 35
AF BF 23
AF BI 11
AF Bw 72
AF CD 38

consolidated total of area code 15+20 = 35, 30 + 8 = 38

table structures(two tables I am using)

create temp table country (ccode char(5),
acode char(4), rcode char(4));

create temp table report(count char(5),acode char(5));

how to write the sql?This is a simple sum and group by:

select c.rcode, c.ccode, sum(t.count)
from country c, temp t
where c.acode = t.acode
group by c.rcode, c.ccode;

BTW, why is column count declared as char(5)?

No comments:

Post a Comment