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 |
|
yumyum113
Starting Member
31 Posts |
Posted - 2007-05-02 : 18:49:14
|
Hi Guys,Hope you could again help me with this one. I was wondering if there is another way to do a query that I have written.Here is the sample datadeclare @monthly_days_complete table( Rep_type varchar(100), dtc int, total int)insert into @monthly_days_completeselect 'COBRA Report',2, 1 union allselect 'QMCSO',4,1 union allselect 'Hard Pend',3, 18 union allselect 'Hard Pend',2, 1 union allselect 'Hard Pend',7, 2 union allselect 'Hard Pend',13, 2 union allselect 'Hard Pend',26, 3 union allselect 'ID Card Error Report',0,3 union allselect 'ID Card Error Report',2,2 union allselect 'QMCSO',2,1 union allselect 'QMCSO',35,1 union allselect 'QMCSO',6,1 union allselect 'QMCSO',4,1 Here is the output that I want..rep_type aging Total trans------------------------- -------------------- -----------COBRA Report Day 2 1Hard Pend Day 15-28 3Hard Pend Day 2 1Hard Pend Day 3 18Hard Pend Day 7-14 4ID Card Error Report Day 0 3ID Card Error Report Day 2 2QMCSO Day 2 1QMCSO Day 29+ 1QMCSO Day 4 2QMCSO Day 6 1Here is how I do it...declare @temptable table( Rep_type varchar(25), dtc int, Aging varchar(20), TotalTrans int)insert into @temptableselect rep_type,dtc,null,sum(total)from @monthly_days_completegroup by rep_type,dtcupdate @temptableset aging = case when dtc=0 then 'Day 0' when dtc=1 then 'Day 1' when dtc=2 then 'Day 2' when dtc=3 then 'Day 3' when dtc=4 then 'Day 4' when dtc=5 then 'Day 5' when dtc=6 then 'Day 6' when dtc>=7 and dtc <=14 then 'Day 7-14' when dtc>=15 and dtc <=28 then 'Day 15-28' when dtc>28 then 'Day 29+' endselect rep_type,aging,sum(totaltrans)from @temptablegroup by rep_type,agingorder by rep_typeQUESTION: Could the output that I'am looking for be done with one query without using a temporary table?Thanks in advance for taking time. |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2007-05-02 : 19:16:08
|
| [code]SELECT rep_type, case when dtc=0 then 'Day 0' when dtc=1 then 'Day 1' when dtc=2 then 'Day 2' when dtc=3 then 'Day 3' when dtc=4 then 'Day 4' when dtc=5 then 'Day 5' when dtc=6 then 'Day 6' when dtc>=7 and dtc <=14 then 'Day 7-14' when dtc>=15 and dtc <=28 then 'Day 15-28' when dtc>28 then 'Day 29+' END AS aging, sum(total)FROM @monthly_days_completegroup by rep_type, case when dtc=0 then 'Day 0' when dtc=1 then 'Day 1' when dtc=2 then 'Day 2' when dtc=3 then 'Day 3' when dtc=4 then 'Day 4' when dtc=5 then 'Day 5' when dtc=6 then 'Day 6' when dtc>=7 and dtc <=14 then 'Day 7-14' when dtc>=15 and dtc <=28 then 'Day 15-28' when dtc>28 then 'Day 29+' ENDORDER BY rep_type[/code] |
 |
|
|
yumyum113
Starting Member
31 Posts |
Posted - 2007-05-02 : 19:26:27
|
Wow! wow! and wow! thanks. |
 |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2007-05-02 : 19:49:18
|
quote: Originally posted by yumyum113 Wow! wow! and wow! thanks. 
My pleasure! |
 |
|
|
|
|
|
|
|