我正在努力写一点写SQL查询的最有效方法,这消除了每个人的max(day)。我尝试了where where day < max(day)
,但我们的hadoop环境不允许这样做。
基本上,目标是选择一个在过去570天内切换到同一供应商的手机类型相同的人。
对查询的table1
部分有任何建议吗?
with table2 as
(select listener_id, device_id, max(day) day from
devicetable b
where vendor_id = 42
and category = 'something'
group by listener_id, device_id, day) -- max day for each person
,table1 as
(select listener_id, device_id, ROW_NUMBER () over (PARTITION BY listener_id, device_id order by day desc) rowno from
(select listener_id, device_id, day from devicetable
where vendor_id=42 and category = 'something'
group by listener_id, device_id, day)
where rowno <> 1)
insert into finaltable
select a.listener_id
from table1 a
left join
table2 b
on a.listener_id = b.listener_id
where datediff (a.day, b.day) <=570 and a.day <= b.day -- setting the difference
and a.device_id <> b.device_id
and b.listener_id is not null; ```
答案 0 :(得分:0)
仅是部分代码的第一个建议
如果您想要最多的日期,则不要在分组依据中提及日期
select listener_id, device_id, max(day) day
from devicetable b
where vendor_id = 42
and category = 'something'
group by listener_id, device_id
是引用数据库列还是引用max的别名
答案 1 :(得分:0)
尝试这样的事情:
SELECT listener_id, device_id, day FROM devicetable AS A
LEFT OUTER JOIN (SELECT listener_id, Max(Day) AS MaxDate FROM devicetable GROUP BY listener_id) AS B ON A.listener_id=B.listener_id AND A.day=B.MaxDate
WHERE B.listener_id IS NULL
它将返回所有行,但不包括每个侦听器具有最大日期的行。