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 |
jonneponne
Starting Member
5 Posts |
Posted - 2006-11-28 : 06:46:35
|
I've got a table containing (among some other columns) 3 different type columns.TABLE xxxx as int type 1 as string type 2 as string type 3 as string date as DateFirst i'd like to select the number of counts each of these appear in my table. I realized that it can be done by union 3 select statements each grouped by a type.select type1, count(type1) from table group by type1unionselect type2, count(type2) from table group by type2...Now this is time consuming and not what I would like to doSecondly i'll try to do something likeSELECT type1 , SUM(CASE WHEN MONTH(date) = 1 THEN 1 END) AS 'Januar' ,SUM(CASE WHEN MONTH(date) = 2 THEN 1 END) AS 'Februar' ,SUM(CASE WHEN MONTH(date) = 3 THEN 1 END) AS 'Mars' ,SUM(CASE WHEN MONTH(date) = 4 THEN 1 END) AS 'April' ,SUM(CASE WHEN MONTH(date) = 5 THEN 1 END) AS 'Mai' ,SUM(CASE WHEN MONTH(date) = 6 THEN 1 END) AS 'Juni' ,SUM(CASE WHEN MONTH(date) = 7 THEN 1 END) AS 'Juli' ,SUM(CASE WHEN MONTH(date) = 8 THEN 1 END) AS 'August' ,SUM(CASE WHEN MONTH(date) = 9 THEN 1 END) AS 'Sept' ,SUM(CASE WHEN MONTH(date) = 10 THEN 1 END) AS 'Okt' ,SUM(CASE WHEN MONTH(date) = 11 THEN 1 END) AS 'Nove' ,SUM(CASE WHEN MONTH(date) = 12 THEN 1 END) AS 'Desem' ,SUM(CASE WHEN YEAR(date) = 2006 THEN 1 END) AS 'TOTAL'FROM tableWHERE YEAR(date) = 2006GROUP BY typeThis in combination with union the other 2 types results in what I'd like to do but then again, phuu this is some crappy approach :)Any suggustions in how to solve this?I.E I'd like to list each type count for each month like this Jan Feb Mars ... type1 23 43 45type2 12 11 15type3 54 55 65Any hints?/J |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-11-28 : 07:05:01
|
Yes. Try thisSELECT type AS 'Category', SUM(CASE WHEN [month] = 1 THEN 1 ELSE 0 END) AS 'Januar', SUM(CASE WHEN [month] = 2 THEN 1 ELSE 0 END) AS 'Februar', SUM(CASE WHEN [month] = 3 THEN 1 ELSE 0 END) AS 'Mars', SUM(CASE WHEN [month] = 4 THEN 1 ELSE 0 END) AS 'April', SUM(CASE WHEN [month] = 5 THEN 1 ELSE 0 END) AS 'Mai', SUM(CASE WHEN [month] = 6 THEN 1 ELSE 0 END) AS 'Juni', SUM(CASE WHEN [month] = 7 THEN 1 ELSE 0 END) AS 'Juli', SUM(CASE WHEN [month] = 8 THEN 1 ELSE 0 END) AS 'August', SUM(CASE WHEN [month] = 9 THEN 1 ELSE 0 END) AS 'Sept', SUM(CASE WHEN [month] = 10 THEN 1 ELSE 0 END) AS 'Okt', SUM(CASE WHEN [month] = 11 THEN 1 ELSE 0 END) AS 'Nove', SUM(CASE WHEN [month] = 12 THEN 1 ELSE 0 END) AS 'Desem', SUM(1) AS 'TOTAL'FROM ( SELECT [Type 1] type, MONTH([date]) [month] FROM YourTable WHERE [date] >= '20060101' AND [date] < '20070101' UNION ALL SELECT [Type 2], MONTH([date]) FROM YourTable WHERE [date] >= '20060101' AND [date] < '20070101' UNION ALL SELECT [Type 3], MONTH([date]) FROM YourTable WHERE [date] >= '20060101' AND [date] < '20070101' ) mGROUP BY typeORDER BY type Peter LarssonHelsingborg, Sweden |
 |
|
jonneponne
Starting Member
5 Posts |
Posted - 2006-11-28 : 07:37:13
|
Thanks it works, and its much less code!But, its not faster. Is there a way to work around the unions? |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-11-28 : 08:00:47
|
No. Not with the table design you use today.But it will be a lot faster if you can add a proper index for YourTable table.Make an index for [Type 1], [Type 2], [Type 3] and [date] columns.Then run query twice and take time second time.Peter LarssonHelsingborg, Sweden |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-11-28 : 08:47:39
|
>>'Okt', 'Nove','Desem',Which language is this? MadhivananFailing to plan is Planning to fail |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-11-28 : 08:53:31
|
I believe that is portuguese or castellan.Peter LarssonHelsingborg, Sweden |
 |
|
|
|
|
|
|