我有一个数据透视查询,似乎无法找到错误发生的位置。它对我来说很好看。我试图按交易日期的月份来调整我的SUM(金额)。交易日期的格式为2017-01-01。我觉得错误就在这个领域。
SELECT *
FROM(SELECT MONTH(dbo.bill_t_ARTransaction.TransactionDate) AS DATEMONTH, dbo.bill_c_ChargeZone.ChargeZoneCode, dbo.bill_t_ARTransaction.Amount
FROM dbo.bill_c_TransactionType
INNER JOIN dbo.bill_c_TransactionTypeGroup ON (dbo.bill_c_TransactionType.TransactionTypeGroupCode = dbo.bill_c_TransactionTypeGroup.TransactionTypeGroupCode)
INNER JOIN dbo.bill_t_ARTransaction ON (dbo.bill_c_TransactionType.TransactionTypeCode = dbo.bill_t_ARTransaction.TransactionTypeCode)
INNER JOIN dbo.bill_t_TripTicket ON (dbo.bill_t_ARTransaction.RunNumber = dbo.bill_t_TripTicket.RunNumber)
INNER JOIN dbo.bill_c_ChargeZone ON (dbo.bill_t_TripTicket.ChargeZoneCode = dbo.bill_c_ChargeZone.ChargeZoneCode)
WHERE dbo.bill_t_TripTicket.CompanyCode = '105'
AND dbo.bill_t_ARTransaction.TransactionDate BETWEEN '2017-01-01' AND '2017-05-31'
AND dbo.bill_c_TransactionType.TransactionTypeCode = 'INV') AS srctbl
PIVOT (SUM(Amount) FOR MONTH(srctbl.TransactionDate) IN ([1],[2],[3],[4],[5])) AS pvttbl;
谢谢!
答案 0 :(得分:0)
它可能与PIVOT子句中的括号有关。正确的语法是:
SELECT *
FROM
(
SELECT
MONTH(dbo.bill_t_ARTransaction.TransactionDate) AS DATEMONTH
, dbo.bill_c_ChargeZone.ChargeZoneCode
, dbo.bill_t_ARTransaction.Amount
FROM
dbo.bill_c_TransactionType
INNER JOIN
dbo.bill_c_TransactionTypeGroup
ON
(
dbo.bill_c_TransactionType.TransactionTypeGroupCode = dbo.bill_c_TransactionTypeGroup.TransactionTypeGroupCode
)
INNER JOIN
dbo.bill_t_ARTransaction
ON
(
dbo.bill_c_TransactionType.TransactionTypeCode = dbo.bill_t_ARTransaction.TransactionTypeCode
)
INNER JOIN
dbo.bill_t_TripTicket
ON
(
dbo.bill_t_ARTransaction.RunNumber = dbo.bill_t_TripTicket.RunNumber
)
INNER JOIN
dbo.bill_c_ChargeZone
ON
(
dbo.bill_t_TripTicket.ChargeZoneCode = dbo.bill_c_ChargeZone.ChargeZoneCode
)
WHERE
dbo.bill_t_TripTicket.CompanyCode = '105'
AND dbo.bill_t_ARTransaction.TransactionDate BETWEEN '2017-01-01' AND '2017-05-31'
AND dbo.bill_c_TransactionType.TransactionTypeCode = 'INV'
)
AS srctbl PIVOT SUM(Amount) FOR MONTH(srctbl.TransactionDate) IN ([1]
, [2]
, [3]
, [4]
, [5]) AS pvttbl
;
答案 1 :(得分:0)
我在子查询(srctbl)
中没有看到TransactionDate列在您的枢轴声明中,您有:
FOR MONTH(srctbl.TransactionDate)
认为你的意思是:
FOR MONTH(srctbl.DATEMONTH)
答案 2 :(得分:0)
已解决 - 感谢您“全部”的帮助。事实证明这是一些事情。在“Pivot”这个词后面有一个括号,当DotNetMatt足以修复我的语法时,它将错误从“near”改为“SUM附近”这个词。第二部分VDK和DotNetMatt一起找到了。我需要使用正确的查询列完全限定我的列。现在新行:
AS srctbl PIVOT (SUM(srctbl.Amount) FOR srctbl.DATEMONTH IN ([1]
, [2]
, [3]
, [4]
, [5])) AS pvttbl;
现在很棒!再次感谢大家。