我正在编写一个查询,将一些日期组合在一起计算行数。我使用左连接将两个表连接在一起,第二列中的行是NULL我想输出0。
以下是我的数据片段。
交易表
ITEM DOE T_DOEDAY
42 11/06/2015 10:40 11/06/2015
14 12/06/2015 06:19 12/06/2015
2151 15/06/2015 07:27 15/06/2015
2114 16/06/2015 09:58 16/06/2015
98541 17/06/2015 07:15 17/06/2015
101 18/06/2015 10:14 18/06/2015
9854 19/06/2015 06:14 19/06/2015
2121 22/06/2015 07:14 22/06/2015
2354 23/06/2015 07:12 23/06/2015
252 24/06/2015 07:12 24/06/2015
5654 15/06/2015 10:12 25/06/2015
245 15/06/2015 09:11 26/06/2015
..............
属性表
ITEM DOE DOEDAY
CD 11/06/2015 10:40 11/06/2015
CD 12/06/2015 06:19 12/06/2015
OP 15/06/2015 07:27 15/06/2015
DF 16/06/2015 09:58 16/06/2015
CD 19/06/2015 06:14 19/06/2015
CD 22/06/2015 07:14 22/06/2015
OD 23/06/2015 07:12 23/06/2015
OD 24/06/2015 07:12 24/06/2015
OD 15/06/2015 10:12 25/06/2015
CD 15/06/2015 09:11 26/06/2015
...............
我在查询中尝试了以下内容 -
SELECT *
FROM (SELECT COUNT(DISTINCT T.ITEM) AS ITEM_COUNT, T.ITEM, T.DOE, T.DOEDAY AS T_DOEDAY, T.pos
FROM TRANSACTIONS T
group by T_DOEDAY ) J1
LEFT JOIN
(SELECT IFNULL(COUNT(T1.DEFECT), 0) as DEFECT_COUNT,
T1.DOEDAY AS T1_DOEDAY
FROM ATTRIBUTES T1
GROUP BY T1_DOEDAY ) J2
ON T_DOEDAY = T1_DOEDAY
其中输出以下内容 -
ITEM_COUNT ITEM DOE T_DOEDAY DEFECT_COUNT T1_DOEDAY
32 10 11/06/2015 10:40 11/06/2015 0 11/06/2015
69 18 12/06/2015 06:19 12/06/2015 0 12/06/2015
49 2 15/06/2015 07:27 15/06/2015 0 15/06/2015
14 21 16/06/2015 09:58 16/06/2015 0 16/06/2015
3 6 17/06/2015 07:14 17/06/2015 NULL NULL
16 18 18/06/2015 09:18 18/06/2015 NULL NULL
31 22 19/06/2015 06:14 19/06/2015 0 19/06/2015
98 89 22/06/2015 07:14 22/06/2015 0 22/06/2015
14 204 23/06/2015 07:12 23/06/2015 0 23/06/2015
68 202 24/06/2015 07:12 24/06/2015 0 24/06/2015
26 8 15/06/2015 10:12 25/06/2015 0 25/06/2015
133 1 15/06/2015 09:11 26/06/2015 0 26/06/2015
所以我尝试了这个查询 -
SELECT *
FROM (SELECT COUNT(DISTINCT T.ITEM) AS ITEM_COUNT, T.ITEM, T.DOE, T.DOEDAY AS T_DOEDAY, T.pos
FROM TRANSACTIONS T group by T_DOEDAY ) J1
LEFT JOIN
(SELECT IF(COUNT(T1.DEFECT) = NULL,0,COUNT(T1.DEFECT)) as DEFECT_COUNT,
T1.DOEDAY AS T1_DOEDAY
FROM ATTRIBUTES T1
GROUP BY T1_DOEDAY ) J2
ON T_DOEDAY = T1_DOEDAY
再输出以下内容 -
ITEM_COUNT ITEM DOE T_DOEDAY DEFECT_COUNT T1_DOEDAY
32 10 11/06/2015 10:40 11/06/2015 0 11/06/2015
69 18 12/06/2015 06:19 12/06/2015 0 12/06/2015
49 2 15/06/2015 07:27 15/06/2015 0 15/06/2015
14 21 16/06/2015 09:58 16/06/2015 0 16/06/2015
3 6 17/06/2015 07:14 17/06/2015 NULL NULL
16 18 18/06/2015 09:18 18/06/2015 NULL NULL
31 22 19/06/2015 06:14 19/06/2015 0 19/06/2015
98 89 22/06/2015 07:14 22/06/2015 0 22/06/2015
14 204 23/06/2015 07:12 23/06/2015 0 23/06/2015
68 202 24/06/2015 07:12 24/06/2015 0 24/06/2015
26 8 15/06/2015 10:12 25/06/2015 0 25/06/2015
133 1 15/06/2015 09:11 26/06/2015 0 26/06/2015
预期结果
ITEM_COUNT ITEM DOE T_DOEDAY DEFECT_COUNT T1_DOEDAY
32 10 11/06/2015 10:40 11/06/2015 0 11/06/2015
69 18 12/06/2015 06:19 12/06/2015 0 12/06/2015
49 2 15/06/2015 07:27 15/06/2015 0 15/06/2015
14 21 16/06/2015 09:58 16/06/2015 0 16/06/2015
3 6 17/06/2015 07:14 17/06/2015 0 NULL
16 18 18/06/2015 09:18 18/06/2015 0 NULL
31 22 19/06/2015 06:14 19/06/2015 0 19/06/2015
98 89 22/06/2015 07:14 22/06/2015 0 22/06/2015
14 204 23/06/2015 07:12 23/06/2015 0 23/06/2015
68 202 24/06/2015 07:12 24/06/2015 0 24/06/2015
26 8 15/06/2015 10:12 25/06/2015 0 25/06/2015
133 1 15/06/2015 09:11 26/06/2015 0 26/06/2015
我不明白为什么IFNULL或IF函数不能正常工作,sql运行正常,没有错误被标记出来。
如果有人能对此有所了解,我将非常感激。
答案 0 :(得分:0)
如果isnull
为真,则必须添加默认值
SELECT * FROM (SELECT COUNT(DISTINCT T.ITEM) AS ITEM_COUNT, T.ITEM, T.DOE, T.DOEDAY AS T_DOEDAY, T.pos FROM TRANSACTIONS T group by T_DOEDAY ) J1 LEFT JOIN
(SELECT IFNULL(COUNT(T1.DEFECT),0) as DEFECT_COUNT,
T1.DOEDAY AS T1_DOEDAY FROM ATTRIBUTES T1 GROUP BY T1_DOEDAY ) J2 ON T_DOEDAY = T1_DOE
答案 1 :(得分:0)
我没有看到您使用IFNULL
尝试以下查询。
SELECT * FROM (SELECT COUNT(DISTINCT T.ITEM) AS ITEM_COUNT, T.ITEM, T.DOE, T.DOEDAY AS T_DOEDAY, T.pos FROM TRANSACTIONS T group by T_DOEDAY ) J1 LEFT JOIN
(SELECT IFNULL(COUNT(T1.DEFECT),0) as DEFECT_COUNT,
T1.DOEDAY AS T1_DOEDAY FROM ATTRIBUTES T1 GROUP BY T1_DOEDAY ) J2 ON T_DOEDAY = T1_DOE
答案 2 :(得分:0)
您需要在加入后执行IFNULL
,而不是在进行计数的子查询中。当没有匹配的行时,LEFT JOIN
会添加空值。
SELECT J1.*, IFNULL(defect_count, 0) AS defect_count
FROM (SELECT COUNT(DISTINCT T.ITEM) AS ITEM_COUNT, T.ITEM, T.DOE, T.DOEDAY AS T_DOEDAY, T.pos
FROM TRANSACTIONS T
group by T_DOEDAY ) J1
LEFT JOIN
(SELECT COUNT(*) as DEFECT_COUNT,
T1.DOEDAY AS T1_DOEDAY
FROM ATTRIBUTES T1
GROUP BY T1_DOEDAY ) J2
ON J1.T_DOEDAY = J2.T1_DOEDAY