年 月 日 时 频次
2007 01 02 21 4
2007 01 02 22 10
2007 01 02 23 6
2007 01 03 00 4
2007 01 03 02 10
2007 01 03 09 6
2007 01 03 10 2
2007 01 03 21 1
2007 01 04 04 2
2007 01 04 09 1
2007 01 04 10 1
2007 01 04 11 2
2007 01 04 21 1
2007 01 04 22 3
2007 01 04 23 12
现在想分组统计每天21时-08时的总频次
如:上表结果为:
年 月 日 时 频次
2007 01 02 21时-08时 34
2007 01 03 21时-08时 3
2007 01 04 21时-08时 21
2007 01 02 21 4
2007 01 02 22 10
2007 01 02 23 6
2007 01 03 00 4
2007 01 03 02 10
2007 01 03 09 6
2007 01 03 10 2
2007 01 03 21 1
2007 01 04 04 2
2007 01 04 09 1
2007 01 04 10 1
2007 01 04 11 2
2007 01 04 21 1
2007 01 04 22 3
2007 01 04 23 12
现在想分组统计每天21时-08时的总频次
如:上表结果为:
年 月 日 时 频次
2007 01 02 21时-08时 34
2007 01 03 21时-08时 3
2007 01 04 21时-08时 21
解决方案 »
- sql 两个xml关联操作
- 请教用1t和1.5t的硬盘来装500g并且持续增加的数据库,性能差距大吗
- 关于按时间段录入价格的功能,请高人指点
- 给50分 关于openrowset
- 明天要给人面试,因为是第一次,所以短时间内想不出什么问题,大家帮忙出点问题
- makepipe,readpipe 如何使用?命名管道究竟如何使用? 添加了连接服务器后,能够显示它的表和视图但是不能查询等任何操作,有什么用?
- sql server求指导 与php有关!急急急
- 关于存储过程内存占用的疑问
- 100分求解:一SQL新建登录的语句
- VFP数据加密问题,如何加密dbf里面的密码字段?
- left join on 的问题
- 这句SQL语句是什么意思
from tb
group by 年,月,日,时
from tb
group by 年 , 月 , 日 ,
--> Author : jinjazzli
--> Target : ---->1000
--> Date : 2009-12-15 15:25:07
--> Version: SQL Server 2005
---------------------------------------------
--> 测试数据: @tb
declare @tb table (年 int,月 varchar(2),日 varchar(2),时 varchar(2),频次 int)
insert into @tb
select 2007,'01','02','21',4 union all
select 2007,'01','02','22',10 union all
select 2007,'01','02','23',6 union all
select 2007,'01','03','00',4 union all
select 2007,'01','03','02',10 union all
select 2007,'01','03','09',6 union all
select 2007,'01','03','10',2 union all
select 2007,'01','03','21',1 union all
select 2007,'01','04','04',2 union all
select 2007,'01','04','09',1 union all
select 2007,'01','04','10',1 union all
select 2007,'01','04','11',2 union all
select 2007,'01','04','21',1 union all
select 2007,'01','04','22',3 union all
select 2007,'01','04','23',12select
年,月,日,
时 =max('21时-08时'),
频次=sum(case when cast(时 as int)>=21 or cast(时 as int)<=8 then 频次 else 0 end)
from @tb
group by 年,月,日
年 月 日 时 频次
----------- ---- ---- --------- -----------
2007 01 02 21时-08时 20
2007 01 03 21时-08时 15
2007 01 04 21时-08时 18(3 行受影响)
if object_id('[tb]') is not null drop table [tb]
create table [tb]([年] int,[月] varchar(2),[日] varchar(2),[时] varchar(2),[频次] int)
insert [tb]
select 2007,'01','02','21',4 union all
select 2007,'01','02','22',10 union all
select 2007,'01','02','23',6 union all
select 2007,'01','03','00',4 union all
select 2007,'01','03','02',10 union all
select 2007,'01','03','09',6 union all
select 2007,'01','03','10',2 union all
select 2007,'01','03','21',1 union all
select 2007,'01','04','04',2 union all
select 2007,'01','04','09',1 union all
select 2007,'01','04','10',1 union all
select 2007,'01','04','11',2 union all
select 2007,'01','04','21',1 union all
select 2007,'01','04','22',3 union all
select 2007,'01','04','23',12select 年,月,日,
'21时-08时' as [时],
sum([频次]) as [频次]
from
(select 年,月,
(case when [时]<'08' then right('00'+ltrim(cast([日] as int)-1),2) else [日] end) as [日],
[频次]
from [tb]) t
group by 年,月,日
go
create table [tb] (年 int,月 nvarchar(4),日 nvarchar(4),时 nvarchar(4),频次 int)
insert into [tb]
select 2007,'01','02','21',4 union all
select 2007,'01','02','22',10 union all
select 2007,'01','02','23',6 union all
select 2007,'01','03','00',4 union all
select 2007,'01','03','02',10 union all
select 2007,'01','03','09',6 union all
select 2007,'01','03','10',2 union all
select 2007,'01','03','21',1 union all
select 2007,'01','04','04',2 union all
select 2007,'01','04','09',1 union all
select 2007,'01','04','10',1 union all
select 2007,'01','04','11',2 union all
select 2007,'01','04','21',1 union all
select 2007,'01','04','22',3 union all
select 2007,'01','04','23',12
select 年,月,日,N'[21时-08时]'时,频次=Sum(频次)
from(
select 年,月,日,N'[21时-08时]'时,频次=Sum(频次)
from tb where cast(时 as int) between 21 and 23
group by 年,月,日
union all
select 年,月,日-1 日,N'[21时-08时]'时,频次=Sum(频次)
from tb where cast(时 as int) between 0 and 8
group by 年,月,日
)t group by 年,月,日
/*
年 月 日 时 频次
----------- ---- ----------- --------- -----------
2007 01 2 [21时-08时] 34
2007 01 3 [21时-08时] 3
2007 01 4 [21时-08时] 16(3 個資料列受到影響)
*/
from table1
where 时 >='21' or 时=<'08'
group by 年,月,日
insert into @tb
select 2007,'01','02','21',4 union all
select 2007,'01','02','22',10 union all
select 2007,'01','02','23',6 union all
select 2007,'01','03','00',4 union all
select 2007,'01','03','02',10 union all
select 2007,'01','03','09',6 union all
select 2007,'01','03','10',2 union all
select 2007,'01','03','21',1 union all
select 2007,'01','04','04',2 union all
select 2007,'01','04','09',1 union all
select 2007,'01','04','10',1 union all
select 2007,'01','04','11',2 union all
select 2007,'01','04','21',1 union all
select 2007,'01','04','22',3 union all
select 2007,'01','04','23',12SELECT DATEPART(yyyy, 日2) AS 年,CONVERT(NVARCHAR(2),日2 , 101) AS 月, CONVERT(NVARCHAR(2),日2 , 103) , '21时-08时' AS 时,频次 FROM (
SELECT 日2,SUM(频次) AS 频次 FROM (
SELECT *, CONVERT(INT, 时) AS 时2, CASE WHEN CONVERT(INT, 时)<'8' THEN CONVERT(DATETIME ,年+'-'+月+'-'+日 )-1 ELSE CONVERT(DATETIME ,年+'-'+月+'-'+日 ) END AS 日2 FROM @tb
) AS b
WHERE 时2>=21 OR 时2<8
GROUP BY b.日2 ) AS a
declare @tb table (年 NVARCHAR(4),月 NVARCHAR(2),日 NVARCHAR(2) ,时 NVARCHAR(2),频次 int)
insert into @tb
select 2007, '01 ', '02 ', '21 ',4 union all
select 2007, '01 ', '02 ', '22 ',10 union all
select 2007, '01 ', '02 ', '23 ',6 union all
select 2007, '01 ', '03 ', '00 ',4 union all
select 2007, '01 ', '03 ', '02 ',10 union all
select 2007, '01 ', '03 ', '09 ',6 union all
select 2007, '01 ', '03 ', '10 ',2 union all
select 2007, '01 ', '03 ', '21 ',1 union all
select 2007, '01 ', '04 ', '04 ',2 union all
select 2007, '01 ', '04 ', '09 ',1 union all
select 2007, '01 ', '04 ', '10 ',1 union all
select 2007, '01 ', '04 ', '11 ',2 union all
select 2007, '01 ', '04 ', '21 ',1 union all
select 2007, '01 ', '04 ', '22 ',3 union all
select 2007, '01 ', '04 ', '23 ',12 SELECT DATEPART(yyyy, 日2) AS 年,CONVERT(NVARCHAR(2),日2 , 101) AS 月, CONVERT(NVARCHAR(2),日2 , 103) , '21时-08时 ' AS 时,频次 FROM (
SELECT 日2,SUM(频次) AS 频次 FROM (
SELECT *, CONVERT(INT, 时) AS 时2, CASE WHEN CONVERT(INT, 时) < '8 ' THEN CONVERT(DATETIME ,年+ '- '+月+ '- '+日 )-1 ELSE CONVERT(DATETIME ,年+ '- '+月+ '- '+日 ) END AS 日2 FROM @tb
) AS b
WHERE 时2> =21 OR 时2 <8
GROUP BY b.日2 ) AS a
insert into @tb
select 2007, '01 ', '02 ', '21 ',4 union all
select 2007, '01 ', '02 ', '22 ',10 union all
select 2007, '01 ', '02 ', '23 ',6 union all
select 2007, '01 ', '03 ', '00 ',4 union all
select 2007, '01 ', '03 ', '02 ',10 union all
select 2007, '01 ', '03 ', '09 ',6 union all
select 2007, '01 ', '03 ', '10 ',2 union all
select 2007, '01 ', '03 ', '21 ',1 union all
select 2007, '01 ', '04 ', '04 ',2 union all
select 2007, '01 ', '04 ', '09 ',1 union all
select 2007, '01 ', '04 ', '10 ',1 union all
select 2007, '01 ', '04 ', '11 ',2 union all
select 2007, '01 ', '04 ', '21 ',1 union all
select 2007, '01 ', '04 ', '22 ',3 union all
select 2007, '01 ', '04 ', '23 ',12 SELECT DATEPART(yyyy, 日2) AS 年,CONVERT(NVARCHAR(2),日2 , 101) AS 月, CONVERT(NVARCHAR(2),日2 , 103) , '21时-08时 ' AS 时,频次 FROM (
SELECT 日2,SUM(频次) AS 频次 FROM (
SELECT *, CONVERT(INT, 时) AS 时2, CASE WHEN CONVERT(INT, 时) < '8 ' THEN CONVERT(DATETIME ,年+ '- '+月+ '- '+日 )-1 ELSE CONVERT(DATETIME ,年+ '- '+月+ '- '+日 ) END AS 日2 FROM @tb
) AS b
WHERE 时2> =21 OR 时2 <8
GROUP BY b.日2 ) AS a