http://expert.csdn.net/Expert/topic/2102/2102788.xml?temp=.1526911

解决方案 »

  1.   

    declare @sql varchar(8000)
    set @sql = 'select day(日期) 号数'
    select @sql = @sql + ',sum(case 名称 when '''+名称+''' then 库存 else 0 end) ['+名称+'库存],sum(case 名称 when '''+名称+''' then 销售数量 else 0 end) ['+名称+'销售数量]'
      from (select distinct 名称 from 表A) as a
    select @sql = @sql+' from 表A group by day(日期)'exec(@sql)
    go
      

  2.   

    老问题--交叉表问题,已有n个人在m年或月前问过,也有人回答过k遍。其中m,m,k>>5。不过此处我想问一句:w1,w2,...,wn,当wi的数目很多时比如100时,若这样排出来的表人能看么?
      

  3.   

    select IDENTITY (int,1,1) as 序号,
    sum(case when (名称='WL1') then 库存 ) else 0 end as WL1KC ,
    sum(case when (名称='WL1') then  销售数量  else 0 end ) AS WL1XSSL,
    sum(case when (名称='WL2') then 库存 ) else 0 end as WL2KC ,
    sum(case when (名称='WL2') then  销售数量  else 0 end ) AS WL2XSSL
    FROM 表A
    如果你有多个物料,就是先用一个QUERY  (SELECT DISTINCT WL FROM 表A )
    然后对它进行循环就可以了。
    其实最后只是在DBGIRD上显示的问题了。
      

  4.   

    不好意思,上面的语句应该还加上group by 日期。
      

  5.   

    交叉表+动态生成SQL的
    declare @sql varchar(8000)
    set @sql='select day(日期) as 号数'
    select @sql=@sql
        +char(13)+',sum(case 名称 when '''+名称+''' then 库存 end) as ['+名称+'_库存]'
        +char(13)+',sum(case 名称 when '''+名称+''' then 销售数量 end) as ['+名称+'_销售数量]'
    from(select distinct 名称 from 表A) as a
    set @sql=@sql+char(13)+' from 表A group by day(日期)'exec(@sql)