SQL中的自定义订单依据

时间:2019-06-23 18:06:39

标签: sql sql-server

我编写了一个查询,以获取每小时每小时列ID的总和。小时值是根据称为created_at的表中的日期字段计算得出的。我使用了if案例,以更好地理解小时值结果。但是,当我按小时排序(现在是用例后的字符串)时,我没有得到最好的结果。我想按实际小时而不是按字符串版本订购。我该怎么办?

我尝试使用

order by case when DATEPART(hour,created_at) = 0 then 0
when DATEPART(hour,x.created_at) = 1 then 1
when DATEPART(hour,x.created_at) = 2 then 2
when DATEPART(hour,x.created_at) = 3 then 3
when DATEPART(hour,x.created_at) = 4 then 4
when DATEPART(hour,x.created_at) = 5 then 5
when DATEPART(hour,x.created_at) = 6 then 6
when DATEPART(hour,x.created_at) = 7 then 7
when DATEPART(hour,x.created_at) = 8 then 8
when DATEPART(hour,x.created_at) = 9 then 9
when DATEPART(hour,x.created_at) = 10 then 10
when DATEPART(hour,x.created_at) = 11 then 11
when DATEPART(hour,x.created_at) = 12 then 12
when DATEPART(hour,x.created_at) = 13 then 13
when DATEPART(hour,x.created_at) = 14 then 14
when DATEPART(hour,x.created_at) = 15 then 15
when DATEPART(hour,x.created_at) = 16 then 16
when DATEPART(hour,x.created_at) = 17 then 17
when DATEPART(hour,x.created_at) = 18 then 18
when DATEPART(hour,x.created_at) = 19 then 19
when DATEPART(hour,x.created_at) = 20 then 20
when DATEPART(hour,x.created_at) = 21 then 21
when DATEPART(hour,x.created_at) = 22 then 22
when DATEPART(hour,x.created_at) = 23 then 23
end

仅会收到一个错误,表明created_at必须按查询分组。

以下是我需要修改的原始查询。

select case 
    when DATEPART(hour,created_at) = 0 then '12 AM to 1 AM'
    when DATEPART(hour,created_at) = 1 then '1 AM to 2 AM'
    when DATEPART(hour,created_at) = 2 then '2 AM to 3 AM'
    when DATEPART(hour,created_at) = 3 then '3 AM to 4 AM'
    when DATEPART(hour,created_at) = 4 then '4 AM to 5 AM'
    when DATEPART(hour,created_at) = 5 then '5 AM to 6 AM'
    when DATEPART(hour,created_at) = 6 then '6 AM to 7 AM'
    when DATEPART(hour,created_at) = 7 then '7 AM to 8 AM'
    when DATEPART(hour,created_at) = 8 then '8 AM to 9 AM'
    when DATEPART(hour,created_at) = 9 then '9 AM to 10 AM'
    when DATEPART(hour,created_at) = 10 then '10 AM to 11 AM'
    when DATEPART(hour,created_at) = 11 then '11 AM to 12 PM'
    when DATEPART(hour,created_at) = 12 then '12 PM to 1 PM'
    when DATEPART(hour,created_at) = 13 then '1 PM to 2 PM'
    when DATEPART(hour,created_at) = 14 then '2 PM to 3 PM'
    when DATEPART(hour,created_at) = 15 then '3 PM to 4 PM'
    when DATEPART(hour,created_at) = 16 then '4 PM to 5 PM'
    when DATEPART(hour,created_at) = 17 then '5 PM to 6 PM'
    when DATEPART(hour,created_at) = 18 then '6 PM to 7 PM'
    when DATEPART(hour,created_at) = 19 then '7 PM to 8 PM'
    when DATEPART(hour,created_at) = 20 then '8 PM to 9 PM'
    when DATEPART(hour,created_at) = 21 then '9 PM to 10 PM'
    when DATEPART(hour,created_at) = 22 then '10 PM to 11 PM'
    when DATEPART(hour,created_at) = 23 then '11 PM to 12 AM'
    end as hour_of_day, count(id) as count_total 
from myTable 
where  CAST(created_at AS DATE) = CAST((getdate()-1) AS DATE) 
group by 1

结果应按以下顺序排序-

hour_of_day      count_total
12 AM to 1 AM    1
1 AM to 2 AM     20
2 AM to 3 AM     23
...
6PM to 7 PM      9
...
11PM to 12AM     78

2 个答案:

答案 0 :(得分:3)

我相信您的dbms是SQL Server,因此请使用执行分组的CTE,然后从cte中选择:

with cte as (
  select  
    DATEPART(hour, created_at) as hourofday, 
    count(id) as count_total from myTable 
  where CAST(created_at AS DATE) = CAST((getdate()-1) AS DATE) 
  group by DATEPART(hour,created_at)
)
select case 
when hourofday = 0 then '12 AM to 1 AM'
when hourofday = 1 then '1 AM to 2 AM'
when hourofday = 2 then '2 AM to 3 AM'
when hourofday = 3 then '3 AM to 4 AM'
when hourofday = 4 then '4 AM to 5 AM'
when hourofday = 5 then '5 AM to 6 AM'
when hourofday = 6 then '6 AM to 7 AM'
when hourofday = 7 then '7 AM to 8 AM'
when hourofday = 8 then '8 AM to 9 AM'
when hourofday = 9 then '9 AM to 10 AM'
when hourofday = 10 then '10 AM to 11 AM'
when hourofday = 11 then '11 AM to 12 PM'
when hourofday = 12 then '12 PM to 1 PM'
when hourofday = 13 then '1 PM to 2 PM'
when hourofday = 14 then '2 PM to 3 PM'
when hourofday = 15 then '3 PM to 4 PM'
when hourofday = 16 then '4 PM to 5 PM'
when hourofday = 17 then '5 PM to 6 PM'
when hourofday = 18 then '6 PM to 7 PM'
when hourofday = 19 then '7 PM to 8 PM'
when hourofday = 20 then '8 PM to 9 PM'
when hourofday = 21 then '9 PM to 10 PM'
when hourofday = 22 then '10 PM to 11 PM'
when hourofday = 23 then '11 PM to 12 AM'
end as hour_of_day, 
count_total 
from cte
order by hourofday

答案 1 :(得分:2)

最可能的解决方案是:

order by min(x.created_at)

如果最少created_at分散在多天内,则可能无法正常工作。这肯定可以工作:

order by min(datepart(hour, x.created_at))