无法显示枢轴结果

时间:2017-01-11 14:00:33

标签: sql-server pivot

我在旋转结果中得到错误,如下所示:

select tanggal,[1],[2] from
(
SELECT
    (CONVERT(DATE, tanggal, 103)) as tanggal,
        id_jenis,
     (harga * jumlah) as total
FROM
    [dbo].[PNL_TP_SISA_PRODUKSI]
WHERE
    YEAR (CONVERT(DATE, tanggal)) = 2016
AND MONTH (CONVERT(DATE, tanggal)) = 8
AND id_unit_pengolah = 40) 
c
PIVOT (MAX(total) FOR id_jenis IN([1],[2]))

并且此错误显示:

  

[Err] 42000 - [SQL Server]')'附近的语法不正确。

因为错误

而感到困惑

1 个答案:

答案 0 :(得分:1)

我认为您需要为pivot

提供别名
 select tanggal,[1],[2] from
    (
    SELECT
        (CONVERT(DATE, tanggal, 103)) as tanggal,
            id_jenis,
         (harga * jumlah) as total
    FROM
        [dbo].[PNL_TP_SISA_PRODUKSI]
    WHERE
        YEAR (CONVERT(DATE, tanggal)) = 2016
    AND MONTH (CONVERT(DATE, tanggal)) = 8
    AND id_unit_pengolah = 40) 
    c
    PIVOT (MAX(total) FOR id_jenis IN([1],[2])) as pvt