转轴不止一列

时间:2016-02-11 11:21:18

标签: c# sql sql-server excel sql-server-2008

我在SQL Server 2008中有两个表,其中一个表示每周需要的文章,另一个表示我有股票,有些像

需要:

Article                      Week                           Need

--------------------------------------------------------------------

aa                            1                                25
aa                            2                                13
aa                            4                                33
aa                            6                                21
aa                            25                               40
ab                            1                                 1
ab                            2                                 3
ab                            16                               14
ab                            50                               50

股票

  Article                       Units
---------------------------------------------------------------------------
    aa                            80    
    ab                            14
    ac                             8                

我需要展示类似的内容:

 Article       WEEK1          WEEK2       WEEK4         WEEK6        WEEK16       WEEK25   
            needs stock   needs stock  needs stock   needs stock   needs stock  needs stock


 aa           25    80      13   55      42    22      21    20      0   -1        40  -1
 ab            1    14       3   13       0    10        0   10     14  10          0  -4   

这将在Encel中显示,因为我使用C#和T-SQL,我的问题是如果使用PIVOT我可以用每周的需求来做它但我期望需求和股票,如果我没有我可以获得本周的库存文章,保存对象中的数据并每周计算库存,然后格式化Excel,但我在对象中需要一个问题:

puclic class needsPerWeek
{
string Article{get;set;}
int needsWeek1{get;set;}
int stockWeek1{get;set;}
int needsWeek2{get;set;}
int stockWeek2{get;set;}
int needsWeek{get;set;}
int stockWeek4{get;set;}
int needsWeek6{get;set;}
int stockWeek6{get;set;}
int needsWeek16{get;set;}
int stockWeek16{get;set;}
int needsWeek25{get;set;}
int stockWeek25{get;set;}
int needsWeek50{get;set;}
int stockWeek50{get;set;}
....
....
}

但我不知道我会花多少时间来回忆。

那么,我可以使用pivot吗?或其他任何方式来获取它?

1 个答案:

答案 0 :(得分:1)

只需复制并粘贴以下代码,然后根据需要进行更改。我们只是制作动态列字符串(UnitsWeek1,UnitsWeek2,NeedsWeek1 ......等)。并使用Pivot获取两个表,并通过加入两个表来获得最终表。

DECLARE @cols1 AS NVARCHAR(MAX),
   @query1  AS NVARCHAR(MAX),
   @cols2 AS NVARCHAR(MAX),
   @query2  AS NVARCHAR(MAX);

select @cols1 = STUFF((SELECT distinct ',' + QUOTENAME('NeedsWeek'+Week) 
                   from NeedsMaster FOR XML PATH(''), TYPE
           ).value('.', 'NVARCHAR(MAX)') 
       ,1,1,'')

set @query1 = 'SELECT Article, ' + @cols1 + ' from 
            (
                select Article
                       , Need
                       ,''NeedsWeek''+Week as Week 
                       from
                   NeedsMaster 
           ) x
           pivot 
           (
               min(need)
               for Week in (' + @cols1 + ')
           ) p '

select @cols2 = STUFF((SELECT distinct ',' + QUOTENAME('UnitsWeek'+n.Week) 
                   from NeedsMaster n left join StocksMaster s
on s.Article=n.Article
           FOR XML PATH(''), TYPE
           ).value('.', 'NVARCHAR(MAX)') 
       ,1,1,'')

set @query2 = 'Select * from (SELECT Article, ' + @cols2 + ' from 
            (
                select n.Article
                       , s.Units
                       ,''UnitsWeek''+n.Week as Week

                   from StocksMaster s
                   inner join NeedsMaster n
                      on s.Article=n.Article
           ) x1
           pivot 
           (
               min(Units)
               for Week in (' + @cols2 + ')
           ) p1 ) a inner join 
           (SELECT Article, ' + @cols1 + ' from 
            (
                select Article

                       , Need
                      ,''NeedsWeek''+Week as Week 
                       from
                   NeedsMaster 
           ) x2
           pivot 
           (
               min(need)
               for Week in (' + @cols1 + ')
           ) p2 ) b on a.Article=b.Article'

execute(@query2)

(@ query1只是为了更好地理解目的)。 试一试,我希望你能根据自己的要求进行编辑。

Reference