使用与小型精品店相关的虚拟数据我试图使用3个月内收集的数据计算出一天中哪个小时最繁忙。我使用SQL来构建我的查询,我不知道从哪里开始,我已经找到了最忙碌的一天,但我正在努力利用时间。以下是我的数据示例,我还在努力构建查询:
SALE_ID PRODUCT_NAME PRICE_RANGE PRODUCT_COST DATEOF DAY_OF_WEEK TIME
1 DRESS HIGH 69 2/18/2017 3 13:43
2 TRAINERS MEDIUM 48 1/1/2017 3 9:21
5 TROUSERS HIGH 70 3/2/2017 4 9:20
6 TROUSERS MEDIUM 30 3/30/2017 4 17:00
7 SHORTS LOW 23 3/27/2017 4 16:25
8 TROUSERS MEDIUM 53 3/28/2017 4 9:00
10 TROUSERS MEDIUM 32 3/3/2017 4 9:40
11 SHIRT HIGH 88 3/12/2017 4 9:50
9 TRAINERS MEDIUM 36 3/31/2017 4 9:30
12 TRAINERS HIGH 100 1/20/2017 4 9:10
13 DRESS MEDIUM 48 2/26/2017 4 9:40
14 SHOES MEDIUM 35 1/31/2017 4 10:41
16 TROUSERS MEDIUM 30 2/27/2017 4 10:19
17 TROUSERS LOW 21 3/10/2017 4 9:15
18 DRESS HIGH 92 1/23/2017 4 10:24
19 SHIRT HIGH 84 3/6/2017 4 9:05
20 TRAINERS LOW 27 1/21/2017 4 10:26
21 TROUSERS HIGH 88 2/27/2017 4 10:55
23 HAT MEDIUM 34 1/25/2017 4 12:19
25 SHOES LOW 22 1/6/2017 4 12:30
26 BELT HIGH 86 2/25/2017 4 12:45
27 DRESS HIGH 84 3/26/2017 4 9:50
28 JACKET HIGH 99 2/20/2017 4 12:45
29 SHIRT MEDIUM 48 2/21/2017 4 13:00
31 TRAINERS HIGH 92 2/3/2017 4 12:25
32 BELT MEDIUM 84 3/27/2017 4 10:10
33 TRAINERS LOW 27 2/12/2017 3 13:29
34 SHORTS HIGH 88 1/18/2017 4 16:06
35 HAT HIGH 87 2/9/2017 4 14:26
答案 0 :(得分:0)
如果DATEOF
是DATE
列并且有时间组件,则可以按小时组件进行分组。
select hour_of_day , tph
FROM
(
select TO_CHAR(dateof,'HH24') hour_of_day,count(*) tph
FROM
salestable
GROUP BY TO_CHAR(dateof,'HH24')
ORDER BY tph desc
) where ROWNUM = 1 ;
如果您没有时间组件且需要使用time
列,则可以使用SUBSTR
和INSTR
从time
中提取小时。< / p>
select hour_of_day , tph
FROM
(
select SUBSTR(time,1,INSTR(time,':')-1) hour_of_day,count(*) tph
FROM salestable GROUP BY SUBSTR(time,1,INSTR(time,':')-1)
ORDER BY tph desc
) where ROWNUM = 1 ;