2017-06-15 8 views
0

に参加毎日 複数のカウントを実行していると私は、パーティション内の行をカウントすることにより、私のデータにいくつかのスポットチェックをしようと、私は日ごとに見る「を使用します」の量をカウントし、私が見た値の量をカウントしていた結果に

は私が前に仕事するために、次のクエリの以前のバージョンを入手することができましたが、私はそれを実現することなく、何かを変更している必要があります。

src as 
(
    select partition_date_column, count(*) as src_row_count 
    from database.table 
    where partition_date_column > '2016-01-01' 
    group by partition_date_column 
) 

, 
pst as 
(
    select timestamp_pst as datevalue, count(*) as timestamp_row_count 
    from database.table 
    where partition_date_column > '2016-01-01' 
    and timestamp_pst between '2016-01-01' and '2017-07-01' 
    group by timestamp_pst 
), 

users as 
(
    select timestamp_pst as user_datevalue, count(*) as user_count 
    from database.table 
    where partition_date_column > '2016-01-01' 
    and timestamp_pst between '2016-01-01' and '2017-07-01' 
    and filter_column in ('filterA', 'filterB') 
    group by timestamp_pst 
) 

select datevalue as dayval, src_row_count, timestamp_row_count, user_count 
from pst 
left join src 
on datevalue = partition_date_column 
left join users 
on datevalue = user_datevalue 
order by dayval; 

私はハイブを引き起こすために作ったもの書式設定のエラーに関しては明確ではありませんよこれを認識しない。私はまた、1つが別の列にグループ化されていても、これらの3つの項目を数えるより良い方法があると感じています。

+0

次のエラーを取得していますか?何をしていないのですか?サンプルデータと必要な結果を表示します。 –

答えて

0

私はそれを理解しました。私は、このような複数のselect文が許されるコードの冒頭に "WITH"がありませんでした。

With src as 
(
    select partition_date_column, count(*) as src_row_count 
    from database.table 
    where partition_date_column > '2016-01-01' 
    group by partition_date_column 
) 

, 
pst as 
(
    select timestamp_pst as datevalue, count(*) as timestamp_row_count 
    from database.table 
    where partition_date_column > '2016-01-01' 
    and timestamp_pst between '2016-01-01' and '2017-07-01' 
    group by timestamp_pst 
), 

users as 
(
    select timestamp_pst as user_datevalue, count(*) as user_count 
    from database.table 
    where partition_date_column > '2016-01-01' 
    and timestamp_pst between '2016-01-01' and '2017-07-01' 
    and filter_column in ('filterA', 'filterB') 
    group by timestamp_pst 
) 

select datevalue as dayval, src_row_count, timestamp_row_count, user_count 
from pst 
left join src 
on datevalue = partition_date_column 
left join users 
on datevalue = user_datevalue 
order by dayval; 
0
select  pe.val as dt 

      ,count(case when pe.pos = 0 then 1 end) as src_row_count 

      ,count 
      (
       case 
        when pe.pos = 1 
         and pe.val between date '2016-01-01' and date '2017-07-01' 
        then 1 
       end 
      ) as timestamp_row_count 

      ,count 
      (
       case 
        when pe.pos = 1 
         and pe.val between date '2016-01-01' and date '2017-07-01' 
         and filter_column in ('filterA', 'filterB') 
        then 1 
       end 
      ) as user_count 

from  database.table t 
      lateral view posexplode (array(partition_date_column,timestamp_pst)) pe 

where  partition_date_column > date '2016-01-01' 

group by pe.val 
+0

これは、より良い解決策であり、パフォーマンスが賢明です。 –

関連する問題