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 |
|
adlo
Posting Yak Master
108 Posts |
Posted - 2004-11-15 : 11:01:48
|
| I need to do the following complex query.I have a Member Table with fieldsMember_ID int,DateCreated smalldatetimeI need a resultset containing the Date with the number of members created for that day (MemberCount). Currently my query looks like thisselect CONVERT(CHAR(10),DateCreated,111) as DateCreated,Count(Member_ID) as MemberCountFROM MemberGROUP BY CONVERT(CHAR(10),DateCreated,111)The resultset should include all days that exist between the earliest and latest DateCreated of the member table.If the date does not exist for a spesific day the membercount for that day must be 0.Thus results should look something like2004/09/01 - 1 (Earliest date in table)2004/09/02 - 02004/09/03 - 22004/09/04 - 02004/09/05 - 02004/09/06 - 1 (Latest Date in table)I have a tally table (1-10000)I would really appreciate it if someone can help me with this. |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2004-11-15 : 12:20:22
|
| [code]USE NorthwindGOSET NOCOUNT ONCREATE TABLE myTally99(Col1 int IDENTITY(1,1), Col2 char(1))GODECLARE @x intSELECT @x = 0WHILE @x < 100 BEGIN INSERT INTO myTally99(Col2) SELECT 'x' UNION ALL SELECT 'x' UNION ALL SELECT 'x' UNION ALL SELECT 'x' UNION ALL SELECT 'x' UNION ALL SELECT 'x' UNION ALL SELECT 'x' UNION ALL SELECT 'x' UNION ALL SELECT 'x' UNION ALL SELECT 'x' SELECT @x = @x + 1 ENDGO SELECT xxx.OrderDate, COUNT(o.OrderDate) FROM ( SELECT CONVERT(varchar(10),CONVERT(datetime,Col1 + (SELECT DATEDIFF(d,CONVERT(datetime,1),MIN(OrderDate)) FROM Orders)),102) AS OrderDate FROM myTally99) AS xxxLEFT JOIN (SELECT CONVERT(varchar(10),OrderDate,102) AS OrderDate FROM Orders) AS o ON xxx.OrderDate = o.OrderDate GROUP BY xxx.OrderDate ORDER BY xxx.OrderDateGOSET NOCOUNT OFFDROP TABLE myTally99GO[/code]Brett8-) |
 |
|
|
adlo
Posting Yak Master
108 Posts |
Posted - 2004-11-15 : 23:18:48
|
| Thanks.Just added WHERE myTally99<=(Select DateDiff(d,MIN(OrderDate),Max(DateCreated)) from OrderDate)to limit to range. |
 |
|
|
|
|
|
|
|