我正在尝试将两个单独运行得非常好的查询组合起来。但我一直在努力让他们团结起来,共同工作,并取得预期的成果。这两个查询是:
select clientid, sum(fee) as "Total Spent"
from bookings
group by clientid;
select l.clientid, sum(m.price * l.quantity) as "Total Spent"
from lineitems l
join merchandise m on m.merchid = l.merchid
group by l.clientid;
因此,最终目标是将每个客户用于预订和购买的金额结合起来。i、 e.客户id 12在预订上花费了450美元,在产品上花费了85美元;所以总共是535美元。
数据集如下:
预订表:
+----------+-------+------------+----------+-----------+---------+------------+
| ClientId | Tour | EventMonth | EventDay | EventYear | Payment | DateBooked |
+----------+-------+------------+----------+-----------+---------+------------+
| 12 | South | Feb | 20 | 2016 | 225 | 19/02/2016 |
| 12 | West | Mar | 5 | 2016 | 225 | 3/03/2016 |
+----------+-------+------------+----------+-----------+---------+------------+
行项目表:
+----------+-------+------------+----------+-----------+---------+-----+
| ClientID | Tour | EventMonth | EventDay | EventYear | MerchId | Qty |
+----------+-------+------------+----------+-----------+---------+-----+
| 12 | South | Feb | 20 | 2016 | 20 | 1 |
+----------+-------+------------+----------+-----------+---------+-----+
商品表:
+---------+----------+------------+-------+
| MerchID | Category | ProdName | Price |
+---------+----------+------------+-------+
| 20 | A | Highway | 85 |
+---------+----------+------------+-------+
任何帮助都将不胜感激
2条答案
按热度按时间8yoxcaq71#
这与gordon的答案基本相同,但使用内联的示例数据和总计:
ubof19bj2#
你可以用
join
:如果表具有不同的客户机集,则可能需要外部联接。