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