本帖最后由 hnsongbiao 于 2011-05-16 17:12:15 编辑

解决方案 »

  1.   

    這樣?use Tempdb
    go
    --> --> 
     
    if not object_id(N'T_ProInfo') is null
    drop table T_ProInfo
    Go
    Create table T_ProInfo([ProId] int,[Proname] nvarchar(7),[Prounit] nvarchar(1),[Proprice] MONEY)
    Insert T_ProInfo
    select 1,N'联想笔记本V7',N'台','3000' union all
    select 2,N'戴尔服务器V9',N'台','8000'
    Go
    use Tempdb
    go
    --> --> 
     
    if not object_id(N'T_Stock') is null
    drop table T_Stock
    Go
    Create table T_Stock([StockId] int,[StockName] nvarchar(4))
    Insert T_Stock
    select 1,N'主仓库' union all
    select 2,N'店面' union all
    select 3,N'退货库' union all
    select 4,N'其它仓库'
    Go
    use Tempdb
    go
    --> --> 
     
    if not object_id(N'T_INIProInStock') is null
    drop table T_INIProInStock
    Go
    Create table T_INIProInStock([ProId] int,[StockId] int,[Qty] int)
    Insert T_INIProInStock
    select 1,1,30 union all
    select 1,2,40 union all
    select 1,3,6 union all
    select 2,1,60 union all
    select 2,2,10 union all
    select 2,3,4
    Go
    use Tempdb
    go
    --> --> 
     
    if not object_id(N'T_ProInStock') is null
    drop table T_ProInStock
    Go
    Create table T_ProInStock([ProId] int,[StockId] int,[Qty] int)
    Insert T_ProInStock
    select 1,1,100 union all
    select 1,2,90 union all
    select 1,3,20 union all
    select 2,1,200 union all
    select 2,2,60 union all
    select 2,3,31
    Go
    DECLARE @s NVARCHAR(4000)
    SET @s=''SELECT @s=@s+','+QUOTENAME(N'期初'+[StockName]+QUOTENAME([StockId]))+'=sum(case when a.[StockId]='+RTRIM([StockId])+' then a.[Qty] else 0 end),' 
    +QUOTENAME(N'当前'+[StockName]+QUOTENAME([StockId]))+'=sum(case when a.[StockId]='+RTRIM([StockId])+' then c.[Qty] else 0 end)' 
    FROM T_Stock ORDER BY [StockId]EXEC('Select b.[Proname],b.[Prounit],b.[Proprice]'+@s+'
    from T_ProInStock AS a
    INNER JOIN T_ProInfo AS b ON a.[ProId]=b.[ProId]
    left join T_INIProInStock as c on c.ProId=b.ProId and c.StockId=a.StockId
    GROUP BY b.[Proname],b.[Prounit],b.[Proprice]')/*
    Proname Prounit Proprice 期初主仓库[1] 当前主仓库[1] 期初店面[2] 当前店面[2] 期初退货库[3] 当前退货库[3] 期初其它仓库[4] 当前其它仓库[4]
    戴尔服务器V9 台 8000.00 200 60 60 10 31 4 0 0
    联想笔记本V7 台 3000.00 100 30 90 40 20 6 0 0
    */
      

  2.   


    create table #T_ProInfo
    (ProId int, Proname nvarchar(50), Prounit nvarchar(10), Proprice int)
    insert #T_ProInfo
    select 1, '联想笔记本V7', '台', 3000 union all
    select 2, '戴尔服务器V9', '台', 8000create table #T_Stock
    (StockId int, StockName nvarchar(50))
    insert #T_Stock
    select 1 ,'主仓库' union all
    select 2 , '店面' union all
    select 3 , '退货库' union all
    select 4 ,'内库'create table #T_INIProInStock
    (ProId int, StockId int, Qty int)
    insert #T_INIProInStock
    select 1, 1, 30 union all
    select 1, 2, 40 union all
    select 1, 3, 6 union all
    select 1, 4, 50 union all
    select 2, 1, 60 union all
    select 2, 2, 10 union all
    select 2, 3, 4 union all
    select 2, 4, 57create table #T_ProInStock
    (ProId int, StockId int, Qty int)
    insert #T_ProInStock
    select 1, 1, 100 union all
    select 1, 2, 90 union all
    select 1, 3, 20 union all
    select 1, 4, 76 union all
    select 2, 1, 200 union all
    select 2, 2, 60 union all
    select 2, 3, 31 union all
    select 2, 4, 48declare @sql as nvarchar(max)
    declare @sql2 as nvarchar(max)
    set @sql=''
    set @sql2=''
    select @sql=@sql+',sum(case ip.StockId when '+ltrim(StockId)+' then isnull(ip.Qty,0) else 0 end) as ''(初期)'+StockName+'''',
           @sql2=@sql2+',sum(case p.StockId when '+ltrim(StockId)+' then isnull(p.Qty,0) else 0 end) as ''(当前)'+StockName+''''
           from #T_Stock
    set @sql='select isnull(t.Proname,'''') as Proname,isnull(t.Prounit,'''') as Prounit,isnull(t.Proprice,0) as Proprice'
              +@sql+@sql2+' from #T_INIProInStock as ip left join #T_ProInStock as p '+
             ' on ip.ProId=p.ProId and ip.StockId=p.StockId Left join #T_ProInfo as t on t.ProId=p.ProId group by p.ProId,t.Proname,t.Prounit,t.Proprice'
    exec(@sql)