SQL使用groupBy复制表

时间:2018-04-21 13:56:53

标签: sql duplicates mariadb

我正在尝试使用简单的查询来比较收入/支出,但出于某种原因,我得到了重复的数据。这是我正在运行的查询:

SELECT
    Event.Name as "Event", 
    Concat("£", round(sum(Ticket.Price),2)) as "Ticket Sales",
    sum(Invoice.NetTotal) as "Invoice Costs",
    Concat("£", round(sum(Ticket.Price),2) - round(sum(Invoice.NetTotal),2)) as "Total Loss"
FROM Ticket 
JOIN Event ON Ticket.EventID = Event.EventID
JOIN Invoice ON Event.EventID = Invoice.EventID
GROUP BY Event.EventID;

这是我得到的结果

+--------------------------+--------------+---------------+------------+
| Event                    | Ticket Sales | Invoice Costs | Total Loss |
+--------------------------+--------------+---------------+------------+
| Victorious Festival 2018 | £47.94       |          1800 | £-1752.06  |
+--------------------------+--------------+---------------+------------+

尽管发票表中只有2件物品,总计600英镑, 票表中的3个相关项目共计24.97英镑

+-----------+--------+---------+---------------+-------------+----------+------+
| InvoiceNo | ItemID | EventID | HireStartDate | HireEndDate | NetTotal | VAT  |
+-----------+--------+---------+---------------+-------------+----------+------+
|         1 |      1 |       1 | 2018-05-05    | 2018-05-06  |      500 |   20 |
|         2 |      2 |       1 | 2018-05-05    | 2018-05-06  |      100 |   20 |
+-----------+--------+---------+---------------+-------------+----------+------+
    +----------+---------+-------+------------+------------+----------+
    | TicketNo | EventID | Price | ValidFrom  | ValidTo    | Class    |
    +----------+---------+-------+------------+------------+----------+
    |        1 |       1 |  7.99 | 2018-05-05 | 2018-05-22 | Standard |
    |        2 |       1 |  7.99 | 2018-05-05 | 2018-05-22 | Standard |
    |        3 |       2 |    10 | 2018-04-28 | 2018-04-28 | Standard |
    |        4 |       2 |    10 | 2018-04-28 | 2018-04-28 | Standard |
    |        5 |       2 |    10 | 2018-04-28 | 2018-04-28 | Standard |
    |        6 |       2 |    10 | 2018-04-28 | 2018-04-28 | Standard |
    |        7 |       2 |    10 | 2018-04-28 | 2018-04-28 | Standard |
    |        8 |       2 |    10 | 2018-04-28 | 2018-04-28 | Standard |
    |        9 |       1 |  7.99 | 2018-05-05 | 2018-05-22 | Standard |
    +----------+---------+-------+------------+------------+----------+

1 个答案:

答案 0 :(得分:1)

您有两个不同的独立维度。最好的解决方案是在加入之前聚合

SELECT e.Name as "Event",
       Concat("£", round(sum(t.Price), 2)) as "Ticket Sales",
       sum(i.NetTotal) as "Invoice Costs",
       Concat("£", round(sum(t.Price), 2) - round(sum(i.NetTotal), 2)) as "Total Loss"
FROM Event e JOIN
     (SELECT t.EventId, SUM(Price) as Price
      FROM Ticket t
      GROUP BY t.EventId
     ) t
     ON t.EventID = e.EventID JOIN
     (SELECT i.EventId, SUM(i.NetTotal) as NetTotal
      FROM Invoice i
      GROUP BY i.EventId
     ) i
     ON e.EventID = i.EventID
GROUP BY e.EventID;

两条评论。首先,我不喜欢在EventId上进行汇总,因为它不在SELECT中(而是更喜欢EventName)。假设它是Events的主键,那么这个结构很好 - id唯一地标识事件中的每一行,因此名称是明确定义的。

其次,你可能想要加入left join,所以你要包括所有事件,甚至那些可能缺少票据或发票的事件。