Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server SQL Server 2008中需要计数查询_Sql Server_Datetime_Count - Fatal编程技术网

Sql server SQL Server 2008中需要计数查询

Sql server SQL Server 2008中需要计数查询,sql-server,datetime,count,Sql Server,Datetime,Count,我的表名是abc,我想按日期和状态排列数据,其中按日期排列的计数为on,按日期排列的计数为off ID Time Status ----------- ----------------------- -------------------------------------------------- 1 2013-12-24 00:00:00 on 2 2013-12-25 00:00:00

我的表名是abc,我想按日期和状态排列数据,其中按日期排列的计数为on,按日期排列的计数为off

ID          Time                    Status
----------- ----------------------- --------------------------------------------------
1           2013-12-24 00:00:00     on
2           2013-12-25 00:00:00     on
3           2013-12-26 00:00:00     on
4           2013-12-27 00:00:00     on
5           2013-12-28 00:00:00     on
6           2013-12-29 00:00:00     on
7           2013-12-30 00:00:00     on
8           2013-12-31 00:00:00     on
9           2013-12-24 00:00:00     off
10          2013-12-25 00:00:00     off
11          2013-12-27 00:00:00     off
12          2013-12-27 00:00:00     on
13          2013-12-27 00:00:00     off
14          2013-12-27 00:00:00     on
15          2013-12-27 00:00:00     off
16          2013-12-28 00:00:00     on
17          2013-12-28 00:00:00     off
18          2013-12-28 00:00:00     on
19          2013-12-29 00:00:00     off
20          2013-12-29 00:00:00     on
21          2013-12-30 00:00:00     off
22          2013-12-30 00:00:00     on
23          2013-12-30 00:00:00     off
24          2013-12-30 00:00:00     on
25          2013-12-30 00:00:00     off
26          2013-12-31 00:00:00     on
27          2013-12-31 00:00:00     off
28          2013-12-31 00:00:00     on
29          2013-12-31 00:00:00     off
30          2013-12-31 00:00:00     on
31          2013-12-31 00:00:00     off

像这样的东西怎么样

Select time, SUM(on_cnt),SUM(off_cnt) FROM
(select time,[on] as on_cnt,[off] as off_cnt from table1
pivot 
(
 count(Status)
 for Status in ([on],[off])
) as p) as st
GROUP BY time

请不要交叉张贴:这是好的和有效的,但它会重复日期,就像它只会重复日期,如果它也有时间部分,从你的例子它没有。那么它有时间部分吗?好的,我明白,是的,它有不同的时间,你是对的,我怎么能删除时间部分?现在就开始吧。因此,类似于
CAST([Time]AS Date)DateOnly的功能非常有效,但是它使用了3个连接,这不是减少连接数量的任何方法吗?现在检查。消除连接
SELECT 
[Time], 
SUM(CASE WHEN [Status] = 'On' THEN 1 ELSE 0 END) CntON,
SUM(CASE WHEN [Status] = 'Off' THEN 1 ELSE 0 END) CntOff
FROM Table
GROUP BY [Time]