以下是在“少于14天”或“少于30天”部分标记order_date的查询。有一个订单日期是未来的日期,它也包含在“不到14天”内。
我尝试了以下内容:
WHEN DATEDIFF(DD,OH.order_date, GETDATE()) < 14 AND DATEDIFF(DD,OH.order_date, GETDATE()) < GETDATE() THEN 'Less than 14 days'
未来的日期仍然存在。请指教
原始代码:
SELECT CS.cust_id, OH.order_date,
CASE
WHEN DATEDIFF(DD,OH.order_date, GETDATE()) < 14 THEN 'Less than 14 days'
WHEN DATEDIFF(DD,OH.order_date, GETDATE()) < 30 THEN 'Less than 30 days'
END
AS 'orderAge'
FROM customers AS CS
INNER JOIN order_headers AS OH ON OH.cust_id = CS.cust_id
ORDER BY cust_id, order_date;
答案 0 :(得分:3)
SELECT CS.cust_id, OH.order_date,
CASE
WHEN DATEDIFF(DD,OH.order_date, GETDATE()) < 0 THEN 'Future date'
WHEN DATEDIFF(DD,OH.order_date, GETDATE()) < 14 THEN 'Less than 14 days'
WHEN DATEDIFF(DD,OH.order_date, GETDATE()) < 30 THEN 'Less than 30 days'
END
AS 'orderAge'
FROM customers AS CS
INNER JOIN order_headers AS OH ON OH.cust_id = CS.cust_id
ORDER BY cust_id, order_date;
答案 1 :(得分:1)
尝试使用代码,但使用0作为边界点。您只需要DATEDIFF函数的正返回值。
WHEN DATEDIFF(DD,OH.order_date, GETDATE()) < 14 AND DATEDIFF(DD,OH.order_date, GETDATE()) > 0 THEN 'Less than 14 days'