我需要帮助创建一个查询,以便从早上9点到早上8点(第二天)对列进行排序。
我将2个查询联合在一起,使用单独的顺序和日期范围参数,并添加排名以对返回的行进行排序。但似乎在返回0到8个查询结果之前,查询返回9到23个查询的行。 unexpected result
我不知道如何处理这个,因为这是我第一次遇到这个复杂的查询。我已经阅读了一些有关此问题的主题,并提出了这段代码。
SELECT StationCode, Month, Day, Year, Hour, Rainfall
FROM
(SELECT 1 AS rank, StationCode, Month, Day, Year, Hour, Rainfall, DateValue(CStr([Month] & '/' & [Day] & '/' & [Year])) AS [Date]
FROM table1
WHERE (((StationCode)=10) AND ((Hour)>=9) AND ((DateValue(CStr([Month] & '/' & [Day] & '/' & [Year]))) Between DateValue([month1] & '/' & [day1] & '/' & [year1]) And DateValue([month2] & '/' & [day2] & '/' & [year2])))
ORDER BY Month, Day, Year, Hour
UNION
SELECT 2 AS rank, StationCode, Month, Day, Year, Hour, Rainfall, DateValue(CStr([Month] & '/' & [Day] & '/' & [Year])) AS [Date]
FROM table1
WHERE (((StationCode)=10) AND ((Hour)<=8) AND ((DateValue(CStr([Month] & '/' & [Day] & '/' & [Year]))) Between DateValue([month1] & '/' & [day1] & '/' & [year1]) And DateValue([month2] & '/' & [day2] & '/' & [year2])))
ORDER BY Month, Day, Year, Hour) dt
ORDER BY rank, Month, Day, Year
示例数据:
StationCode | Month | Day | Year | Hour | Rainfall
10 | 1 | 1 | 2010 | 0 | 0
10 | 1 | 1 | 2010 | 1 | 0
10 | 1 | 1 | 2010 | 2 | 0
10 | 1 | 1 | 2010 | 3 | 0
10 | 1 | 1 | 2010 | 4 | 0
10 | 1 | 1 | 2010 | 5 | 0
10 | 1 | 1 | 2010 | 6 | 0
10 | 1 | 1 | 2010 | 7 | 0
10 | 1 | 1 | 2010 | 8 | 0
10 | 1 | 1 | 2010 | 9 | 0
10 | 1 | 1 | 2010 | 10 | 0
10 | 1 | 1 | 2010 | 11 | 0
10 | 1 | 1 | 2010 | 12 | 0
10 | 1 | 1 | 2010 | 13 | 0
10 | 1 | 1 | 2010 | 14 | 0
10 | 1 | 1 | 2010 | 15 | 0
10 | 1 | 1 | 2010 | 16 | 0
10 | 1 | 1 | 2010 | 17 | 0
10 | 1 | 1 | 2010 | 18 | 0
10 | 1 | 1 | 2010 | 19 | 0
10 | 1 | 1 | 2010 | 20 | 0
10 | 1 | 1 | 2010 | 21 | 0
10 | 1 | 1 | 2010 | 22 | 0
10 | 1 | 1 | 2010 | 23 | 0
10 | 1 | 2 | 2010 | 0 | 0
10 | 1 | 2 | 2010 | 1 | 0
10 | 1 | 2 | 2010 | 2 | 0
10 | 1 | 2 | 2010 | 3 | 0
10 | 1 | 2 | 2010 | 4 | 0
10 | 1 | 2 | 2010 | 5 | 0
10 | 1 | 2 | 2010 | 6 | 0
10 | 1 | 2 | 2010 | 7 | 0
10 | 1 | 2 | 2010 | 8 | 0
10 | 1 | 2 | 2010 | 9 | 0
10 | 1 | 2 | 2010 | 10 | 0
so on...
预期结果:
StationCode | Month | Day | Year | Hour | Rainfall
10 | 1 | 1 | 2010 | 9 | 0
10 | 1 | 1 | 2010 | 10 | 0
10 | 1 | 1 | 2010 | 11 | 0
10 | 1 | 1 | 2010 | 12 | 0
10 | 1 | 1 | 2010 | 13 | 0
10 | 1 | 1 | 2010 | 14 | 0
10 | 1 | 1 | 2010 | 15 | 0
10 | 1 | 1 | 2010 | 16 | 0
10 | 1 | 1 | 2010 | 17 | 0
10 | 1 | 1 | 2010 | 18 | 0
10 | 1 | 1 | 2010 | 19 | 0
10 | 1 | 1 | 2010 | 20 | 0
10 | 1 | 1 | 2010 | 21 | 0
10 | 1 | 1 | 2010 | 22 | 0
10 | 1 | 1 | 2010 | 23 | 0
10 | 1 | 2 | 2010 | 0 | 0
10 | 1 | 2 | 2010 | 1 | 0
10 | 1 | 2 | 2010 | 2 | 0
10 | 1 | 2 | 2010 | 3 | 0
10 | 1 | 2 | 2010 | 4 | 0
10 | 1 | 2 | 2010 | 5 | 0
10 | 1 | 2 | 2010 | 6 | 0
10 | 1 | 2 | 2010 | 7 | 0
10 | 1 | 2 | 2010 | 8 | 0
提前谢谢。
答案 0 :(得分:0)
尝试在Hour
子句中添加order by
列
根据我的理解,第9行到第23行在“小时”列中,不会将其排序为未包含在order by
子句中。
请尝试以下订单:Year, Month, Day, Hour
数据将按类别排序 第1天为0-8 第1天9-23 第2天为0-8 第2天9-23
如果您想要day 1 and 0-8 hrs
,day 2 and 9-23 hrs
此外,内部查询中的order by
子句没有任何区别,因为您在外部查询中重新排序它
答案 1 :(得分:0)
您可以删除子查询中的order-by子句,它们完全没有效果,因为无论如何行都会在合并(union)之后进行排序。
您将排名作为第一个排序标准。由于您在第一个子查询中指定了rank = 1,因此所有这些记录首先出现。
尝试以下整体排序顺序:年,月,日,排名,小时
更新
根据您的评论,您似乎对选择条件有疑问,而不是排序顺序。
试试这个:
SELECT StationCode, Month, Day, Year, Hour, Rainfall
from table1
where StationCode=10
and DateAdd('h',-8,DateValue(CStr([Month] & '/' & [Day] & '/' & [Year]))
Between DateValue([month1] & '/' & [day1] & '/' & [year1])
And DateValue([month2] & '/' & [day2] & '/' & [year2])
order by StationCode, Year, Month, Day, Hour
这里的技巧是将时间戳移动8小时。