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 |
|
Pregz
Starting Member
2 Posts |
Posted - 2012-03-22 : 10:01:19
|
| Hi GuysI have following outputYesr Month DayID Day Count Number Cost2012 2 3 03/02/2012 228 42467 (R30) 68402012 2 6 06/02/2012 307 42467 (R30) 92102012 2 7 07/02/2012 86 42467 (R30) 25802012 2 9 09/02/2012 970 42467 (R30) 291002012 2 10 10/02/2012 2715 42467 (R30) 81450using SQL : select YEAR(sentdate) Year,MONTH(sentdate) Month,DAY(sentdate) DayID,CONVERT(varchar,sentdate,103) as Day,COUNT( *) as SMSsent,case MSISDN when '42467' then MSISDN+' (R30)' end as MSISDN,case MSISDN when '42467' then Cast((COUNT(*)*30) as float) end as Cost from tblMobileMessages where GatewayType like '%simfarm%' and MSISDN in ('42467') and SendStatus=2and datediff(mm,sentdate,getdate())<=1 group by year(sentdate), MONTH(sentdate),CONVERT(varchar,sentdate,103) ,DAY(sentdate),MSISDNorder by year(sentdate), MONTH(sentdate),Day(sentdate)I want to alter this query or find a better way to include constant values in the output i.e i would like to include a record for every day even if there is no data in the database e.g2012 2 2 02/02/2012 228 42467 (R30) 68402012 2 3 03/02/2012 0 42467 (R30) 02012 2 4 04/02/2012 0 42467 (R30) 02012 2 5 05/02/2012 0 42467 (R30) 02012 2 6 06/02/2012 307 42467 (R30) 92102012 2 7 07/02/2012 86 42467 (R30) 25802012 2 9 09/02/2012 970 42467 (R30) 291002012 2 10 10/02/2012 2715 42467 (R30) 81450 |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2012-03-22 : 10:28:06
|
| One easy solution is to create a Date/Calendar table and join to that. You could also do the same with the function or CTE, but I'd suggest a table. |
 |
|
|
|
|
|
|
|