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 |
|
KinYeung
Starting Member
14 Posts |
Posted - 2006-06-16 : 06:46:15
|
| Hi all,I have a table, table structure as follow:MYTABLE(ID,EFFDT,CATEGORY,TYPE)value:1,'01/01/2005','PERSONAL','A'2,'12/01/2005','PERSONAL','B'3,'08/03/2005','BUSINESS','B'4,'22/04/2005','PERSONAL','A'5,'26/04/2005','PERSONAL','A'6,'01/04/2005','BUSINESS','A'I want to generate a report like the followingMONTH TOTAL_PER_A TOTAL_PER_B TOTAL_BUS_A TOTAL_BUS_B1/2005 1 1 0 02/2005 0 0 0 03/2005 0 0 0 14/2005 2 0 1 0I've created 4 views for TOTAL_PER_A, TOTAL_PER_B TOTAL_BUS_A AND TOTAL_BUS_B (I use group by to get the count of id for each of them) then join together and get the result table.I think it's stupid but i cannot think any other better way can get the same result. Is it has a better way to do the same thingspls advice, thanksRegards,Kin |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2006-06-16 : 07:23:30
|
Something like this...  --datadeclare @MYTABLE table (ID int, EFFDT datetime, CATEGORY varchar(10), TYPE char(1))insert @MYTABLE select 1,'01/01/2005','PERSONAL','A'union all select 2,'12/01/2005','PERSONAL','B'union all select 3,'08/03/2005','BUSINESS','B'union all select 4,'22/04/2005','PERSONAL','A'union all select 5,'26/04/2005','PERSONAL','A'union all select 6,'01/04/2005','BUSINESS','A'--calculationdeclare @months table (month int)insert @months select '200501'union all select '200502'union all select '200503'union all select '200504'select month, sum(case when CATEGORY = 'PERSONAL' and TYPE = 'A' then 1 else 0 end) as TOTAL_PER_A, sum(case when CATEGORY = 'PERSONAL' and TYPE = 'B' then 1 else 0 end) as TOTAL_PER_B, sum(case when CATEGORY = 'BUSINESS' and TYPE = 'A' then 1 else 0 end) as TOTAL_BUS_A, sum(case when CATEGORY = 'BUSINESS' and TYPE = 'B' then 1 else 0 end) as TOTAL_BUS_Bfrom @months a left outer join @MYTABLE b on a.month = year(effdt) * 100 + month(effdt)group by month/*resultsmonth TOTAL_PER_A TOTAL_PER_B TOTAL_BUS_A TOTAL_BUS_B ----------- ----------- ----------- ----------- ----------- 200501 1 1 0 0200502 0 0 0 0200503 0 0 0 1200504 2 0 1 0*/ Ryan Randallwww.monsoonmalabar.com London-based IT consultancy Solutions are easy. Understanding the problem, now, that's the hard part. |
 |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2006-06-16 : 07:28:55
|
| By the way, you might find it useful to use a function to get the '@months' table (instead of hard-coding it). If so, I'd recommend using this one...http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=61519And if you need a 'dynamic pivot', these will be useful...http://www.sqlteam.com/item.asp?ItemID=2955http://weblogs.sqlteam.com/jeffs/archive/2005/05/02/4842.aspxRyan Randallwww.monsoonmalabar.com London-based IT consultancy Solutions are easy. Understanding the problem, now, that's the hard part. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-06-16 : 07:42:25
|
| Also read about Cross-tab Reports in sql server help fileMadhivananFailing to plan is Planning to fail |
 |
|
|
KinYeung
Starting Member
14 Posts |
Posted - 2006-06-16 : 22:05:28
|
| Thanks a lot. It's useful. |
 |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2006-06-16 : 23:38:59
|
Another way, using the date function that Ryan mentioned.declare @MYTABLE table (ID int, EFFDT datetime, CATEGORY varchar(10), TYPE char(1))insert @MYTABLEselect 1,'20050101','PERSONAL','A'union all select 2,'20050112','PERSONAL','B'union all select 3,'20050308','BUSINESS','B'union all select 4,'20050422','PERSONAL','A'union all select 5,'20050426','PERSONAL','A'union all select 6,'20050401','BUSINESS','A'select MONTH = max(YEAR_MONTH_NAME_LONG), sum(case x when 11 then 1 else 0 end) as PER_A, sum(case x when 12 then 1 else 0 end) as PER_B, sum(case x when 21 then 1 else 0 end) as BUS_A, sum(case x when 22 then 1 else 0 end) as BUS_Bfrom -- Date function from script library dbo.F_TABLE_DATE('20050101','20050430') a left outer join ( select *, x = case CATEGORY when 'PERSONAL' then 10 when 'BUSINESS' then 20 end+ case TYPE when 'A' then 1 when 'B' then 2 end from @MYTABLE ) b on a.date = b.EFFDTgroup by a.MONTH_SEQ_NOorder by a.MONTH_SEQ_NOResults:MONTH PER_A PER_B BUS_A BUS_B -------------- ----------- ----------- ----------- ----------- 2005 January 1 1 0 02005 February 0 0 0 02005 March 0 0 0 12005 April 2 0 1 0(4 row(s) affected) CODO ERGO SUM |
 |
|
|
|
|
|
|
|