Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
jung1975
Aged Yak Warrior
503 Posts |
Posted - 2003-04-25 : 16:58:39
|
| I have a table look like:Year Subtype counts2002 4 22002 5 2312002 6 12182002 7 1322003 1 12003 4 32003 5 592003 6 2902003 7 44And, trying to group by subtype. the result should look like:Year Subtype Counts2002 Indemity 2332002 Info 1322002 Medical 12182003 Indemity 632003 Info 442003 Medical 290I have used following SQL to get the above result. Beginselect year(clm_incdate)as injyear,clm_subtype as subtype,count(claim_key) as claimcount into #temp1from claim,loc_xrefwhere claim.ref_location = loc_xref.location_key andre_loc1 = 231456Group by year(clm_incdate),clm_subtypeorder by year(clm_incdate)EndBeginselect injyear,(Case when subtype = 7 then 'Info' when subtype = 6 then 'Medical' when subtype < 6 then 'Indemity' Else 'X'End) As subtype,claimcountfrom#temp1Group byinjyear,Case when subtype = 7 then 'Info' when subtype = 6 then 'Medical' when subtype < 6 then 'Indemity' Else 'X' End,claimcountEndBut the result look like this:Year subtype counts2002 Indemity 22002 Indemity 2312002 Info 1322002 Medical 12182003 Indemity 12003 Indemity 32003 Indemity 592003 Info 442003 Medical 290I am not sure what I am doing wrong.Any help would be appreciated it..Thanks |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2003-04-25 : 17:15:43
|
| Don't group on the claimcount field ... and I'm not sure why you are using a temp table.- JeffEdited by - jsmith8858 on 04/25/2003 17:16:02 |
 |
|
|
claire
Starting Member
19 Posts |
Posted - 2003-04-26 : 03:12:36
|
| Try thiscreate table table1(Year int,Subtype int,counts int) goinsert into table1 select 2002,4,2insert into table1 select 2002,5,231insert into table1 select 2002,6,1218insert into table1 select 2002,7,132insert into table1 select 2003,1,1insert into table1 select 2003,4,3insert into table1 select 2003,5,59insert into table1 select 2003,6,290insert into table1 select 2003,7,44go---------------------------------------------------select T.year,T.subtype,sum(T.counts) from (select year, (Case when subtype = 7 then 'Info' when subtype = 6 then 'Medical' when subtype < 6 then 'Indemity' Else 'X' End) As subtype ,sum(counts) as countsfrom table1Group by year,subtype ) as Tgroup by T.year,T.subtype order by T.yeargo |
 |
|
|
|
|
|
|
|