想对一个表进行分组查询,表中的列有:Date, Key1, type, num
表中的数据如下:
Date Key1 type num
2012-12-20 10:22:55 1111 0 1
2012-12-20 10:32:55 1111 1 2
2012-12-20 10:34:55 1111 1 3
2012-12-20 10:32:55 1112 1 2
2012-12-20 10:42:55 1112 1 1
2012-12-21 10:22:55 1111 0 1
2012-12-21 10:22:55 1111 1 3
2012-12-21 10:22:55 1111 0 5
希望通过语句得到如下结果:
2012-12-20 1111 0 1
2012-12-20 1111 1 5
2012-12-20 1112 1 3
2012-12-21 1111 0 6
2012-12-21 1111 1 3我想应该是取(第一列的年月日)和Key1 type进行分组吧,但怎么也不对,请朋友帮忙指点一下,谢谢了!
表中的数据如下:
Date Key1 type num
2012-12-20 10:22:55 1111 0 1
2012-12-20 10:32:55 1111 1 2
2012-12-20 10:34:55 1111 1 3
2012-12-20 10:32:55 1112 1 2
2012-12-20 10:42:55 1112 1 1
2012-12-21 10:22:55 1111 0 1
2012-12-21 10:22:55 1111 1 3
2012-12-21 10:22:55 1111 0 5
希望通过语句得到如下结果:
2012-12-20 1111 0 1
2012-12-20 1111 1 5
2012-12-20 1112 1 3
2012-12-21 1111 0 6
2012-12-21 1111 1 3我想应该是取(第一列的年月日)和Key1 type进行分组吧,但怎么也不对,请朋友帮忙指点一下,谢谢了!
-- Author :DBA_Huangzj
-- Date :2013-01-08 18:15:53
-- Version:
-- Microsoft SQL Server 2008 R2 (SP1) - 10.50.2500.0 (X64)
-- Jun 17 2011 00:54:03
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition (64-bit) on Windows NT 6.1 <X64> (Build 7601: Service Pack 1, v.721)
--
----------------------------------------------------------------
--> 测试数据:[huang]
if object_id('[huang]') is not null drop table [huang]
go
create table [huang]([Date] datetime,[Key1] int,[type] int,[num] int)
insert [huang]
select '2012-12-20 10:22:55',1111,0,1 union all
select '2012-12-20 10:32:55',1111,1,2 union all
select '2012-12-20 10:34:55',1111,1,3 union all
select '2012-12-20 10:32:55',1112,1,2 union all
select '2012-12-20 10:42:55',1112,1,1 union all
select '2012-12-21 10:22:55',1111,0,1 union all
select '2012-12-21 10:22:55',1111,1,3 union all
select '2012-12-21 10:22:55',1111,0,5
--------------开始查询--------------------------select CONVERT(DATE,[Date])[Date],[Key1],[type],SUM(num)num from [huang]
GROUP BY CONVERT(DATE,[Date]),[Key1],[type]
----------------结果----------------------------
/*
Date Key1 type num
---------- ----------- ----------- -----------
2012-12-20 1111 0 1
2012-12-20 1111 1 5
2012-12-20 1112 1 3
2012-12-21 1111 0 6
2012-12-21 1111 1 3(5 行受影响)
*/
----------------------------------------------------------------
-- Author :DBA_Huangzj
-- Date :2013-01-08 18:15:53
-- Version:
-- Microsoft SQL Server 2008 R2 (SP1) - 10.50.2500.0 (X64)
-- Jun 17 2011 00:54:03
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition (64-bit) on Windows NT 6.1 <X64> (Build 7601: Service Pack 1, v.721)
--
----------------------------------------------------------------
--> 测试数据:[huang]
if object_id('[huang]') is not null drop table [huang]
go
create table [huang]([Date] datetime,[Key1] int,[type] int,[num] int)
insert [huang]
select '2012-12-20 10:22:55',1111,0,1 union all
select '2012-12-20 10:32:55',1111,1,2 union all
select '2012-12-20 10:34:55',1111,1,3 union all
select '2012-12-20 10:32:55',1112,1,2 union all
select '2012-12-20 10:42:55',1112,1,1 union all
select '2012-12-21 10:22:55',1111,0,1 union all
select '2012-12-21 10:22:55',1111,1,3 union all
select '2012-12-21 10:22:55',1111,0,5
--------------开始查询--------------------------select SUBSTRING(CONVERT(VARCHAR(20),[Date],121),1,10)[Date],[Key1],[type],SUM(num)num from [huang]
GROUP BY SUBSTRING(CONVERT(VARCHAR(20),[Date],121),1,10),[Key1],[type]
----------------结果----------------------------
/*
Date Key1 type num
---------- ----------- ----------- -----------
2012-12-20 1111 0 1
2012-12-20 1111 1 5
2012-12-20 1112 1 3
2012-12-21 1111 0 6
2012-12-21 1111 1 3(5 行受影响)
*/