| Author |
Topic |
|
esthera
Master Smack Fu Yak Hacker
1410 Posts |
Posted - 2008-12-17 : 06:08:06
|
| I haveSELECT affiliatecode,DATEADD(ww,DATEDIFF(ww,0,date),0) AS date,COUNT(*)FROM messagesWHERE date BETWEEN @startdate AND dateadd(ww,1,@enddate)GROUP BY affiliatecode,DATEADD(ww,DATEDIFF(ww,0,date),0)How can I get it to show every week even if there are no records?(if there are no records it should show 0) |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2008-12-17 : 06:12:36
|
| SELECT affiliatecode,COALESCE(DATEADD(ww,DATEDIFF(ww,0,date),0),0) AS date,COUNT(*)FROM messagesWHERE date BETWEEN @startdate AND dateadd(ww,1,@enddate)GROUP BY affiliatecode,COALESCE(DATEADD(ww,DATEDIFF(ww,0,date),0),0) |
 |
|
|
Nageswar9
Aged Yak Warrior
600 Posts |
Posted - 2008-12-17 : 06:15:16
|
| SELECT affiliatecode,ISNULL(DATEADD(ww,DATEDIFF(ww,0,date),0),0) AS date,COUNT(*)FROM messagesWHERE date BETWEEN @startdate AND dateadd(ww,1,@enddate)GROUP BY affiliatecode,COALESCE(DATEADD(ww,DATEDIFF(ww,0,date),0),0)I Struggle For Excellence |
 |
|
|
esthera
Master Smack Fu Yak Hacker
1410 Posts |
Posted - 2008-12-17 : 06:20:55
|
| that's not working |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-12-17 : 06:23:52
|
Use an auxiliary week number table.SELECT affiliatecode, DATEADD(ww, DATEDIFF(ww, 0, date), 0) AS date, SUM(yak)FROM ( SELECT affiliatecode, date, 1 AS yak FROM messages WHERE date BETWEEN @startdate AND dateadd(ww, 1, @enddate) UNION ALL SELECT c.affiliatecode, DATEADD(DAY, 7 * v.Number, @StartDate), 0 FROM master..spt_values AS v CROSS JOIN ( SELECT DISTINCT affiliatecode FROM messages WHERE date BETWEEN @startdate AND dateadd(ww, 1, @enddate) ) AS c WHERE v.type = 'P' AND v.Number <= DATEDIFF(WW, @startdate, @enddate) ) AS dGROUP BY affiliatecode, DATEADD(ww, DATEDIFF(ww, 0, date), 0) E 12°55'05.63"N 56°04'39.26" |
 |
|
|
esthera
Master Smack Fu Yak Hacker
1410 Posts |
Posted - 2008-12-17 : 06:25:25
|
| both are still not showing weks/affilate codes that are 0 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-12-17 : 06:26:30
|
| [code];With CTE(Date)AS(SELECT @startdateUNION ALLSELECT DATEADD(dd,1,Date)FROM CTEWHERE DATEADD(dd,1,Date)<=@enddate)SELECT t.affiliatecode,DATEADD(ww,DATEDIFF(ww,0,t.date),0),COUNT(*) AS RecCntFROM (SELECT DISTINCT affiliatecode FROM messages)mCROSS JOIN CTE c )tLEFT JOIN messages mON m.date BETWEEN @startdate AND dateadd(ww,1,@enddate)AND m.affiliatecode=t.affiliatecodeGROUP BY t.affiliatecode,DATEADD(ww,DATEDIFF(ww,0,t.date),0)[/code] |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-12-17 : 06:29:04
|
quote: Originally posted by esthera both are still not showing weks/affilate codes that are 0
Really?I am certain of one thing. You haven't even tested the suggestion I posted 12/17/2008 : 06:23:52.What do you need? Absolute Spoonfeeding (tm)?DECLARE @Messages TABLE ( AffiliateCode VARCHAR(20), date DATETIME )INSERT @MessagesSELECT 'A', '2008-01-01' UNION ALLSELECT 'B', '2008-02-01'DECLARE @StartDate DATETIME, @EndDate DATETIMESELECT @StartDate = '2008-01-01', @EndDate = '2008-03-31'SELECT affiliatecode, DATEADD(ww, DATEDIFF(ww, 0, date), 0) AS date, SUM(yak)FROM ( SELECT affiliatecode, date, 1 AS yak FROM @messages WHERE date BETWEEN @startdate AND dateadd(ww, 1, @enddate) UNION ALL SELECT c.affiliatecode, DATEADD(DAY, 7 * v.Number, @StartDate), 0 FROM master..spt_values AS v CROSS JOIN ( SELECT DISTINCT affiliatecode FROM @messages WHERE date BETWEEN @startdate AND dateadd(ww, 1, @enddate) ) AS c WHERE v.type = 'P' AND v.Number <= DATEDIFF(WW, @startdate, @enddate) ) AS dGROUP BY affiliatecode, DATEADD(ww, DATEDIFF(ww, 0, date), 0)ORDER BY affiliatecode, DATEADD(ww, DATEDIFF(ww, 0, date), 0) My suggestion returns this resultsetaffiliatecode date (No column name)A 2007-12-31 1A 2008-01-07 0A 2008-01-14 0A 2008-01-21 0A 2008-01-28 0A 2008-02-04 0A 2008-02-11 0A 2008-02-18 0A 2008-02-25 0A 2008-03-03 0A 2008-03-10 0A 2008-03-17 0A 2008-03-24 0A 2008-03-31 0B 2007-12-31 0B 2008-01-07 0B 2008-01-14 0B 2008-01-21 0B 2008-01-28 1B 2008-02-04 0B 2008-02-11 0B 2008-02-18 0B 2008-02-25 0B 2008-03-03 0B 2008-03-10 0B 2008-03-17 0B 2008-03-24 0B 2008-03-31 0 E 12°55'05.63"N 56°04'39.26" |
 |
|
|
esthera
Master Smack Fu Yak Hacker
1410 Posts |
Posted - 2008-12-17 : 06:40:06
|
| thanks Peso - I didn't see yours before :) |
 |
|
|
|
|
|