0
我有一個SQL調用,目前按年取得數據,並按照下面的代碼按月份顯示。相同的數據,兩個不同的結果
SELECT MaterialCode, [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12]
FROM
(SELECT mb.MaterialCode, DATEPART(m, Date) as [dp], Weight
FROM Issues iss
JOIN MaterialBatches mb ON iss.MaterialBatchId = mb.Id
LEFT JOIN Materials m ON mb.MaterialCode = m.Code
WHERE IssueType = 'MaterialIssue' AND
DatePart(yyyy,Date) = 2013
AND Weight > 0
AND (COALESCE(m.IsNonDispenseMaterial,0) = 0 OR CAST((SELECT Value FROM ConfigurationSettings WHERE Name = 'IncludeNonDispenseWeights') as varchar(10)) = 'true')) i
PIVOT (SUM(Weight) FOR i.dp IN
([1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12])) as pvt
此代碼現在需要更改以使所選月份的起始時間可更改。使用下面的代碼我得到了948多記錄\排回。
SELECT Code, [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12]
FROM
(SELECT mb.Code, DATEPART(m, Date) as [dp], Weight
FROM Issues iss
JOIN Batches mb ON iss.Id = mb.Id
LEFT JOIN Mats m ON mb.Code = m.Code
WHERE IssueType = 'MatIssue' AND
YEAR(Date) = 2013 AND MONTH(Date) = 01 OR
YEAR(Date) = 2013 AND MONTH(Date) = 02 OR
YEAR(Date) = 2013 AND MONTH(Date) = 03 OR
YEAR(Date) = 2013 AND MONTH(Date) = 04 OR
YEAR(Date) = 2013 AND MONTH(Date) = 05 OR
YEAR(Date) = 2013 AND MONTH(Date) = 06 OR
YEAR(Date) = 2013 AND MONTH(Date) = 07 OR
YEAR(Date) = 2013 AND MONTH(Date) = 08 OR
YEAR(Date) = 2013 AND MONTH(Date) = 09 OR
YEAR(Date) = 2013 AND MONTH(Date) = 10 OR
YEAR(Date) = 2013 AND MONTH(Date) = 11 OR
YEAR(Date) = 2013 AND MONTH(Date) = 12
AND Weight > 0
AND (COALESCE(m.IsNonDisp,0) = 0 OR CAST((SELECT Value FROM ConfigurationSettings WHERE Name = 'IncludeNonDisp') as varchar(10)) = 'true')) i
PIVOT(SUM(Weight) FOR i.dp IN
([1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12])) as pvt
你能告訴我,我哪裏出錯了嗎?
非常感謝您!完美的作品...我認爲他們通過樹木稱之爲木材! –
不客氣:) – CeOnSql
我有點困,但是那個大的'where'部分應該做什麼,我的可以縮短到'YEAR(Date)= 2013'? – potashin