我想通過集合函數使用聯合和組,但所有嘗試都會導致缺少行或不正確的數據。下面是產生我所需要的數據的兩個疑問,現在我只需要團結他們分組和按聚合函數
這裏是pastebin
SQL查詢#1的樣本SQL轉儲:
SELECT o.id as order_id, SUM(price * quantity) as total_prod, 0 as total_inst
FROM orders as o, orders_product_line as opl, products as p
WHERE opl.order_id = o.id
AND p.id = opl.product_id
GROUP BY o.id
結果的查詢# 1
+----------+------------+------------+
| order_id | total_prod | total_inst |
+----------+------------+------------+
| 1 | 4200 | 0 |
| 2 | 40000 | 0 |
| 3 | 3600000 | 0 |
| 4 | 44500 | 0 |
| 5 | 1229800 | 0 |
| 6 | 45000000 | 0 |
+----------+------------+------------+
SQL查詢#2:
SELECT o.id as order_id, 0 as total_prod, SUM(rate * hours) as total_inst
FROM orders as o, orders_installation_line as oil, installations as i
WHERE oil.order_id = o.id
AND i.id = oil.intallation_id
GROUP BY order_id
結果的查詢#2:
+----------+------------+------------+
| order_id | total_prod | total_inst |
+----------+------------+------------+
| 1 | 0 | 4675 |
| 2 | 0 | 255000 |
| 3 | 0 | 18880 |
| 4 | 0 | 600 |
| 5 | 0 | 3540 |
+----------+------------+------------+
這是我在使用聯盟的兩個表
SELECT o.id as order_id, SUM(price * quantity) as total_prod, 0 as total_inst FROM orders as o, orders_product_line as opl, products as p WHERE opl.order_id = o.id AND p.id = opl.product_id GROUP BY o.id
UNION ALL
SELECT o.id as order_id, 0 as total_prod, SUM(rate * hours) as total_inst FROM orders as o, orders_installation_line as oil, installations as i WHERE oil.order_id = o.id AND i.id = oil.intallation_id GROUP BY order_id
而結果
+----------+------------+------------+
| order_id | total_prod | total_inst |
+----------+------------+------------+
| 1 | 4200 | 0 |
| 2 | 40000 | 0 |
| 3 | 3600000 | 0 |
| 4 | 44500 | 0 |
| 5 | 1229800 | 0 |
| 6 | 45000000 | 0 |
| 1 | 0 | 4675 |
| 2 | 0 | 255000 |
| 3 | 0 | 18880 |
| 4 | 0 | 600 |
| 5 | 0 | 3540 |
+----------+------------+------------+
最後的嘗試,這是我的嘗試在閱讀堆棧溢出的其他答案後使用union:
SELECT *
FROM
(
SELECT o.id as order_id, SUM(price * quantity) as total_prod, 0 as total_inst
FROM orders as o, orders_product_line as opl, products as p
WHERE opl.order_id = o.id
AND p.id = opl.product_id
GROUP BY o.id
UNION ALL
SELECT o.id as order_id, 0 as total_prod, SUM(rate * hours) as total_inst
FROM orders as o, orders_installation_line as oil, installations as i
WHERE oil.order_id = o.id
AND i.id = oil.intallation_id
GROUP BY order_id
) Q
GROUP BY Q.order_id
而這最後聯盟的結果:
+----------+------------+------------+
| order_id | total_prod | total_inst |
+----------+------------+------------+
| 1 | 4200 | 0 |
| 2 | 40000 | 0 |
| 3 | 3600000 | 0 |
| 4 | 44500 | 0 |
| 5 | 1229800 | 0 |
| 6 | 45000000 | 0 |
+----------+------------+------------+
我缺少什麼?我需要最後一列total_inst
來顯示值。這是我正在尋找的結果:
+----------+------------+------------+
| order_id | total_prod | total_inst |
+----------+------------+------------+
| 1 | 4200 | 4675 |
| 2 | 40000 | 255000 |
| 3 | 3600000 | 18880 |
| 4 | 44500 | 600 |
| 5 | 1229800 | 3540 |
| 6 | 45000000 | 0 |
+----------+------------+------------+
你能展示你想要的結果嗎?在第一個查詢中,您將'total_inst'設置爲0.使用您的表格結構編輯您的文章,您嘗試的結果以及您想要的結果。幫助你會容易得多。 –
編輯顯示我所追求的結果。 @JorgeCampos,在SQL 1中'total_inst'(總安裝收入)爲0,在sql 2中'total_prod'(總產品收入)爲0,因爲訂單可以有產品和/或安裝,並且我希望結果能夠顯示0代表'total_inst'和0'total_prod'。 – cborgia