我正在使用 SQL Server。当我尝试使用 datepart 函数按周对信息进行排序时,它不返回任何值。我的表中有所选日期范围的值。
我尝试使用的查询是:
Declare @StartDate datetime, @EndDate datetime
set @StartDate = '20120401 00:00:00'
set @EndDate = '20120430 23:59:59'
;WITH
mytablePlusHours As
(
SELECT *,
DATEPART(ww, [eci_date]) AS [dateWeek]
FROM [mytable]
)
, mytableHourGroups As
(
SELECT dateWeek,
[eci_country],
COUNT(*) As [Number_Country],
ROW_NUMBER() OVER(PARTITION BY dateWeek ORDER BY [eci_country])
As [Country_Rank]
FROM mytablePlusHours
GROUP BY dateWeek, [eci_country]
)
SELECT
dateWeek AS [eci_date],
[eci_country],
[Number_Country]
FROM mytableHourGroups WITH(NOLOCK)
WHERE [dateWeek] between @StartDate and @EndDate
ORDER BY [dateWeek], [Number_Country] DESC
我可以按天提取信息,但我不知道如何按周对其进行排序。有人可以告诉我我的代码哪里错了吗?谢谢!
我正在尝试显示我的数据,例如:
Week Country Count
16 United States 13
17 Canada 41