最近我们从MySQL转到Vertica。在下面的查询中,我不知道如何在where子句中重新创建<=30检查。目前,这在Vertica中不起作用,但在MySQL中起作用。
从本质上说,用户拥有汽车,汽车也有零部件。我想在一个时间范围内完成汽车和汽车零部件的总量,但只适用于少于或等于30辆汽车的用户。
select
count(distinct cr.id) as 'Cars',
count(distinct cp.id) as 'Car Parts'
from
users u
inner join
user_emails ue on u.id = ue.user_id
inner join
cars cr on cr.user_id = u.id
inner join
car_parts cp on cp.car_id = cr.id
where
(
select count(*) from cars where cars.user_id=u.id
) <=30
and
ue.is_real = true and ue.is_main = true
and
cr.created_at >= '2017-01-01 00:00:00' and cr.created_at <= '2017-02-17 23:59:59'任何帮助或指导都是非常感谢的!
在我的鼠标飞走,显示器变成空白之前,我得到了以下错误:
错误:不支持关联子查询和聚合函数计数。
发布于 2017-03-14 23:28:25
您将以这种方式使用子查询。您可以使用一个窗口函数:
select count(distinct cr.id) as Cars,
count(distinct cp.id) as CarParts
from users u join
user_emails ue
on u.id = ue.user_id join
(select cr.*, count(*) over (partition by user_id) as cnt
from cars cr
) cr
on cr.user_id = u.id join
car_parts cp
on cp.car_id = cr.id
where cr.cnt <= 30 and
ue.is_real = true and ue.is_main = true
cr.created_at >= '2017-01-01' and
cr.created_at < '2017-02-18';备注:
<比<=更好地捕捉特定一天发生的一切。https://stackoverflow.com/questions/42798344
复制相似问题