我正在进行流量分析,并且我对流量的平均旅行时间进行了汇总查看。我要做的是选择与给定时间段的最大平均旅行时间相关的时间。
以下是我正在寻找的代码和结果,没有时间:
SELECT dt.datekey, dt.peak_hours, dt.weekday_name,
ROUND(MAX(avg_t_dur_traffic)/60,2) MaxATT
FROM cat.Analytics_AvgTimes att
INNER JOIN cat.Dim_Date dt on att.DateKey = dt.DateKey and att.timekey = dt.timekey
WHERE dt.DateKey = '20170523' AND dt.peak_hours <> 'off_peak'
GROUP BY dt.DateKey, dt.Peak_Hours, dt.Weekday_Name
datekey peak_hours weekday_name MaxATT
2017-05-23 AM_Peak Tuesday 28.93
2017-05-23 Midday_Peak Tuesday 14.05
2017-05-23 PM_Peak Tuesday 29.95
编辑:我没有专门针对这三个MaxATT时间,我只是寻求有关如何查询信息的帮助...
我在周二的AM高峰时段看到最大平均行程时间(MaxATT)。 29分钟当我加入时,我很难过。我得到以下结果:
请注意我已从结果中删除了几行以节省空间
SELECT dt.datekey, dt.TimeKey, dt.peak_hours, dt.weekday_name,
ROUND(MAX(avg_t_dur_traffic)/60,2) MaxATT
FROM cat.Analytics_AvgTimes att
INNER JOIN cat.Dim_Date dt on att.DateKey = dt.DateKey and att.timekey = dt.timekey
WHERE dt.DateKey = '20170523' and dt.peak_hours <> 'off_peak'
GROUP BY dt.DateKey, dt.Peak_Hours, dt.Weekday_Name, dt.TimeKey
datekey TimeKey peak_hours weekday_name MaxATT
2017-05-23 05:15:00 AM_Peak Tuesday 10.43
2017-05-23 05:30:00 AM_Peak Tuesday 10.45
2017-05-23 07:15:00 AM_Peak Tuesday 12.53
2017-05-23 07:30:00 AM_Peak Tuesday 18.27
2017-05-23 07:45:00 AM_Peak Tuesday 22.85
2017-05-23 08:00:00 AM_Peak Tuesday 28.93
在AM峰值期间,我们可以看到与MaxATT = 28.93相关的时间是08:00。但我想要选择的是每个峰值期间的每个不同时间, 所以我要找的是
datekey TimeKey peak_hours weekday_name MaxATT
2017-05-23 08:00:00 AM_Peak Tuesday 28.93
2017-05-23 13:45:00 Midday_Peak Tuesday 14.05
2017-05-23 17:30:00 PM_Peak Tuesday 29.95
这是尝试使用RANK(),但我失败了,因为MaxATT与正确的值不匹配,它拉的记录多于RANK()= 1
SELECT id, datekey, peak_hours, weekday_name, MaxAtt
FROM(
SELECT id, dt.DateKey, dt.Peak_Hours, dt.Weekday_Name,
ROUND(MAX(avg_t_dur_traffic)/60,2) MaxAtt,
RANK() OVER(PARTITION BY id ORDER BY MAX(avg_t_dur_traffic) DESC) AS
[rank]
FROM cat.Analytics_AvgTimes att
INNER JOIN cat.Dim_Date dt ON att.datekey = dt.DateKey AND att.TimeKey = dt.TimeKey
GROUP BY id, dt.DateKey, dt.Peak_Hours, dt.Weekday_Name
) mt
WHERE mt.[rank] = 1 AND DateKey = '20170523' AND peak_hours <> 'off_peak'
GROUP BY id, datekey, peak_hours, weekday_name, MaxAtt
感谢您的帮助,如果您需要进一步说明,请随时询问!
答案 0 :(得分:2)
使用子查询查找所需的maxatt行,并将它们连接到原始表。
select a.*
from cat.Analytics_AvgTimes a
Inner JOIN (
select datekey, peak_hours, weekday_name, max(maxatt) as maxatt
from cat.Analytics_AvgTimes
group by datekey, peak_hours, weekday_name) b
ON a.datekey = b.datekey and a.peak_hours = b.peak_hours and
a.weekday_name = b.weekday_name and a.maxatt = b.maxatt
小提琴示例http://sqlfiddle.com/#!9/dcacb9/1/0
审核数据后进行编辑:
select one.*
from (select a.*, b.Avg_T_Dur_Traffic as max
from dim_date a, Analytics_AvgTimes b
where a.datekeyid = b.id) one
INNER JOIN (
select a.*, max(b.Avg_T_Dur_Traffic) as max
from dim_date a, Analytics_AvgTimes b
where a.datekeyid = b.id
group by datekey, monthid, weekid, dayid, peakhoursid) two
ON one.datekey = two.datekey and one.monthid = two.monthid and
one.weekid = two.weekid and one.dayid = two.dayid and one.max = two.max