现有一个表数据如下:分店名称             日期                           数量
大隗门市部            2005-12-18 00:00:00.000  11
大隗门市部            2005-12-19 00:00:00.000  12
大隗门市部            2005-12-20 00:00:00.000  16
牛庄门市部            2005-12-18 00:00:00.000  26
牛庄门市部            2005-12-19 00:00:00.000  21
牛庄门市部            2005-12-20 00:00:00.000  22

第n个门市部(数量不知道)想用一SQL语句实现如下表格:日期           合计        大隗门市部        牛庄门市部     
2005-12-18     37             11                26 
2005-12-19     33             12                21
2005-12-20     38             16                22想了半天不知该怎么做,分不多了,请兄弟们谅解一下,多帮帮忙吧!

解决方案 »

  1.   

    建议动态生成SQL。
    先select distinct 分店名称 from tb
    再利用分店名称合成sql
    select 日期,sum(decode(分店名称,'大隗门市部'))+sum(decode(分店名称,'牛庄门市部))…… as 合计,sum(decode(分店名称,'大隗门市部')) as 大隗门市部,sum(decode(分店名称,'牛庄门市部')) as 牛庄门市部 ……
    group by ... 
      

  2.   

    给个以前写的存储过程,你参考下,估计可以解决问题:
    CREATE PROCEDURE [dbo].[Sp_JB_GL_ItemBanlance_AR] 
    ASDeclare @ncurrYear Int
    Declare @ncurrMon Int
    Declare @k Int
    Declare @J Int
    Declare @Cr nVarChar(80)
    Declare @Dr nVarChar(80)
    Declare @SQL nVarChar(4000)
    Declare @SQL_a nVarChar(200)
    Declare @myStr  nVarChar(2000)Set NoCount onSelect @ncurrYear = ncurryear From sys_subsysinstalled where csubsyscode = 'GL'
    Select @ncurrMon = ncurrmon From sys_subsysinstalled where csubsyscode = 'GL'CREATE TABLE #temp_a ([客户代码] nVarChar(20) NOT NULL, [客户名称] nVarChar(100) NOT NULL)
    Set @myStr = '[客户代码], [客户名称]'IF @ncurrMon >= 1
    Begin
    Set @k = 1
    While @k <= @ncurrMon
    Begin
    Set @Cr =  Convert(Char(2), @k)+'月贷方'
    Set @myStr = @myStr + ', IsNull(Sum([' + @Cr + ']), 0) as [' + @Cr + ']'
    Set @SQL = 'ALTER TABLE #temp_a ADD [' + @Cr + '] numeric(20,2) Not NULL Default 0'
    exec  sp_executesql @SQL--, N'@Cr nVarChar(80) out', @Cr out Set @Dr =  Convert(Char(2), @k)+'月借方'
    Set @myStr = @myStr + ', IsNull(Sum([' + @Dr + ']), 0) as [' + @Dr + ']'
    Set @SQL = 'ALTER TABLE #temp_a ADD [' + @Dr + '] numeric(20,2) Not NULL Default 0'
    exec  sp_executesql @SQL--, N'@Dr nVarChar(80) out', @Dr out

    Set @SQL_a = ' '
    Set @SQL = 'Insert Into #temp_a Select b.cCusCode, b.cCusName '
    Set @J = @k
    While @J -1 >0
    Begin
    Set @SQL_a = @SQL_a + ' , 0, 0 '
    Set @J = @J - 1
    End
    Set @SQL = @SQL + @SQL_a
    Set @SQL = @SQL + ', Sum(a.nCrAmt) , Sum(a.nDrAmt) '
    -- IF @k = @ncurrMon
    Set @SQL = @SQL + ' From GL_ItemBanlance a, AR_Customer b '
    -- Else
    -- Set @SQL = @SQL + ' From GL_ItemBanlance_H a, AR_Customer b '
     Set @SQL = @SQL + ' Where a.nCusId = b.id '
     Set @SQL = @SQL + ' and a.nCusId <> 0 and (a.nCrAmt <> 0 or a.nDrAmt <> 0) '
     Set @SQL = @SQL + ' and a.nAccY = ' + Str(@ncurrYear) + ' and a.nAccM = ' + Str(@k) 
     Set @SQL = @SQL + ' and a.nAccId in (Select Id From GL_Account Where Code like ''1131%'') '
     Set @SQL = @SQL + ' Group by   b.cCusCode, b.cCusName '
    Exec sp_executesql @SQL Set @k = @k + 1
    End
    End--余额
    ALTER TABLE #temp_a ADD [本月余额] numeric(20,2) Not NULL Default 0
    Set @myStr = @myStr + ', IsNull(Sum([本月余额]), 0) as [本月余额]'Set @SQL = 'Insert Into #temp_a Select b.cCusCode, b.cCusName '
    Set @SQL = @SQL + @SQL_a + ', 0, 0,    Sum(a.nCloseBalan) From GL_ItemBanlance a, AR_Customer b '
    Set @SQL = @SQL + ' Where a.nCusId = b.id and a.nCusId <> 0 and  a.nCloseBalan <> 0 '
    Set @SQL = @SQL + ' and a.nAccY = ' + Str(@ncurrYear) + ' and a.nAccM = ' + Str(@ncurrMon)
    Set @SQL = @SQL + '  and a.nAccId in (Select Id From GL_Account Where Code like ''1131%'')'
    Set @SQL = @SQL + ' Group by   b.cCusCode, b.cCusName'
    Exec sp_executesql @SQLSet @SQL = 'Select ' + @myStr + ' From #temp_a Group by  [客户代码], [客户名称]'
    Exec sp_executesql @SQL
    GO
      

  3.   

    上面的sp是根据当前月份动态生成字段个数的,你可以根据
    select distinct 分店名称 from 表
    来动态生成字段个数,然后做统计就很简单了吧呵呵
      

  4.   

    --建立测试环境
    --目标
    /*
    日期           合计        大隗门市部        牛庄门市部    
    2005-12-18     37             11                26 
    2005-12-19     33             12                21
    2005-12-20     38             16                22
    */
    Create Table 表(分店名称 varchar(10),日期 varchar(50),数量 varchar(10))
    --插入数据
    insert into 表
    select '大隗门市部','2005-12-18 00:00:00.000','11' union
    select '大隗门市部','2005-12-19 00:00:00.000','12' union
    select '大隗门市部','2005-12-20 00:00:00.000','16' union
    select '牛庄门市部','2005-12-18 00:00:00.000','26' union
    select '牛庄门市部','2005-12-19 00:00:00.000','21' union
    select '牛庄门市部','2005-12-20 00:00:00.000','22'
    --select * from 表
    --测试语句
    select left(日期,10),合计=count(*),sum(case 分店名称 when '大隗门市部' then 1 else 0 end) [大隗门市部],
    sum(case 分店名称 when '牛庄门市部' then 1 else 0 end) [牛庄门市部]from 表
    group by left(日期,10)
     
     
    --删除测试环境
    Drop Table 表
      

  5.   

    利用游标很容易实现这个行专列。试试这个
    declare @str varchar(8000)
    declare @sql varchar(8000)
    delcare @a   varchar(20)
    delcare @b   varchar(20)
    delcare @c   varchar(20)
    delcare @d   float
    --生成表结构
    Create table #temp1(intID int identity(1,1))
    declare mycursor for select 分店名称 from tablename group by 分店名称
    open mycursor
    fecth next from mycursor into @a
    while @@fetch_status=0
    begin
       set @sql='alter table #temp1 add ['+@a+'] varchar(20) null'
       exec(@sql)
       fetch next from mycursor into @a
    end
    close mycursor
    deallocate mycursor
                               /*写入数据*/
    /*声明游标(行数)*/
    declare cur1 cursor for select distinct 分店名称,日期,sum(数量) from tablename group by 分店名称,日期
    /*打开游标*/
    open cur1
    /*读取数据*/
    fetch next from cur1 into @b,@c,@d
    while @@Fetch_status=0
    begin
    insert into #temp1 select 分店名称,日期,sum(数量) as 数量 from tablename group by 分店名称,日期 
    /*声明游标(列数)*/
      declare cur2 cursor for select 分店名称 from tablename group by 分店名称
      open cur2
      fetch next from cur2 into @a
      while @@fetch_status=0
        begin
        set @sql='update #temp1 set ['+@a+']=(select MpspFact from
    Mpsp where 分店名称='''+@b+'''and 日期 ='''
    +@c+'''and 数量='''+@d+''') 
        print(@sql)
        exec(@sql)
        fetch next from cur2 into @a
        end
    /*关闭游标cur2*/
    close cur2
    /*释放游标cur1*/
    deallocate cur2
    fetch next from cur1 into @b,@c,@d
    end
    /*关闭游标cur1*/
    close cur1
    /*释放游标cur1*/
    deallocate cur1
    select * from #temp1