尝试创建一个列,告诉我weekofyear()以及日期:
Week Date org cost
--------------------------------------------
50 "2016/12/12-2016/12/18" 1 400
51 "2016/12/19-2016/12/25" 1 400
52 "2016/12/26" 1 400
所以这告诉我第52周只包括12月26日的数据,而不是整个星期的数据。
这是我的查询:
SELECT
weekofyear(date) as week_number,
exchange,
country,
channel,
sum (media_cost) as mc
FROM
imps
WHERE
date BETWEEN '2016-12-26' AND '2017-01-08'
GROUP BY
weekofyear(date) as week_number,
exchange,
country,
channel;
我看过How do you get the “week start date” and “week end date” from week number in SQL Server?,但这并不能解决看它包含的日子的问题。 可能会使用垃圾箱吗?你有什么建议。
答案 0 :(得分:0)
在您的查询中,WHERE
将无效,因为字符串" 2016/12 / 12-2016 / 12/18"永远不会在两个日期之间。您需要制作两个日期列,例如week_begin
和week_end
。那么您的WHERE
可以是:
WHERE week_begin BETWEEN '2016-12-26' AND '2017-01-08'
OR week_end BETWEEN '2016-12-26' AND '2017-01-08'