需要Sql Query喜欢这种格式

时间:2014-02-20 04:50:58

标签: sql sql-server-2008

这是我的查询

SELECT  *,'WESTRAC - T120' AS [Dealer Code] FROM [CPO by wesTrac by Customers] WHERE [CPO by wesTrac by Customers].[Dealer Breakdown] LIKE 'T080 - N%' 

运行此我就像这样

Dealer  Customer    Dealer Breakdown    Type    2012/Jan    2012/Feb 2012/Mar 2012/Apr  2012/May    2012/Jun    2012/Jul    2012/Aug    2012/Sep    2012/Oct    2012/Nov    2012/Dec

WESTRAC - T080  A.P.Delaney & Co    T080 - N165580 - apdelaney  PS WEB  0   189 19  0   20  78  0   0   92  0   0   0

但我需要这样的输出:

Dealer         TYPE     DEALER BREAKDOWN           DEALERCODE       DATE       Oniline Sales
WESTRAC-T080  PSWEB      T080-N165580-apdelaney    WESTRAC-T120     2012/Jan     0
WESTRAC-T080  PSWEB      T080-N165580-apdelaney    WESTRAC-T120     2012/Feb     189
WESTRAC-T080  PSWEB      T080-N165580-apdelaney    WESTRAC-T120     2012/Mar     19
---
---
---
WESTRAC-T080  PSWEB      T080-N165580-apdelaney    WESTRAC-T120     2012/Dec     0

1 个答案:

答案 0 :(得分:0)

根据您的评论,听起来您正试图unpivot您的结果。这是一个精简版本(您需要指定所有列):

SELECT dealer, 
       type, 
       dealerbreakdown, 
       'WESTRAC - T120' dealercode, 
       dealerdate, 
       onlinesales 
FROM   dealerbreakdown 
       UNPIVOT ( onlinesales 
               FOR dealerdate IN ([2012/Jan], 
                                  [2012/Feb], 
                                  [2012/Mar]) ) u