2017-04-20 52 views
0

我想將三個單獨的查詢合併爲一個,並且仍然產生相同的結果,但是作爲單個表。 ColumnA和ColumnB實際上都是'yyyy-mm-dd'的日期格式,理想情況下最終結果只是日期列和每個查詢的單獨計數。基於單獨的組列和條件計數

select columnA, count(*) 
from data.table 
where timestamp between '2017-01-01' and '2017-01-07' 
group by columnA 

select columnB, count(*) 
from data.table 
where timestamp between '2017-01-01' and '2017-01-07' 
group by columnB 

select columnB, count(distinct columnC) 
from data.table 
where timestamp between '2017-01-01' and '2017-01-07' 
and columnX in ('itemA','ItemB') 
group by columnB 
+0

看起來像一個教科書用例'聯盟所有' –

+0

雖然在反思我認爲戈登已經更好地理解。 –

回答

1

圍棋與UNION ALL

select columnA, count(*) 
from data.table 
where timestamp between '2017-01-01' and '2017-01-07' 
group by columnA 
UNION ALL 
select columnB, count(*) 
from data.table 
where timestamp between '2017-01-01' and '2017-01-07' 
group by columnB 
UNION ALL 
select columnB, count(distinct columnC) 
from data.table 
where timestamp between '2017-01-01' and '2017-01-07' 
and columnX in ('itemA','ItemB') 
group by columnB 
1

以下查詢表達你想要做什麼:

select d.dte, coalesce(a.cnt, 0) as acnt, coalesce(b.cnt, 0) as bcnt, 
     b.c_cnt 
from (select columnA as dte from data.table where timestamp between '2017-01-01' and '2017-01-07' 

     union 
     select columnB from data.table where timestamp between '2017-01-01' and '2017-01-07' 
    ) d left join 
    (select columnA, count(*) as cnt 
     from data.table 
     where timestamp between '2017-01-01' and '2017-01-07' 
     group by columnA 
    ) a 
    on d.dte = a.columnA left join 
    (select columnB, count(*) as cnt, 
      count(distinct case when columnX in ('itemA','ItemB') then columnC end) as c_cnt 
     from data.table 
     where timestamp between '2017-01-01' and '2017-01-07' 
     group by columnB 
    ) b 
    on d.dte = b.columnB; 

我認爲這是蜂巢兼容的,但偶爾的蜂巢具有驚人的偏差SQL的其他方言。

1

下,似乎是你想要什麼:

select columnA, count(*) as cnt from data.table where timestamp between '2017-01-01' and '2017-01-07' group by columnA 
Union All 
select columnB, count(*) as cnt from data.table where timestamp between '2017-01-01' and '2017-01-07' group by columnB 
Union All 
select columnB, count(distinct columnC) as cnt from data.table where timestamp between '2017-01-01' and '2017-01-07' and columnX in ('itemA','ItemB') group by columnB 
0

我能夠得到它使用下面的方法來工作:

With pullA as 
(
    select columnA, count(*) as A_count 
    from data.table 
    group by columnA 
), 
pullB as 
(
    select columnB, count(*) as B_count 
    from data.table 
    group by columnB 
), 

pullC as 
(
    select columnB , count(*) as C_count 
    from data.table 
    where columnX in ('itemA', 'itemB') 
    group by columnB 
) 

select ColumnB, A_count, B_count, C_count 
from pullB 
left join pullA 
on ColumnB = ColumnA 
left join pullC 
on ColumnB = ColumnC 

是這種方法比任何或多或少效率聯盟還是子查詢方法?