問題座落在自己的SELECT語句的4號線:CASE WHEN ct.TransactionReason=622 THEN ABS(ct.netquantity) ELSE c.RealNetWeight END AS NetWeight
SQL CASE混淆了我的分組
當我加入這一行的聲明,我的分組會改變。現在它不是返回一行,而是現在給我返回不同c.realnetweight
的行數。 問題是我只想返回一行。有點像一個合併,當有一個ct.transactionreason = 622
,它應該給我ABS(ct.netquantity)
,否則c.realnetweight
。代碼可以在下面找到,建議將非常有幫助。謝謝。
SELECT CASE WHEN P.Wrapped = 1 THEN T.[Level]+1 ELSE T.[Level] END AS [Level]
, @CoilId AS CoilId
, c.SupplierCoilID
, CASE WHEN ct.TransactionReason=622 THEN ABS(ct.netquantity) ELSE c.RealNetWeight END AS NetWeight
, C.RealGrossWeight
, p1.Description
, p1.product
, s.StackID
, s.ProductID
, s.Weight
, P.Product
, P.Description AS 'ProductDescription'
, COUNT(t.BlankId) AS 'NumberOfBlanks'
, c1.Description as 'Status'
, pv.ProductionWeight
, pv.BlankWeight
, t.BlankStatus
FROM @Trace T
INNER JOIN SKUTraceability SKUT ON SKUT.SKUID = T.SKUID
INNER JOIN Stack s ON SKUT.StackID = s.StackID
INNER JOIN Product p ON s.ProductID = p.ProductID
INNER JOIN Coil c ON [email protected]
INNER JOIN CoilTransaction ct on [email protected]
INNER JOIN Product p1 ON c.ProductID=p1.ProductID
INNER JOIN Code c1 ON t.BlankStatus=c1.codenumber AND c1.codetypeid=17
INNER JOIN @ProductVersion pv ON pv.ProductID=p.ProductId AND s.ProductVersion = pv.ProductVersion
WHERE t.BlankId IS NOT NULL
GROUP BY T.[Level]
, c.SupplierCoilID
, CASE WHEN ct.TransactionReason=622 THEN ABS(ct.netquantity) ELSE c.RealNetWeight END
, c.RealGrossWeight
, p1.Description
, p1.product
, s.StackID
, s.ProductID
, s.Weight
, p.Product
, p.Description
, c1.Description
, pv.ProductionWeight
, pv.BlankWeight
, p.Wrapped
, t.BlankStatus
如何刪除組中的CASE WHEN列,並將其替換爲NetWeight列? – StackTrace
然後它會給我一個關於ct.transactionreason和ct.netquantity不在GROUP BY或AGGREGATE函數中的錯誤。 –
您需要將它們添加到組中或在聚合子句中。所以,如果你有不同的'c.realnetweight'哪一個應該顯示在你的'一行' - 更小,最大或什麼? – gotqn