temp为各个时间段的售书情况,
统计每天各类书的售书总量,比如'语文'在20101001日的总量为5;'数学'为7CREATE TABLE [dbo].[temp](
[id] [int] IDENTITY(1,1) NOT NULL,
[bookname] [varchar](50) COLLATE Chinese_PRC_CI_AS NULL,
[counts] [int] NULL,
[time] [datetime] NULL
) ON [PRIMARY]
go
insert into temp(bookname,counts,time)
select '语文',1,'2010-10-1 00:00:00'
union 
select '语文',1,'2010-10-1 01:00:00'
union 
select '语文',2,'2010-10-1 02:00:00'
union 
select '语文',1,'2010-10-1 03:00:00'
union 
select '语文',3,'2010-10-2 06:00:00'
union 
select '数学',2,'2010-10-1 07:00:00'
union 
select '数学',2,'2010-10-1 08:00:00'
union 
select '数学',3,'2010-10-1 09:00:00'
union 
select '数学',10,'2010-10-3 07:00:00'
union 
select '数学',5,'2010-10-3 08:00:00'
union 
select '英语',7,'2010-10-1 09:00:00'
union 
select '英语',7,'2010-10-8 10:00:00'

解决方案 »

  1.   


    select bookname,sum(counts),cast(time as date) as date
    from temp group by cast(time as date)
      

  2.   

    select bookname ,sum(isnull(count,0)) from temp   group by convert(time,char(10),120)
      

  3.   

    select convert(varchar,time,112)day, bookname, sum(counts) from temp group by convert(varchar,time,112), bookname
      

  4.   


    CREATE TABLE #tb(
        [id] [int] IDENTITY(1,1) NOT NULL,
        [bookname] [varchar](50) COLLATE Chinese_PRC_CI_AS NULL,
        [counts] [int] NULL,
        [time] [datetime] NULL
    ) ON [PRIMARY]
    go
    insert into #tb(bookname,counts,time)
    select '语文',1,'2010-10-1 00:00:00'
    union 
    select '语文',1,'2010-10-1 01:00:00'
    union 
    select '语文',2,'2010-10-1 02:00:00'
    union 
    select '语文',1,'2010-10-1 03:00:00'
    union 
    select '语文',3,'2010-10-2 06:00:00'
    union 
    select '数学',2,'2010-10-1 07:00:00'
    union 
    select '数学',2,'2010-10-1 08:00:00'
    union 
    select '数学',3,'2010-10-1 09:00:00'
    union 
    select '数学',10,'2010-10-3 07:00:00'
    union 
    select '数学',5,'2010-10-3 08:00:00'
    union 
    select '英语',7,'2010-10-1 09:00:00'
    union 
    select '英语',7,'2010-10-8 10:00:00'select cast(time as date) as date,bookname,sum(counts)
    from #tb group by cast(time as date),bookname
      

  5.   

    消息 243,级别 16,状态 1,第 1 行
    类型 date 不是已定义的系统类型。
    消息 243,级别 16,状态 1,第 1 行
    类型 date 不是已定义的系统类型。
      

  6.   

    消息 243,级别 16,状态 1,第 1 行
    类型 time 不是已定义的系统类型。
    消息 207,级别 16,状态 1,第 1 行
    列名 'count' 无效。
      

  7.   

    +1,不过convert(varchar,time,112) 中112是什么意思,返回时间字符串的格式还是什么
      

  8.   

    http://msdn.microsoft.com/zh-cn/library/ms187928%28v=SQL.100%29.aspx
      

  9.   

    - -!!我是08的,你不支持date
    下面就行了
    select CONVERT(char(10),time,20) as date,bookname,sum(counts)
    from #tb group by CONVERT(char(10),time,20),bookname