簡體   English   中英

如何對聯合中不同表的 ID 求和和計數

[英]How to sum and count Id's of different tables from a Union

我有 3 個表councilsstation_levymarket_levy並且加入了 station 表以獲得station_levy 中的理事會。 我需要通過理事會總計 station_levy + market_levy 的數量來獲取數據,並獲得投標總額。

表格如下

councils
---------------+----------+
| council_id   | Name     |
+--------------+----------+
| 1            | LSK      |
---------------+----------+
| 2            | KBW      |
---------------+----------+


station_levy
------------------+-------------+-----------------+
| station_levy_id | station_id  | amount_tendered |
+-----------------+-------------+-----------------+
|       1         |   3         |    10.00        |
+-----------------+-------------+-----------------+
|       2         |   3         |    10.00        |
+-----------------+-------------+-----------------+
|       3         |   1         |    5.00         |
+-----------------+-------------+-----------------+


(station_id = 1 is found in the LSK council_id=1  And station_id = 3 is found in the KBW council_id=2)

market_levy
------------------+-------------+-----------------+
| market_levy_id  | council_id  | amount_tendered |
+-----------------+-------------+-----------------+
|       1         |   1         |    5.00         |
+-----------------+-------------+-----------------+
|       2         |   2         |    5.00         |
+-----------------+-------------+-----------------+
|       3         |   1         |    5.00         |
+-----------------+-------------+-----------------+

mysql

SELECT c.council_name, (COUNT(market_levy.market_levy_id)+ COUNT(st.station_levy_id )) count, SUM(amount_tendered) revenue
    FROM councils c 
    JOIN (
        (SELECT council_id, amount_tendered,market_levy_id  FROM market_levy  WHERE transaction_date >= CURDATE() )
        UNION ALL

        (SELECT station_levy_id , councils.council_id, amount_tendered 
        FROM station_levy st     
        JOIN stations ON stations.station_id = st.station_id
        JOIN councils ON councils .council_id= stations .council_id
        WHERE transaction_datetime >= CURDATE()
    )) totalCouncilRevenue USING (council_id)
    group by council_id, c.council_name ORDER BY SUM(amount_tendered) DESC 

預期結果

------------------+-------------+-----------------+
| council_name    | count       | revenue         |
+-----------------+-------------+-----------------+
|      LSK        |   3         |    15.00        |
+-----------------+-------------+-----------------+
|      KBW        |   3         |    25.00        |
+-----------------+-------------+-----------------+

您混淆了UNION ALL中的列,匹配council_idstation_levy_idamount_tenderedcouncil_id以及market_levy_idamount_tendered

然后,在您的主查詢中,您嘗試訪問market_levy.market_levy_idst.station_levy_id ,但是這些列不可訪問,因為您 select 來自名為totalCouncilRevenue的子查詢,而不是來自標有market_levyst的表。

您的查詢已修復:

SELECT 
  c.council_name, 
  COUNT(*) AS transaction_count,
  SUM(amount_tendered) AS revenue
FROM councils c 
JOIN
(
  SELECT council_id, amount_tendered
  FROM market_levy  
  WHERE transaction_date >= CURDATE() 
  UNION ALL
  SELECT s.council_id, st.amount_tendered 
  FROM station_levy st    
  JOIN stations s ON s.station_id = st.station_id
  WHERE st.transaction_datetime >= CURDATE()
) totalCouncilRevenue USING (council_id)
GROUP BY council_id, c.council_name 
ORDER BY SUM(amount_tendered) DESC;

不過,我更喜歡在加入之前進行聚合:

SELECT 
  c.council_name, 
  COALESCE(t1.cnt, 0) + COALESCE(t2.cnt, 0) AS transaction_count,
  COALESCE(t1.total, 0) + COALESCE(t2.total, 0) AS revenue
FROM councils c 
LEFT JOIN
(
  SELECT council_id, SUM(amount_tendered) as total, COUNT(*) as cnt
  FROM market_levy  
  WHERE transaction_date >= CURDATE() 
  GROUP BY council_id
) t1 USING (council_id)
LEFT JOIN
(
  SELECT s.council_id, SUM(st.amount_tendered) as total, COUNT(*) as cnt
  FROM station_levy st    
  JOIN stations s ON s.station_id = st.station_id
  WHERE st.transaction_datetime >= CURDATE()
  GROUP BY s.council_id
) t2 USING (council_id)
ORDER BY revenue DESC;

此類查詢通常不太容易出錯,有時速度更快,因為它們可以更有效地使用索引。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM