2014-08-28 42 views
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 

如果今天是2014年8月5日,我想结果是这样的:

| 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 

所以我希望过去一周(每日)的注册用户数。 我试图找到它在谷歌(失败) - 但是,我希望你能帮助。

+0

你能说清楚吗? 2014-08-01 12:00:00出现三次,但在你的结果显示1 – 2014-08-28 17:31:32

回答

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()[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 
+0

#temptable数据库中的真正表(这是否创建一个表)?但是,结果明显符合我的预期。谢谢你。我必须为此创建一个过程(这是关于MySQL,我正在使用phpmyadmin)。 – AmazingTurtle 2014-08-29 18:22:28

+1

@BlackHat'#temptable'创建一个仅在当前会话中可见的本地临时表。 我可能会把它放到一个存储过程中,它会为它创建一个计划,并且在每次运行时都会更快。 一些简短的阅读,很好地解释它。 [Here](http://decipherinfosys.wordpress.com/2007/05/04/temporary-tables-ms-sql-server/) 有关#,##和@:[Here](http ://stackoverflow.com/questions/2219714/sql-server-tables-what-is-the-difference-between-and) – Hashman 2014-09-02 12:08:59

0

尝试是这样的:

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 

如果哟ü尝试筛选出使用的东西登记超过7天以上的像datediff()

where datediff(day,t.registrationDate,getdate()) <= 7 

你转身列registrationDate表达。因此,查询优化器无法使用任何可能应用的索引,从而强制进行表扫描。如果桌子很大,表现很可能是......不理想。