Sql server 使用嵌入式sql语句的透视表

Sql server 使用嵌入式sql语句的透视表,sql-server,Sql Server,我创建了一个select查询,它将我的分类账数据拉入并将其分组为表格格式 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

我创建了一个select查询,它将我的分类账数据拉入并将其分组为表格格式

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   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语句中收到错误消息。

尝试在in条件中的值周围放置方括号:

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

要隐藏空值,请使用:

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

ORDER BY CASE WHEN PL_Line = 30 THEN 1 ELSE 2 END, PL_Line
SELECT*错误,您应该在外部SELECT中指定列,并且数字列应该在方括号中:

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

谢谢你修复了这个错误,我找到了一些其他的结果。所以,现在的问题是,我可以:1-指定第一列的顺序,PL_行,它在完整数据中从2-30开始,我希望它显示为30,2-29 2-如何隐藏空值?是0还是空白?谢谢您的更新。我试图将它们添加到查询中,但我不确定将ORDERBY语句放在哪里。我要么把它放错了位置,要么语法有问题,我总是在=(的旁边)出现错误。我会在第一个select语句中放入我想称之为头的内容,而不是4吗?如选择PL_线,[description]、[4]作为“2016年4月”、[5]作为“2016年5月”。。。但仍然引用了[4],[5]。。。在枢轴部分?
ORDER BY CASE WHEN PL_Line = 30 THEN 1 ELSE 2 END, PL_Line
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