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 |
|
craigmacca
Posting Yak Master
142 Posts |
Posted - 2008-11-27 : 04:43:45
|
| Hi i need a stored proc to do this1. get a count of rows2. group them into monthsso i can display on my webpagejan 48feb 35march 12if the count is zero then i dont want to show anything at alli have tried thisSELECT COUNT(SchProcessID) AS MonthCount, DATENAME(MONTH, SchSMS_Date) AS [MonthName]FROM ScheduledMessageToSendWHERE SchClubID = @ClubIdGROUP BY DATENAME(MONTH, SchSMS_Date)ORDER BY SchSMS_Date |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-11-27 : 04:48:15
|
| See which one you wantSELECT COUNT(SchProcessID) AS MonthCount, DATENAME(MONTH, SchSMS_Date) AS [MonthName]FROM ScheduledMessageToSendWHERE SchClubID = @ClubIdGROUP BY DATENAME(MONTH, SchSMS_Date)HAVING COUNT(SchProcessID)>0 ORDER BY SchSMS_DateSELECT COUNT(SchProcessID) AS MonthCount, DATEADD(MONTH,DATEDIFF(MONTH, 0,SchSMS_Date),0) AS [MonthName]FROM ScheduledMessageToSendWHERE SchClubID = @ClubIdGROUP BY DATEADD(MONTH,DATEDIFF(MONTH, 0,SchSMS_Date),0)HAVING COUNT(SchProcessID)>0 ORDER BY SchSMS_DateMadhivananFailing to plan is Planning to fail |
 |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2008-11-27 : 05:12:35
|
| If the count is 0 then it won't appear so there's no need to worryYour problem is trying to order by the date when it doesn't appear in the resultset.SELECT COUNT(*) AS MonthCount, DATENAME(MONTH, SchSMS_Date) AS [MonthName]FROM ScheduledMessageToSendWHERE SchClubID = @ClubIdGROUP BY DATENAME(MONTH, SchSMS_Date)ORDER BY DATENAME(MONTH, SchSMS_Date)orselect MonthCount, left(yrmth,4) as yr, datename(mm,yrmth+'01') as MonthNamefrom(SELECT COUNT(*) AS MonthCount, convert(varchar(6),SchSMS_Date,112) as yrmthFROM ScheduledMessageToSendWHERE SchClubID = @ClubIdGROUP BY convert(varchar(6),SchSMS_Date,112)) aorder by yrmthJust take the yr out of the select if you only have one year.or if it's across yearsselect MonthCount, datename(mm,'1900' + mth+'01') as MonthNamefrom(SELECT COUNT(*) AS MonthCount, right(convert(varchar(6),SchSMS_Date,112),2) as mthFROM ScheduledMessageToSendWHERE SchClubID = @ClubIdGROUP BY right(convert(varchar(6),SchSMS_Date,112),2)) aorder by mth==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
|
|
|
|
|