Sql 按日期和条目计数分组
我要简短一点,这张桌子是这样的:Sql 按日期和条目计数分组,sql,tsql,date,statistics,Sql,Tsql,Date,Statistics,我要简短一点,这张桌子是这样的: | id (int) | registerDate (DATETIME) |----------|----------------- | 1 | 2014-07-29 12:00:00 | 2 | 2014-08-01 12:00:00 | 3 | 2014-08-01 12:00:00 | 4 | 2014-08-01 12:00:00 | 5 | 2014-08-02 12:00:00 |
| id (int) | registerDate (DATETIME)
|----------|-----------------
| 1 | 2014-07-29 12:00:00
| 2 | 2014-08-01 12:00:00
| 3 | 2014-08-01 12:00:00
| 4 | 2014-08-01 12:00:00
| 5 | 2014-08-02 12:00:00
| 6 | 2014-08-02 12:00:00
| 7 | 2014-08-04 12:00:00
| registerDate (DATETIME) | count (int)
| 2014-08-04 | 1
| 2014-08-03 | 0
| 2014-08-02 | 2
| 2014-08-01 | 1
| 2014-07-31 | 0
| 2014-07-30 | 0
| 2014-07-29 | 1
select registerDate = convert(date,t.registerDate) ,
registrations = count(*)
from dbo.my_special_registration_table t
where t.registrationDate >= dateadd(day,-7,convert(date,getdate()))
group by convert(date,t.registerDate)
order by 1
如果今天是2014-08-05,我想要这样的结果:
| id (int) | registerDate (DATETIME)
|----------|-----------------
| 1 | 2014-07-29 12:00:00
| 2 | 2014-08-01 12:00:00
| 3 | 2014-08-01 12:00:00
| 4 | 2014-08-01 12:00:00
| 5 | 2014-08-02 12:00:00
| 6 | 2014-08-02 12:00:00
| 7 | 2014-08-04 12:00:00
| registerDate (DATETIME) | count (int)
| 2014-08-04 | 1
| 2014-08-03 | 0
| 2014-08-02 | 2
| 2014-08-01 | 1
| 2014-07-31 | 0
| 2014-07-30 | 0
| 2014-07-29 | 1
select registerDate = convert(date,t.registerDate) ,
registrations = count(*)
from dbo.my_special_registration_table t
where t.registrationDate >= dateadd(day,-7,convert(date,getdate()))
group by convert(date,t.registerDate)
order by 1
所以我想知道过去一周(每天)的注册用户数。
我试着在谷歌上找到它(没有成功)-但是,我希望你能帮助我
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()-@DAYSBACK))
group by RegDate)
INSERT INTO #temptable values ((GETDATE()-@DAYSBACK), 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
试着这样做:
| id (int) | registerDate (DATETIME)
|----------|-----------------
| 1 | 2014-07-29 12:00:00
| 2 | 2014-08-01 12:00:00
| 3 | 2014-08-01 12:00:00
| 4 | 2014-08-01 12:00:00
| 5 | 2014-08-02 12:00:00
| 6 | 2014-08-02 12:00:00
| 7 | 2014-08-04 12:00:00
| registerDate (DATETIME) | count (int)
| 2014-08-04 | 1
| 2014-08-03 | 0
| 2014-08-02 | 2
| 2014-08-01 | 1
| 2014-07-31 | 0
| 2014-07-30 | 0
| 2014-07-29 | 1
select registerDate = convert(date,t.registerDate) ,
registrations = count(*)
from dbo.my_special_registration_table t
where t.registrationDate >= dateadd(day,-7,convert(date,getdate()))
group by convert(date,t.registerDate)
order by 1
如果您尝试使用类似于datediff()
的方法过滤7天以上的注册:
datediff(day,t.registrationDate,getdate())在哪里可以说清楚?2014-08-01 12:00:00出现了三次,但在您的结果中,它显示1是数据库中的真实表(这会创建表吗)?然而,结果显然符合我的期望。谢谢你。我是否必须为此创建一个过程(关于MySQL,我使用的是phpmyadmin)。@BlackHat#tentable
创建一个本地临时表,该表仅在当前会话中可见。我可能会把它放到一个存储过程中,它会为它创建一个计划,并且每次运行时都会更快。一些简短的阅读很好地解释了这一点。关于#,##和@的类似问题: