简体   繁体   English

MySQL 左外连接故障

[英]MySQL left outer join trouble

Here is a query that groups transactions by pricepoint on an hourly basis:这是一个按价格点按小时对交易进行分组的查询:

SELECT hour(Stamp) AS hour, PointID AS pricepoint, count(1) AS counter
FROM Transactions
GROUP BY 1,2;

Sample output:样品 output:

+------+------------+---------+
| hour | pricepoint | counter |
+------+------------+---------+
|    0 |         19 |       5 |
|    0 |         20 |      14 |
|    1 |         19 |       3 |
|    1 |         20 |      12 |
|    2 |         19 |       2 |
|    2 |         20 |       8 |
|    3 |         19 |       2 |
|    3 |         20 |       4 |
|    4 |         19 |       1 |
|    4 |         20 |       1 |
|    5 |         19 |       4 |
|    5 |         20 |       1 |
|    6 |         20 |       2 |
|    8 |         19 |       1 |
|    8 |         20 |       4 |
|    9 |         19 |       2 |
|    9 |         20 |       5 |
|   10 |         19 |       6 |
|   10 |         20 |       1 |
|   11 |         19 |      10 |
|   11 |         20 |       2 |
|   12 |         19 |      10 |
|   12 |         20 |       3 |
|   13 |         19 |      10 |
|   13 |         20 |      10 |
|   14 |         19 |       8 |
|   14 |         20 |       3 |
|   15 |         19 |       6 |
|   15 |         20 |       8 |
|   16 |         19 |      11 |
|   16 |         20 |      10 |
|   17 |         19 |       7 |
|   17 |         20 |      17 |
|   18 |         19 |       7 |
|   18 |         20 |       9 |
|   19 |         19 |      10 |
|   19 |         20 |      12 |
|   20 |         19 |      17 |
|   20 |         20 |      11 |
|   21 |         19 |      12 |
|   21 |         20 |      29 |
|   22 |         19 |       6 |
|   22 |         20 |      21 |
|   23 |         19 |       9 |
|   23 |         20 |      23 |
+------+------------+---------+

As you can see, some hours have no transactions (eg 7am), and some hours only have transactions for a single pricepoint (eg 6am, only pricepoint 20 but no transactions for pricepoint 19).如您所见,有些时间没有交易(例如早上 7 点),有些时间只有一个价格点的交易(例如早上 6 点,只有价格点 20,但价格点 19 没有交易)。

I would like to display the results set with "0" when there are no transactions, rather than just not being there as is the case now.当没有交易时,我想用“0”显示结果集,而不是像现在这样不存在。

Trying to work with a LEFT OUTER JOIN there.尝试在那里使用 LEFT OUTER JOIN。 The inHour table contains values 0..23 inHour表包含值 0..23

SELECT H.hour, PointID AS Pricepoint, COALESCE(T.counter, 0) AS Count
FROM inHour H
LEFT OUTER JOIN
(
 SELECT hour(Stamp) AS Hour, PointID, count(1) AS counter
 FROM Transactions
 GROUP BY 1,2
 ) T
ON T.Hour = H.hour;

This produces the following output (truncated for brevity):这将产生以下 output(为简洁起见截断):

|    5 |         19 |     4 |
|    5 |         20 |     1 |
|    6 |         20 |     2 |
|    7 |       NULL |     0 |
|    8 |         19 |     1 |
|    8 |         20 |     4 |

What I would like in fact would be:我实际上想要的是:

|    5 |         19 |     4 |
|    5 |         20 |     1 |
|    6 |         19 |     0 |
|    6 |         20 |     2 |
|    7 |         19 |     0 |
|    7 |         20 |     0 |
|    8 |         19 |     1 |
|    8 |         20 |     4 |

In my desired output, the value "0" is put next to pricepoints that had no transactions during a given hour.在我想要的 output 中,值“0”放在给定时间内没有交易的价格点旁边。

Your suggestions would be welcome.欢迎您提出建议。 Thanks.谢谢。

SELECT h.Hour, p.Pricepoint, COUNT(t.*) AS Count
FROM inHour h,
(SELECT DISTINCT PointId AS Pricepoint FROM Transactions) p
LEFT OUTER JOIN Transactions t
ON h.Hour = hour(t.Stamp) AND p.Pricepoint = t.PointID
GROUP BY h.Hour, p.Pricepoint
ORDER BY h.Hour, p.Pricepoint

I don't have time at the moment to try this, so let me know if it doesn't work and I'll try to adjust.我现在没有时间尝试这个,所以如果它不起作用,请告诉我,我会尝试调整。

Someone probably has a better solution than this, but I would use a UNION to simplify things:有人可能有比这更好的解决方案,但我会使用 UNION 来简化事情:

SELECT hour(Stamp) AS hour, PointID AS pricepoint, count(1) AS counter
FROM Transactions
GROUP BY 1,2

UNION

SELECT hour,0 AS pricepoint,0 AS counter FROM inHour WHERE hour NOT IN (SELECT hour(Stamp) FROM Transactions)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM