Sql 在带有两个表的单个查询中选择月度订单金额和项目小计
我有一个订单表,格式如下:Sql 在带有两个表的单个查询中选择月度订单金额和项目小计,sql,Sql,我有一个订单表,格式如下: | id | service_fee_cents | grand_total_cents | created_at | |----|-------------------|-------------------|---------------| | 1 | 1400 | 10000 | Jan 21 2018 | | 2 | 1000 | 10000 |
| id | service_fee_cents | grand_total_cents | created_at |
|----|-------------------|-------------------|---------------|
| 1 | 1400 | 10000 | Jan 21 2018 |
| 2 | 1000 | 10000 | Feb 16 2018 |
| 3 | 500 | 10000 | March 21 2018 |
| 4 | 500 | 10000 | March 20 2018 |
以及表单中的Items表
| id | order_id | title | price_cents | quantity |
|----|----------|--------|-------------|----------|
| 1 | 1 | lorem | 2000 | 2 |
| 2 | 1 | ipsum | 2030 | 1 |
| 3 | 2 | pie | 4000 | 4 |
| 4 | 3 | cheese | 6000 | 2 |
| 5 | 3 | burger | 7000 | 1 |
| 6 | 4 | custar | 1000 | 1 |
| month | total_service_fee | total_grand_total | total_subtotal |
|-----------|-------------------|-------------------|----------------|
|2017-11-01 | 42 | 1,610 | 610 |
|2017-12-01 | 30 | 19,912 | 1,912 |
|2018-01-01 | 179 | 1,413 | 413 |
|2018-02-01 | 165 | 2,910 | 910 |
|2018-03-01 | 1,403 | 10,727 | 1,727 |
我正在尝试运行一个SQL查询,以获取表单中的结果
| id | order_id | title | price_cents | quantity |
|----|----------|--------|-------------|----------|
| 1 | 1 | lorem | 2000 | 2 |
| 2 | 1 | ipsum | 2030 | 1 |
| 3 | 2 | pie | 4000 | 4 |
| 4 | 3 | cheese | 6000 | 2 |
| 5 | 3 | burger | 7000 | 1 |
| 6 | 4 | custar | 1000 | 1 |
| month | total_service_fee | total_grand_total | total_subtotal |
|-----------|-------------------|-------------------|----------------|
|2017-11-01 | 42 | 1,610 | 610 |
|2017-12-01 | 30 | 19,912 | 1,912 |
|2018-01-01 | 179 | 1,413 | 413 |
|2018-02-01 | 165 | 2,910 | 910 |
|2018-03-01 | 1,403 | 10,727 | 1,727 |
我已使用此查询获得了前三列:
SELECT
date_trunc('month', created_at)::date AS month,
SUM(service_fee_cents) / 100 AS total_service_fee,
SUM(grand_total_cents) / 100 AS total_grand_total
FROM orders
GROUP BY month ORDER BY month
我怎么才能拿到最后一个?在应用程序中,我通过以下Ruby代码获得总和:
order_subtotal = order.items.map{|item| item.price * item.quantity}.reduce(:+)
它基本上会获取订单的所有项目,将价格乘以数量并添加结果。这应该是一个好的开始:
SELECT Date_trunc('month', created_at) :: DATE AS month,
SUM(service_fee_cents) / 100 AS total_service_fee,
SUM(grand_total_cents) / 100 AS total_grand_total,
SUM(total_subtotal) / 100 AS total_subtotals
FROM orders o
join (SELECT order_id,
SUM(price_cents * quantity) total_subtotal
FROM items i
GROUP BY order_id) i
ON o.id = i.order_id
GROUP BY month
ORDER BY month
您只需将Orders表连接到Items表并按月生成小计的总和,就可以实现这一目标。但是,如果像您所说的每个订单中有数千个项目,那么运行这个查询可能会有点昂贵
SELECT
date_trunc('month', created_at)::date AS month,
SUM(service_fee_cents) / 100 AS total_service_fee,
SUM(grand_total_cents) / 100 AS total_grand_total,
SUM(price_cents * quantity) / 100 AS sub_total
FROM Orders o
JOIN Items i ON i.order_id = o.id
GROUP BY month ORDER BY month
是我一个人的问题,还是你提供的样本数据根本不符合要求?我只看到一份2月份的订单(ID 2),那是4x4000。你的小计行建议应该是910?如果您要提供样本数据,请确保它与您的预期一致results@Twelfth我的示例数据被截断,因为实际数据有几十万行长。对于您给出的示例数据,2月的预期输出为4*4000?这是用于哪个数据库引擎的?我认为您的示例查询缺少与items表的联接谢谢Daniel,但是,我得到了以下错误:
列“b.total\u subtotal”必须出现在GROUP BY子句中或在聚合函数行4中使用:…嗯(grand\u total\u cents)/100作为总计、总计、子总计……
抱歉,请将其添加到您的组中-请参阅更新的答案-这是否符合您的预期?您还可以进行总和(总计、小计)或最大/最小值。Daniel-您的答案将受益于对您的答案地址的简短英语解释,这样用户就可以知道您做了什么,并可以在以后重复。“子查询以获取总计和小计并连接回主查询”就足够了+1无需考虑当我测试时,@DanielMarcus我无法对你的答案进行两个字符的编辑,但你在某些部分编写了orderid
而不是orderid
。不要按小计分组。在select中使用sum(total_subtotal),在group by中去掉它,不幸的是,数字都错了。我不确定哪里出了问题,但它们更大,尽管它们的折线图遵循相同的形状,但只有峰值更高,而谷值更低。也许你说的小计=价格*数量时,我理解得太字面了。您是否希望每月对小计=(价格/美分*数量)/100进行汇总?是的,但即使进行了这些调整,结果仍然是错误的:(我在这里添加了一个SQL Fiddle来确认逻辑。如果给定月份的小计是数量*价格,那么您在2018年1月给出的样本行中应该是60.30、2月:160和200年3月。一个潜在的问题是,我没有在除法之前将美分列转换为十进制,这样您可能会在那里失去精度。如何解决您是否正在使用数据验证结果?