我已经找到类似的东西,但找不到任何东西。
我有一个查询,它提供每天发生的交易数量,并且需要按年,月,周进行分组但当然有几个月跨越多周数字,例如。 9月& 2009年10月。
以去年的第39周为例(9月和10月)。周四是10月1日,因此10月的那一周下降4天,因此9月最后3天的交易量应该加到10月的第一周?清除?
例如:
VOLUME ---- ---- TRANSACTION YEAR ---- ----一个月WEEK
1264.1730 ---- ---- 53 ---- 2009年九月37 ----
2739.7200 --- 109 ---- 2009年---- ----月38
的 522.5500 ----- ---- 21 ---- 2009年十月39 ----
1196.6450 ---- ---- 51 ---- 2009年九月39 ----
2827.9550 --- 113 ---- 2009年---- ----月40
2730.4050 --- 110 ---- 2009年---- ----月41
3763.7200 --- 154 ---- 2009年---- ----月42
3425.6250 --- 137 ---- 2009年---- ----月43
3551.8100 --- 143 ---- 2009年----月 - 44
2788.0150 --- 113 ---- ---- 2009年十一月 - 45
答案 0 :(得分:1)
问题在于日历很尴尬,而且你无能为力。据我所知,你有三个选择:
根据您的描述,您似乎想要选项2:
SELECT year, MIN(month), week, SUM(transaction)
FROM Table1
GROUP BY year, week
答案 1 :(得分:0)
这样的事情可以做到:
-- For weeks starting Sunday and ending Saturday, the US default:
SET DATEFIRST 7
-- Alternatively, for weeks starting Saturday and ending Friday:
--SET DATEFIRST 6
SELECT
[Date]
, DATENAME(WEEKDAY,[Date]) AS [DayOfWeek]
, DATEADD(DAY,1-DATEPART(WEEKDAY,[Date]),[Date]) AS WeekStarting
, DATEADD(DAY,7-DATEPART(WEEKDAY,[Date]),[Date]) AS WeekEnding
FROM (
SELECT CONVERT(DATETIME,'20100124') UNION ALL
SELECT CONVERT(DATETIME,'20100125') UNION ALL
SELECT CONVERT(DATETIME,'20100126') UNION ALL
SELECT CONVERT(DATETIME,'20100127') UNION ALL
SELECT CONVERT(DATETIME,'20100128') UNION ALL
SELECT CONVERT(DATETIME,'20100129') UNION ALL
SELECT CONVERT(DATETIME,'20100130') UNION ALL
SELECT CONVERT(DATETIME,'20100131') UNION ALL
SELECT CONVERT(DATETIME,'20100201') UNION ALL
SELECT CONVERT(DATETIME,'20100202') UNION ALL
SELECT CONVERT(DATETIME,'20100203') UNION ALL
SELECT CONVERT(DATETIME,'20100204') UNION ALL
SELECT CONVERT(DATETIME,'20100205') UNION ALL
SELECT CONVERT(DATETIME,'20100206')
) a ([Date])
然后,将您的周开始日期或结束日期转换为月份:
SELECT *
, WeekStartingMonthStart = DATEADD(DAY,1-DAY(WeekStarting),WeekStarting)
, WeekStartingMonthEnd = DATEADD(DAY,-1,DATEADD(MONTH,1,DATEADD(DAY,1-DAY(WeekStarting),WeekStarting)))
, WeekEndingMonthStart = DATEADD(DAY,1-DAY(WeekEnding),WeekEnding)
, WeekEndingMonthEnd = DATEADD(DAY,-1,DATEADD(MONTH,1,DATEADD(DAY,1-DAY(WeekEnding),WeekEnding)))
FROM (
SELECT
[Date]
, DATENAME(WEEKDAY,[Date]) AS [DayOfWeek]
, DATEADD(DAY,1-DATEPART(WEEKDAY,[Date]),[Date]) AS WeekStarting
, DATEADD(DAY,7-DATEPART(WEEKDAY,[Date]),[Date]) AS WeekEnding
FROM (
SELECT CONVERT(DATETIME,'20100124') UNION ALL
SELECT CONVERT(DATETIME,'20100125') UNION ALL
SELECT CONVERT(DATETIME,'20100126') UNION ALL
SELECT CONVERT(DATETIME,'20100127') UNION ALL
SELECT CONVERT(DATETIME,'20100128') UNION ALL
SELECT CONVERT(DATETIME,'20100129') UNION ALL
SELECT CONVERT(DATETIME,'20100130') UNION ALL
SELECT CONVERT(DATETIME,'20100131') UNION ALL
SELECT CONVERT(DATETIME,'20100201') UNION ALL
SELECT CONVERT(DATETIME,'20100202') UNION ALL
SELECT CONVERT(DATETIME,'20100203') UNION ALL
SELECT CONVERT(DATETIME,'20100204') UNION ALL
SELECT CONVERT(DATETIME,'20100205') UNION ALL
SELECT CONVERT(DATETIME,'20100206')
) a ([Date])
) a