我的 SQL 查询中的条目使结果值加倍

Entries in my SQL Query doubles the result values

我有 3 个 Table(交易、交易条目和投标条目)。当我 运行 我的 SQL 查询时,由于 TransactionEntry Table.

,我得到了两次总和值

假设我有 2 件物品(价值 288.75 美元的衣服和价值 50.00 美元的免费雨伞)。我使用价值 $300 的现金和价值 $100 的 GC 结算付款,现在我有价值 $61.25 的现金找零。

这是 3 tables

中的条目

交易table

TransactionNumber|Total|
========================
1                |338.75|

交易入口Table

TransactionNumber|ItemID|Price|
===============================
1                |245648|288.75|
1                |129   |50.00 |

投标项目Table

TransactionNumber|TenderID|Description|Amount|
==============================================
1                |1       |Cash       |300.00| 
1                |1       |Cash       |-61.25|
1                |20      |GC         |100.00|

这是我到目前为止所做的

select [Transaction].TransactionNumber,
       (case when [Transaction].RecallType = 0 then [Transaction].total else 0 end) as Sales,
       sum(case when TransactionEntry.ItemID = 6922 then TransactionEntry.Price
        when TransactionEntry.price = 0 then TransactionEntry.price else 0 end) as Free,
       sum(case when TenderEntry.tenderID = 1 then TenderEntry.Amount else 0 end) as Cash,
       sum(case when TenderEntry.tenderID = 20 then TenderEntry.Amount else 0 end) as GC
 from  [Transaction] inner join 
       TransactionEntry on [Transaction].transactionnumber = TransactionEntry.transactionnumber inner join 
       TenderEntry on [Transaction].transactionnumber = TenderEntry.transactionnumber
 group by [Transaction].TransactionNumber,
          (case when [Transaction].RecallType = 0 then [Transaction].total else 0 end)

我得到了这种输出

TransactionNumber|Sales |Free  |Cash   |GC   |
==============================================
1                |338.75|150.00|477.50|200.00|

而不是这个

TransactionNumber|Sales |Free |Cash   |GC    |
==============================================
1                |338.75|50.00|238.75 |100.00|

您希望每个事务一行,所以我建议您在执行联接之前将联接的 table 求和到事务级别,如下所示:

select t1.TransactionNumber,
       case when t1.RecallType = 0 then t1.total else 0 end as Sales,
       t2.Free,
       t3.Cash,
       t3.GC
from [Transaction] t1
inner join (select transactionnumber, 
            sum(case when ItemID = 6922 then Price else 0 end) as free 
            from TransactionEntry 
            group by transactionnumber) as t2 
            on t1.transactionnumber = t2.transactionnumber
inner join (select transactionnumber, 
            sum(case when tenderID = 1 then Amount else 0 end) as Cash,
            sum(case when tenderID = 20 then Amount else 0 end) as GC
            from TenderEntry
            group by transactionnumber) as t3
            on t1.transactionnumber = t3.transactionnumber

请注意,我使用 table 别名(t1、t2、t3)来压缩代码并使其更易于阅读。看起来好像“交易”是一个保留字,所以可能不是 table 名称的好选择,因为它意味着您在引用它时总是必须使用方括号将名称括起来。

您的这段代码似乎有问题,

sum(case when TransactionEntry.ItemID = 6922 then TransactionEntry.Price  
when TransactionEntry.price = 0 then TransactionEntry.price else 0 end) as Free,

因为你的雨伞,你说是免费的,是项目 129 而不是 6922。此外,“当 TransactionEntry.price = 0 然后......”将价格设置为 0 而不管价格。我不确定你的意图是什么?