年       月       日       时       频次
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

解决方案 »

  1.   

    select 年,月,日,时,频次=Sum(频次)
    from tb
    group by 年,月,日,时
      

  2.   

    你的频次结果貌似有误select 年    ,  月   ,   日 ,     时 =  '21时-08时'   频次 =sum(频次)
    from tb 
    group by 年   ,   月   ,  日 ,
      

  3.   

    ---------------------------------------------
    --> 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 行受影响)
      

  4.   

    --> 测试数据:[tb]
    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  年,月,日
      

  5.   

    if object_id('[tb]') is not null drop table [tb]
    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 個資料列受到影響)
    */
      

  6.   

    select 年,月,日,'21时-08时' as  时,sum(频次) as 频次
    from table1
    where 时 >='21' or 时=<'08'
    group by 年,月,日
      

  7.   

    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',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
      

  8.   


    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 
      

  9.   

    =。=晕,~~再贴一次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