0
讓我首先說我不是SQL編碼方面的專家。我以爲我有這個查詢,直到我把它放入SSRS,並注意到我的'消費'是幾乎所有情況下的價值翻倍。我無法弄清楚什麼是錯的。我改變了連接,但從來沒有得到預期的結果。這是查詢。SQL語句未返回預期的確切結果
SELECT a.No_
,Sum(b.Consumption) as Consumption
,sum(c.[Item Ledger Entry Quantity]) as 'Total Qty'
,sum(e.Amount) as 'Total Amount'
FROM Crest$Item a
INNER JOIN (SELECT [Item No_] INo, Sum(Quantity) Consumption
FROM [Crest$Item Ledger Entry]
WHERE [Entry Type] = 5 and [Posting Date] Between '2017-01-01' and '2017-01-31'
GROUP BY [Item No_]) b on a.No_ = b.INo
INNER JOIN [Crest$Value Entry] c ON a.No_ = c.[Item No_]
INNER JOIN [Crest$G_L - Item Ledger Relation] d on c.[Entry No_] = d.[Value Entry No_]
INNER JOIN [Crest$G_L Entry] e ON d.[G_L Entry No_] = e.[Entry No_]
WHERE e.[G_L Account No_] = '55130'
and c.[Posting Date] between '2017-01-01' and '2017-01-31'
group by a.No_
order by a.No_
當返回的數據我已經在列的正確數據NO_,總數量和總金額,但第二列「消費」是更多的時間比沒有它應該是值完全一倍。
我沒有看到問題。 謝謝。
似乎消費總和乘以外部查詢的行數。一些休息表或在消費領域使用分組在外部查詢而不是重複使用總和 – niktrs
您正在使用哪些DBMS? –
Microsoft SQL Server 2014 – mdebner