求一SQL语句(Access)有一个书店销量统计表,共需统计四种销量(小说类销量,考试类销量,其他销量,总销量),已经有如下的一个表  书店名      销量类型      销量数    日期   
大众书店       总销量        800        2009-3-21
大众书店       考试类销量     500       2009-3-21
大众书店       其他销量       300       2009-3-21
新华书店       总销量        1800       2009-3-21
新华书店       小说类销量     800       2009-3-21
新华书店       考试类销量     500       2009-3-21
新华书店       其他销量       500       2009-3-21
...........................................
要求用一个sql语句得到如下形式的表(统计每一天,每个书店的小说类销量,考试类销量,
其他销量,总销量。如没有销量则为0)
书店名     小说类销量  考试类销量   其他销量  总销量    日期   
大众书店      0           500 300 800 2009-3-21
新华书店      800         500 500 1800 2009-3-21
谢谢大家了!!

解决方案 »

  1.   


    Select 書店名,日期,
        sum(case when 銷售類型='小說類銷量' then 銷售數 else 0 end ) as  小说类销量,
        sum(case when 銷售類型='考试类销量then 銷售數 else 0 end ) as  考试类销量,
        sum(case when 銷售類型='其他销量' then 銷售數 else 0 end ) as  其他销量 ,
        sum(case when 銷售類型='总销量' then 銷售數 else 0 end ) as  总销量 
    from tb
    group by 書店名,日期
      

  2.   

    上面的少了個分號.Select 書店名,日期,
        sum(case when 銷售類型='小說類銷量' then 銷售數 else 0 end ) as  小说类销量,
        sum(case when 銷售類型='考试类销量'then 銷售數 else 0 end ) as  考试类销量,
        sum(case when 銷售類型='其他销量' then 銷售數 else 0 end ) as  其他销量 ,
        sum(case when 銷售類型='总销量' then 銷售數 else 0 end ) as  总销量 
    from tb
    group by 書店名,日期
      

  3.   

    就三种类型的话,用楼上的,
    如果销量类型是不定的话,那用动态SQL了
      

  4.   

    樓主是Access數據庫呀.
    Select 書店名,日期,
        sum(iif( 銷售類型='小說類銷量' ,銷售數 ,0 ) as  小说类销量,
        sum(iif(銷售類型='考试类销量'), 銷售數 , 0 ) as  考试类销量,
        sum(iif銷售類型='其他销量' , 銷售數 ,0  ) as  其他销量 ,
        sum(iif(銷售類型='总销量' ), 銷售數 , 0 ) as  总销量 
    from tb
    group by 書店名,日期
      

  5.   

    declare @s varchar(8000)
    set @s='Select 书店名,日期 '
    select @s=@s+',sum(case when 销售类型 ='''+销售类型+''' then 销售数 else 0 end) ['+销售类型+']'
    from (select distinct 销售类型 from tb) t
    select @s=@s+' group by 书店名,日期'
    动态的
      

  6.   

    declare @s varchar(8000) 
    set @s='Select 书店名,日期 ' 
    select @s=@s+',sum(case when 销售类型 ='''+销售类型+''' then 销售数 else 0 end) ['+销售类型+']' 
    from (select distinct 销售类型 from tb) t 
    select @s=@s+' group by 书店名,日期'
      

  7.   

    create table #temp
    (
      书店名 nchar(10),
      销量类型 nchar(10),
      销量数  int,
      日期 datetime 
    )
    insert into #temp values('大众书店' ,    '总销量',        800 ,       '2009-3-21')
    insert into #temp values('大众书店' ,     '考试类销量',    500 ,     '2009-3-21')
    insert into #temp values('大众书店' ,     '其他销量' ,     300 ,     '2009-3-21')
    insert into #temp values('新华书店',      '总销量',        1800 ,     '2009-3-21')
    insert into #temp values('新华书店',      '小说类销量',    800 ,     '2009-3-21')
    insert into #temp values('新华书店',      '考试类销量',    500 ,     '2009-3-21')
    insert into #temp values('新华书店',      '其他销量',      500 ,     '2009-3-21')
    SET LANGUAGE N'简体中文'
    select 书店名,sum(case 销量类型 when '小说类销量' then 销量数 else 0 end) as 小说类销量,
                  sum(case 销量类型 when '考试类销量' then 销量数 else 0 end) as 考试类销量,
                  sum(case 销量类型 when '总销量' then 销量数 else 0 end) as 总销量,
                  substring(cast(max(日期) as nvarchar(20)),1,10) as 日期            
    from #temp
    group by 书店名
    drop table #temp----------------------result----------------
    大众书店       0 500 800 03 21 2009
    新华书店       800 500 1800 03 21 2009
      

  8.   

    create table booksale 
    (bookshop varchar(400),booktype varchar(400), amount int, data datetime)
    insert into booksale 
    values 
    ('大众书店','总销量',800,2009-3-21)
    insert into booksale
    values
    ('大众书店','考试类销量',500,2009-3-21) 
    insert into booksale
    values
    ('大众书店','其他销量',300,2009-3-21) 
    insert into booksale
    values
    ('新华书店','总销量',1800,2009-3-21) 
    insert into booksale
    values
    ('新华书店','小说类销量',800,2009-3-21) 
    insert into booksale
    values
    ('新华书店','考试类销量',500,2009-3-21) 
    insert into booksale
    values
    ('新华书店','其他销量',500,2009-3-21) select bookshop,
    sum(case when booktype='小说类销量' then amount else 0 end) as 小说类销量,
    sum(case when booktype='考试类销量' then amount else 0 end) as 考试类销量,
    sum(case when booktype='其他销量' then amount else 0 end) as 其他类销量,
    sum(case when booktype='总销量' then amount else 0 end) as 总类销量,
    convert(data,) from booksale 
    group by data,bookshop
      

  9.   

    datatime 格式没弄没好 不好意思
      

  10.   


      if object_id('booksale') is not null 
      drop table booksalecreate table booksale 
    (bookshop varchar(400),booktype varchar(400), amount int, data datetime) 
    insert into booksale 
    values 
    ('大众书店','总销量',800,'2009-3-21') 
    insert into booksale 
    values 
    ('大众书店','考试类销量',500,'2009-3-21') 
    insert into booksale 
    values 
    ('大众书店','其他销量',300,'2009-3-21') 
    insert into booksale 
    values 
    ('新华书店','总销量',1800,'2009-3-21') 
    insert into booksale 
    values 
    ('新华书店','小说类销量',800,'2009-3-21') 
    insert into booksale 
    values 
    ('新华书店','考试类销量',500,'2009-3-21') 
    insert into booksale 
    values 
    ('新华书店','其他销量',500,'2009-3-21') select bookshop, 
    sum(case when booktype='小说类销量' then amount else 0 end) as [小说类销量], 
    sum(case when booktype='考试类销量' then amount else 0 end) as [考试类销量], 
    sum(case when booktype='其他销量' then amount else 0 end) as [其他类销量], 
    sum(case when booktype='总销量' then amount else 0 end) as [总类销量], 
    convert(varchar(10),data,120) as [日期]
    from booksale 
    group by data,bookshopdrop table booksale
      

  11.   


    有一个书店销量统计表,共需统计四种销量(小说类销量,考试类销量,其他销量,总销量) ,已经有如下的一个表   书店名      销量类型      销量数    日期  
    大众书店      总销量        800        2009-3-21 
    大众书店      考试类销量    500      2009-3-21 
    大众书店      其他销量      300      2009-3-21 
    新华书店      总销量        1800      2009-3-21 
    新华书店      小说类销量    800      2009-3-21 
    新华书店      考试类销量    500      2009-3-21 
    新华书店      其他销量      500      2009-3-21 
    ........................................... 
    要求用一个sql语句得到如下形式的表(统计每一天,每个书店的小说类销量,考试类销量, 
    其他销量,总销量。如没有销量则为0) 
    书店名    小说类销量  考试类销量  其他销量  总销量    日期  
    大众书店      0          500 300 800 2009-3-21 
    新华书店      800        500 500 1800 2009-3-21 select 书店名,
          小说类销量=sum(case when 销量类型='小说类销量' then 销量数 else 0 end),
          考试类销量=sum(case when 销量类型='考试类销量' then 销量数 else 0 end),
          其他销量=sum(case when 销量类型='其他销量' then 销量数 else 0 end),
         总销量=sum(case when 销量类型='总销量' then 销量数 else 0 end),
        日期 from table group by 书店名, 日期
      

  12.   

    4楼这个sql的括号我改了改就可以了,不过还是有一个问题,比如这个书店2009-3-21什么书也没有卖出去,但它在2009-3-22
    卖了很多书,也应该在2009-3-21日中统计它的数据,便于对比。显示  xx书店 0 0 0 0 2009-3-21 这样的话该怎么写呢?