SELECT registerDate, count(registerDate) FROM [TABLE] WHERE
registerDate between (GETDATE()-7) and GETDATE()
group by registerDate
order by registerDate desc
这将需要一个表所示:
2 |1905-06-26 00:00:00.000
4 |2014-08-03 00:00:00.000
5 |2014-08-02 00:00:00.000
1 |2014-08-01 00:00:00.000
3 |2014-07-01 00:00:00.000
6 |2010-07-01 00:00:00.000
7 |2015-07-01 00:00:00.000
8 |2014-08-28 00:00:00.000
9 |2014-08-26 00:00:00.000
10 |2014-08-26 00:00:00.000
,打造:
2014-08-28 00:00:00.000 | 1
2014-08-26 00:00:00.000 | 2
的问题,这是它不显示不在表中的天。 给我多一点时间,我会有一个更新的版本。
编辑:
现在更复杂的一个...
-- Declare how far back you want to go
DECLARE @DAYSBACK int = 6
-- Select into a temptable
select CONVERT(date, registerDate) as RegDate, count(registerDate) as DateCount
INTO #temptable
from Temp where registerDate between (GETDATE()-6) and GETDATE()
group by registerDate order by registerDate desc
-- Check to see if exists if not, insert row
WHILE @DAYSBACK >= 0 BEGIN
IF NOT EXISTS (select top 1 1 from #temptable
where RegDate= CONVERT(date, (GETDATE()[email protected]))
group by RegDate)
INSERT INTO #temptable values ((GETDATE()[email protected]), 0)
SET @DAYSBACK = @DAYSBACK -1
END
-- Select what you want
select * from #temptable order by RegDate desc
-- Drop the table you created.
DROP TABLE #temptable
使用与上述相同的表,它会输出:
Register Date | Date Count
--------------------------
2014-08-28 | 1
2014-08-27 | 0
2014-08-26 | 2
2014-08-25 | 0
2014-08-24 | 0
2014-08-23 | 0
2014-08-22 | 0
你能说清楚吗? 2014-08-01 12:00:00出现三次,但在你的结果显示1 – 2014-08-28 17:31:32