条件1,很多年的数据,对每月或者每星期做统计。
条件2,都是对单列数据统计,按月或星期。

解决方案 »

  1.   

    对每月或者每星期做统计对每月统计
    group by  convert(varchar(7),convert(datetime,'2007-10-20'),120)对每星期做统计
    group by  dateadd(day,-datepart(weekday,convert(datetime,时间)+@@datefirst-1)+1,convert(datetime,时间))
      

  2.   

    给一个别人的例子给你吧create table t(ip varchar(30),[time] varchar(30),[address] varchar(50))
    insert into t
    select '192.168.1.3','20061028140125','http://localhost/'  union all
    select '192.168.1.3','20061028140125','http://localhost/'  union all
    select '192.168.1.3','20061028140125','http://localhost/'  union all
    select '192.168.1.3','20061028140125','http://localhost/'  union all
    select '192.168.1.3','20061028140125','http://localhost/'  union all
    select '192.168.1.3','20061120140125','http://localhost/'  union all
    select '192.168.1.3','20061201140125','http://localhost/'  union all
    select '192.168.1.3','20061201140125','http://localhost/'  union all
    select '192.168.1.3','20061201140125','http://localhost/'  union all
    select '192.168.1.3','20061201140125','http://localhost/'  
    select 
    [每年]=(select top 1 count(*) as total from t group by left([time],8) order by total desc),
    [每周]=(select top 1 count(*) as total from t group by datepart(week,left([time],8)) order by total desc),
    [每月]=(select top 1 count(*) as total from t group by left([time],6) order by total desc),
    [当月]=(select count(*)  from t where datepart(week,left([time],8))=datepart(week,getdate())),
    [当周]=(select count(*)  from t where datepart(week,left([time],8))=datepart(week,getdate())-1)
      

  3.   

    我自己做出来了。month:
    SELECT     STR(YEAR(DateTime))+STR(MONTH(DateTime)) AS date, MIN(DateTime) AS MinDateTime, MAX(DateTime) AS MaxDateTime, SUM(Volume) 
                          AS Volume, MAX(HighPrice) AS highprice, MIN(LowPrice) AS lowprice
    FROM         FinancialNEW
    GROUP BY STR(YEAR(DateTime)) + STR(MONTH(DateTime))
    ORDER BY dateweek:
    SELECT     STR(YEAR(DateTime)) + STR(DATEPART(wk, DateTime)) AS date, MIN(DateTime) AS MinDateTime, MAX(DateTime) AS MaxDateTime, SUM(Volume) 
                          AS Volume, MAX(HighPrice) AS highprice, MIN(LowPrice) AS lowprice
    FROM         FinancialNEW
    GROUP BY STR(YEAR(DateTime)) + STR(DATEPART(wk, DateTime))
    ORDER BY date
      

  4.   

    顶了。最近很少来CSDN转悠了。。
      

  5.   

    用EXCEL的数据透视表更简单.呵呵