我不能把我的脑袋包裹起来:(
我正在尝试从我的专栏“Diff”中输出3个不同的结果
< 1< 3< 7
到目前为止,这是我的查询:
SELECT DATEDIFF(CURRENT_DATE, updated_at) AS Diff, COUNT(*) FROM Table GROUP BY Diff
但实际上我只需输出3列,金额<1,<3和<7
我希望有人可以提供帮助
答案 0 :(得分:1)
使用条件聚合:
SELECT
COUNT(CASE WHEN DATEDIFF(CURRENT_DATE, updated_at) < 1 THEN 1 END) AS diff_lt_1,
COUNT(CASE WHEN DATEDIFF(CURRENT_DATE, updated_at) >= 1 AND
DATEDIFF(CURRENT_DATE, updated_at) < 3 THEN 1 END) AS diff_1_to_3,
COUNT(CASE WHEN DATEDIFF(CURRENT_DATE, updated_at) >= 3 AND
DATEDIFF(CURRENT_DATE, updated_at) < 7 THEN 1 END) AS diff_3_to_7,
COUNT(CASE WHEN DATEDIFF(CURRENT_DATE, updated_at) >= 7 THEN 1 END) AS diff_gt_7
FROM Table;
实际上并不清楚您应该在哪个列GROUP BY
进行操作。如果您显示示例数据以显示您要执行的操作,我们可以修改上述查询。