2017-03-14 14 views
0

最近、MySQLからVerticaに切り替えました。私は< = 30を再作成する方法について迷っています。これは現在、Verticaでは動作しませんが、MySQLでは動作します。MySqlからVerticaに切り替えると、where節が機能しない

基本的に、ユーザーは車を所有しており、車には部品があります。私は時間枠で車と車のパーツの合計を計算したいが、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' 

ご協力いただきありがとうございます。

ERROR: Correlated subquery with aggregate function COUNT is not supported

+0

これはVerticaでは機能しませんか? –

+0

どのように「機能しない」のですか?マウスは離れて飛ぶ?モニターが消えますか?消化不良ですか? –

+0

ハハ。情報の欠如に関するお詫び - 「エラー:集計関数COUNTを持つ相関サブクエリはサポートされていません」 – Grip55

答えて

0

あなたは、サブクエリこの方法を使用します。私のマウスは飛び去ると私のモニターがブランクになる前に

は、私はこのエラーを取得します。あなたは、ウィンドウ関数を使用します。

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'; 

注:

  • 一重引用符でカラムのエイリアスを囲まないでください。それは起こるのを待っているバグです。文字列と日付の定数には単一引用符を使用してください。
  • 日付ロジックを簡略化することができます。 <を使用すると、特定の日に発生したすべてをキャプチャするのには、<=よりも優れています。
+0

これは素晴らしい作品です!私はそのパーティションがトリックをしたのを見る...それを完全に理解するためにこれについてもう少し詳しく読む必要がある。これは正しい道に私を置く - 本当に感謝しています!そしてあなたのヒントは、 – Grip55

関連する問題