简体   繁体   English

枢转多个栏

[英]Pivot more than one column

i have two tables in SQL Server 2008, in one i have the needs of articles per week, and in another i have the stock, somethin like 我在SQL Server 2008中有两个表,一个中有每周需要的文章,另一个中我有库存,有点像

NEEDS: 需求:

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

STOCKS 股票

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

and i need to show something like: 我需要显示类似的东西:

 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   

This will show in Encel, for this i use C# and T-SQL, my problem is that if use PIVOT i can oly do it with the needs per week but i expect the needs and the stock, if i do without the pivot i can obtain the article the week an the stock, save the data in objects and do the calculation of the stock per week and then format the Excel, but i have one problem i need in the object something like: 这将在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;}
....
....
}

but i don´t know how much weeks i will recibe. 但是我不知道我会收到多少周。

So, can i do this with pivot?, or any other way to obtain this? 那么,我可以使用数据透视表吗?还是可以通过其他任何方式来做到这一点?

Just copy and paste the code below and do some changes according to your need. 只需复制并粘贴下面的代码,然后根据需要进行一些更改。 We are just making dynamic column strings (UnitsWeek1, UnitsWeek2, NeedsWeek1... etc). 我们只是制作动态列字符串(UnitsWeek1,UnitsWeek2,NeedsWeek1 ...等)。 And get two tables using Pivot and obtain a final table by joining the two. 并使用Pivot获取两个表,并通过将两个表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 is just for better understanding purpose). (@ query1只是为了更好地理解目的)。 Give it a try and i hope you will edit it as your requirements. 试试看,我希望您可以根据需要进行编辑。

Reference 参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM