返回加入的最大日期和空日期 Table
Returning Max Date and Null Date From Joined Table
我正在尝试从加入的 table 中获取特定时间段内的最大日期或空日期的结果。连接或我的 having 子句有问题吗?我现在不知所措。
我已经在 Whosebug 上检查了很多问题,但我无法达到报告空结果和最大结果的预期结果。
SELECT
i.item
, i.description
--, i.last_inv
--, i.change_date
, CASE WHEN MAX(case when m.trans_date is null THEN 1 ELSE 0 END) = 0 THEN MAX(m.trans_date) END AS 'last_used'
FROM item_mst i
LEFT JOIN matltran_mst m on m.item = i.item
WHERE i.item like '2%' and m.trans_type <> 'M'
GROUP BY i.item, i.description
HAVING (MAX(m.trans_date) NOT BETWEEN '10/1/2017' and '5/8/2019' or MAX(m.trans_date) IS NULL)
ORDER BY 'last_used'
我期待大约 8 个结果 last_used 日期和超过 1,000 个(实际数量待定),但我得到的是 last_used 日期的 8 个结果,没有任何 NULL日期。
这是预期结果的示例:
项目last_used
21050000202-0 2017-08-31 00:00:00.000
2200130425 2017-08-09 00:00:00.000
2200130494 2017-09-15 00:00:00.000
220030010 2017-09-25 00:00:00.000
2201050937-2 2017-09-01 00:00:00.000
22081001202 2017-08-16 00:00:00.000
2209070909 2017-08-15 11:31:45.230
221500 2017-08-21 00:00:00.000
21010000215 空
21050000215 空
21050000215-0 空
210800 空
21330000202 空
213600 空
22000000210 空
您需要将 "m.trans_type <> 'M' " 从 WHERE 子句移动到 ON 子句。
"m.trans_type <> 'M'" 导致此语句的行为类似于内部联接。
您正在检查 所有 个日期 NULL
而不是其中一个。所以,试试这个:
HAVING MAX(m.trans_date) NOT BETWEEN '2017-10-01' AND '2019-05-08' OR
-- or whatever these dates really are
COUNT(m.trans_date) < COUNT(*) -- are any of them NULL?
您还可以修复查询的其余部分:
SELECT . . .,
(CASE WHEN COUNT(m.trans_date) = COUNT(*) THEN MAX(m.trans_dte) END) as last_used -- NULL if any are NULL
FROM item_mst i LEFT JOIN
matltran_mst m
ON m.item = i.item AND and m.trans_type <> 'M'
WHERE i.item like '2%'
GROUP BY i.item, i.description
HAVING MAX(m.trans_date) NOT BETWEEN '2017-10-01' AND '2019-05-08' OR
-- or whatever these dates really are
COUNT(m.trans_date) < COUNT(*) -- are any of them NULL?
ORDER BY last_used;
我觉得你可以直接忽略这么多CASE/WHEN-
MAX(m.trans_date) END AS 'last_used'
这将直接 return 最大数据(如果日期可用)或 NULL(如果没有可用日期)。请更改 ORDER BY 如下-
ORDER BY MAX(m.trans_date)
如果您对列执行 MAX 等函数,则该列中任何具有 NULL 值的行都将被忽略。如果在 UNION 查询中将查询 WITH NULL m.trans_date 的行从查询 NOT BETWEEN 某些日期的行查询中分离出来,您可能会得到想要的结果:
SELECT
i.item
, i.description
--, i.last_inv
--, i.change_date
, 1 AS 'last_used'
FROM item_mst i
LEFT JOIN matltran_mst m on m.item = i.item
WHERE m.trans_date IS NULL
GROUP BY i.item, i.description
ORDER BY 'last_used'
UNION
SELECT
i.item
, i.description
--, i.last_inv
--, i.change_date
, MAX(m.trans_date)AS 'last_used'
FROM item_mst i
LEFT JOIN matltran_mst m on m.item = i.item
WHERE i.item like '2%' and m.trans_type <> 'M'
GROUP BY i.item, i.description
HAVING (MAX(m.trans_date) NOT BETWEEN '10/1/2017' and '5/8/2019')
ORDER BY 'last_used'
我正在尝试从加入的 table 中获取特定时间段内的最大日期或空日期的结果。连接或我的 having 子句有问题吗?我现在不知所措。
我已经在 Whosebug 上检查了很多问题,但我无法达到报告空结果和最大结果的预期结果。
SELECT
i.item
, i.description
--, i.last_inv
--, i.change_date
, CASE WHEN MAX(case when m.trans_date is null THEN 1 ELSE 0 END) = 0 THEN MAX(m.trans_date) END AS 'last_used'
FROM item_mst i
LEFT JOIN matltran_mst m on m.item = i.item
WHERE i.item like '2%' and m.trans_type <> 'M'
GROUP BY i.item, i.description
HAVING (MAX(m.trans_date) NOT BETWEEN '10/1/2017' and '5/8/2019' or MAX(m.trans_date) IS NULL)
ORDER BY 'last_used'
我期待大约 8 个结果 last_used 日期和超过 1,000 个(实际数量待定),但我得到的是 last_used 日期的 8 个结果,没有任何 NULL日期。
这是预期结果的示例:
项目last_used
21050000202-0 2017-08-31 00:00:00.000 2200130425 2017-08-09 00:00:00.000 2200130494 2017-09-15 00:00:00.000 220030010 2017-09-25 00:00:00.000 2201050937-2 2017-09-01 00:00:00.000 22081001202 2017-08-16 00:00:00.000 2209070909 2017-08-15 11:31:45.230 221500 2017-08-21 00:00:00.000 21010000215 空 21050000215 空 21050000215-0 空 210800 空 21330000202 空 213600 空 22000000210 空
您需要将 "m.trans_type <> 'M' " 从 WHERE 子句移动到 ON 子句。
"m.trans_type <> 'M'" 导致此语句的行为类似于内部联接。
您正在检查 所有 个日期 NULL
而不是其中一个。所以,试试这个:
HAVING MAX(m.trans_date) NOT BETWEEN '2017-10-01' AND '2019-05-08' OR
-- or whatever these dates really are
COUNT(m.trans_date) < COUNT(*) -- are any of them NULL?
您还可以修复查询的其余部分:
SELECT . . .,
(CASE WHEN COUNT(m.trans_date) = COUNT(*) THEN MAX(m.trans_dte) END) as last_used -- NULL if any are NULL
FROM item_mst i LEFT JOIN
matltran_mst m
ON m.item = i.item AND and m.trans_type <> 'M'
WHERE i.item like '2%'
GROUP BY i.item, i.description
HAVING MAX(m.trans_date) NOT BETWEEN '2017-10-01' AND '2019-05-08' OR
-- or whatever these dates really are
COUNT(m.trans_date) < COUNT(*) -- are any of them NULL?
ORDER BY last_used;
我觉得你可以直接忽略这么多CASE/WHEN-
MAX(m.trans_date) END AS 'last_used'
这将直接 return 最大数据(如果日期可用)或 NULL(如果没有可用日期)。请更改 ORDER BY 如下-
ORDER BY MAX(m.trans_date)
如果您对列执行 MAX 等函数,则该列中任何具有 NULL 值的行都将被忽略。如果在 UNION 查询中将查询 WITH NULL m.trans_date 的行从查询 NOT BETWEEN 某些日期的行查询中分离出来,您可能会得到想要的结果:
SELECT
i.item
, i.description
--, i.last_inv
--, i.change_date
, 1 AS 'last_used'
FROM item_mst i
LEFT JOIN matltran_mst m on m.item = i.item
WHERE m.trans_date IS NULL
GROUP BY i.item, i.description
ORDER BY 'last_used'
UNION
SELECT
i.item
, i.description
--, i.last_inv
--, i.change_date
, MAX(m.trans_date)AS 'last_used'
FROM item_mst i
LEFT JOIN matltran_mst m on m.item = i.item
WHERE i.item like '2%' and m.trans_type <> 'M'
GROUP BY i.item, i.description
HAVING (MAX(m.trans_date) NOT BETWEEN '10/1/2017' and '5/8/2019')
ORDER BY 'last_used'