使用嵌入式sql语句的数据透视表

时间:2015-09-25 16:32:15

标签: sql-server

我创建了一个选择查询,它可以提取我的分类帐数据并将其分组为表格格式。

select 
    chart.Uf_Mgr_PL_Line,
    MONTH(ledger.trans_date) as trans_month,
    chart.description,
    sum(ledger.for_amount)
From 
    ledger
        inner join chart on ledger.acct=chart.acct
Where
    ledger.control_year = '2016' and chart.Uf_Mgr_PL_Line <> 0
Group by
    Uf_Mgr_PL_Line,
    MONTH(ledger.trans_date),
    chart.description
Order by 
    Uf_Mgr_PL_Line  

这是数据:

PL_LINE trans_month description AMOUNT  
2       9             COGS        100  
2         7    COGS -50  
2         4 COGS    -25  
3         9 Sales Returns - Other   200  
3         6 Sales Returns - Other   155  
3         5 Sales Returns - Other   30  
4         7 Sales Discounts 32  
4         4 Sales Discounts 400  
4         8 Sales Discounts 15  
4         5 Sales Discounts 35  
5         5 Price Protection Allowance  410  
5         6 Price Protection Allowance  22  
5         9 Price Protection Allowance  32  
5         4 Price Protection Allowance  44  
7         9 COGS - RMA Processing   -5  
7        8  COGS - RMA Processing   78  
7        5  COGS - RMA Processing   2555  

所以我需要调整这些数据,使它看起来像这样

    PL_Line   desc               4   5  6   7  8   9  10  11  12  1  2  3
      2        Cogs             -25        -50    100
      3     sales return-other      30  155       200
      4     sales disc          400 35      32  15

我一直在尝试进行数据透视查询,但我认为我的问题在于列标题部分

    select *
           From 
           (    select 
                   chart.Uf_Mgr_PL_Line,
                   MONTH(ledger.trans_date) as [trans_month],
                   chart.description,
                    ledger.for_amount
                 From 
                    ledger
                       inner join chart on ledger.acct=chart.acct
                 Where
                       ledger.control_year = '2016' and chart.Uf_Mgr_PL_Line <> 0
             )AS s
             Pivot
            (   sum(amount)
            for [trans_month] in (4,5,6,7,8,9,10,11,12,1,2,3)
            )as pivot

部分问题是我在in条件中的for语句中收到错误消息。

2 个答案:

答案 0 :(得分:1)

尝试在IN条件的值中加上方括号:

for [trans_month] in ([4],[5],[6],[7],[8],[9],[10],[11],[12],[1],[2],[3])

编辑:

要“隐藏NULLS”,请使用:

SELECT PL_Line,[description],ISNULL([4],0) AS [4],ISNULL([5], 0) AS [5], ...

要进行特殊排序,请使用:

ORDER BY CASE WHEN PL_Line = 30 THEN 1 ELSE 2 END, PL_Line

答案 1 :(得分:1)

SELECT *错误,您应该在外部select和number列中指定列应该在方括号中:

DECLARE @myTable TABLE(
        PL_LINE INT, trans_month INT, [description] VARCHAR(30), AMOUNT INT
)
INSERT INTO @myTable
VALUES
(2, 9, 'COGS', 100),
(2, 7, 'COGS', -50),
(2, 4, 'COGS', -25),
(3, 9, 'Sales Returns - Other', 200),
(3, 6, 'Sales Returns - Other', 155),
(3, 5, 'Sales Returns - Other', 30),
(4, 7, 'Sales Discounts', 32),
(4, 4, 'Sales Discounts', 400),
(4, 8, 'Sales Discounts', 15),
(4, 5, 'Sales Discounts', 35),
(5, 5, 'Price Protection Allowance', 410),
(5, 6, 'Price Protection Allowance', 22),
(5, 9, 'Price Protection Allowance', 32),
(5, 4, 'Price Protection Allowance', 44),
(7, 9, 'COGS - RMA Processing', -5),
(7, 8, 'COGS - RMA Processing', 78),
(7, 5, 'COGS - RMA Processing', 2555)

    SELECT PL_Line,[description],[4],[5],[6],[7],[8],[9],[10],[11],[12],[1],[2],[3] FROM
    (
    SELECT * FROM @myTable
    ) AS t
    PIVOT
    (
        SUM(AMOUNT)
        FOR [trans_month] IN ([4],[5],[6],[7],[8],[9],[10],[11],[12],[1],[2],[3])
    ) AS pvt