以下是我正在尝试的源数据获取最大计数以及相应的hour_id和id值。对于某个日期应该只有一行,现在有特定日期的多个值。
id hour_id count date
621f50772a36e7 23 14474 20141202
621f50157c2973 0 7190 20141203
621f5077582f54 7 5043 20141225
621f505247c107 11 5023 20141224
621f50251c8b33 10 4943 20141224
621f5076c9327b 18 4901 20150113
621f50044c300e 10 4868 20141212
621f500e10fa5d 12 4858 20141224
621f505242ec27 9 4843 20141224
621f505263bc56 14 4716 20141231
621f50774a456c 19 4712 20141206
621f5077414404 19 4674 20141123
621f5077362f46 19 4666 20141224
621f505246ea97 10 4662 20141225
621f50522c6bf5 13 4626 20141226
621f5076c87607 13 4586 20141231
621f5052297007 17 4574 20141224
我试过这个:
select max(count) cc
,partition_date
,location_id
,hour_id
from
(
select
l.location_id
, substr(x.evt_timestamp,9,2) as hour_id
, count(1) as count
,partition_date
from prismeventdetails x
join l_cellsite_location l
on x.evt_location = l.location_id
where x.evt_type = '100'
group by l.location_id
,hour_id
,partition_date
order by cc desc limit 500
) c
group by partition_date,location_id,hour_id
但无法获得所需的结果。 为一个日期获取多行。因为我已将hour_id列入分组 。我想要hour_id和id值,其中count是该日期的最大值。需要帮助,早期回应将不胜感激。
答案 0 :(得分:1)
以下是GROUPed和MAXed表与原始表的连接,应该是您想要的。
WITH maxed AS
(
SELECT max(count) AS max_count, date_
FROM tests.so_30595512
GROUP BY date_
)
SELECT maxed.date_, maxed.max_count, t2.hour, t2.id
FROM maxed
JOIN tests.so_30595512 AS t2
ON maxed.date_ = t2.date_
AND maxed.max_count = t2.count;
这是您在上面列出的表格上的一个查询,但同样的方法可以轻松转换为您在示例查询中使用的任何联接表格。
答案 1 :(得分:0)
尝试在下面将MAX移动到子查询:
select counts cc
,partition_date
,location_id
,hour_id
from
(
select
l.location_id
, substr(x.evt_timestamp,9,2) as hour_id
, max(count(1)) as counts
,partition_date
from prismeventdetails x
join l_cellsite_location l
on x.evt_location = l.location_id
where x.evt_type = '100'
group by partition_date, location_id, hour_id
order by cc desc limit 500
)c
答案 2 :(得分:0)
try this one:
select [count] cc
,partition_date
,location_id
,hour_id
from
(
select
l.location_id
, substr(x.evt_timestamp,9,2) as hour_id
, count(1) as [count]
,partition_date
from prismeventdetails x
join l_cellsite_location l
on x.evt_location = l.location_id
where x.evt_type = '100'
group by l.location_id
,hour_id
,partition_date
,ROW_NUMBER() over (ORDER BY [count] DESC) AS Number
order by cc desc limit 500
) c
WHERE Number = 1
group by partition_date,location_id,hour_id