Author |
Topic |
Steve2106
Posting Yak Master
183 Posts |
Posted - 2012-11-21 : 11:25:44
|
Hi there,I need to get a count of records by their creation date.I need something like:Sep 2012: 185Oct 2012: 230Nov 2012: 176How would I do that?Thanks as always for your help.Best Regards,Steve.Steve |
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2012-11-21 : 11:27:21
|
SELECT DATEADD(month,DATEDIFF(month,0,yourDate),0),count(*)FROM yourTableGROUP BY DATEADD(month,DATEDIFF(month,0,yourDate),0)JimLet the front end do the formatting if possibleEveryday I learn something that somebody else already knew |
|
|
Steve2106
Posting Yak Master
183 Posts |
Posted - 2012-11-21 : 11:43:43
|
Hi Jim,Thanks for your quick response.That works great but would you know how I can show just"2012-10-01" instead of "2012-10-01 00:00:00.000" the time part makes it look bad.Also there is an archive table that I need to include in the counts. Is there a way to combine the 2 counts. The 2 Tables are identical.Thanks you very much for your help I really appreciate it.Best Regards,Steve |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2012-11-21 : 12:07:08
|
Use convert(varchar(11),DATEADD(month,DATEDIFF(month,0,current_timestamp),0),121)in both the select and group by. Wherever the data ends up iw where the formatting should be done, though.You can combine the 2 tables using unionSELECT DATEADD(month,DATEDIFF(month,0,yourDate),0),count(*)FROM yourTableGROUP BY DATEADD(month,DATEDIFF(month,0,yourDate),0)UNIONSELECT DATEADD(month,DATEDIFF(month,0,yourDate),0),count(*)FROM yourOtherTableGROUP BY DATEADD(month,DATEDIFF(month,0,yourDate),0)JimEveryday I learn something that somebody else already knew |
|
|
Steve2106
Posting Yak Master
183 Posts |
Posted - 2012-11-22 : 04:19:54
|
Hi Jim,The convert worked perfect. Thankyou.The union is not as I expected. This puts in 2 values for each month.Is there a way to combine the values so instead of having.2012-11-01: 1402012-11-01: 80I could have2012-11-01: 220Thanks for your continued help, I appreciate it.Best Regards,Steve |
|
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2012-11-22 : 05:40:49
|
You can do as follows:SELECT Column1, SUM(column2)FROM (SELECT DATEADD(month,DATEDIFF(month,0,yourDate),0) Column1 ,count(*) Column2 FROM yourTable GROUP BY DATEADD(month,DATEDIFF(month,0,yourDate),0) UNION SELECT DATEADD(month,DATEDIFF(month,0,yourDate),0),count(*) FROM yourOtherTable GROUP BY DATEADD(month,DATEDIFF(month,0,yourDate),0) ) tGROUP BY Column1 --Chandu |
|
|
Steve2106
Posting Yak Master
183 Posts |
Posted - 2012-11-22 : 09:32:40
|
Hi Chandu,That worked perfect, just what I needed.I appreciate your time.Best Regards,Steve |
|
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2012-11-23 : 01:09:42
|
quote: Originally posted by Steve2106 Hi Chandu,That worked perfect, just what I needed.I appreciate your time.Best Regards,Steve
Welcome--Chandu |
|
|
|
|
|