1

私はこの3つのクエリ結果を持っています

  z_id  |    data    | c_email_i
--------+------------+-----------
 276050 | 2012-03-30 |         1
 319536 | 2012-07-23 |         1
 190033 | 2010-01-18 |         1
 271987 | 2011-11-02 |         1
 319554 | 2012-08-21 |         1
 370881 | 2013-06-18 |         1

  z_id  |    data    | c_call_o
--------+------------+----------
 169456 | 2009-09-07 |        1
 148231 | 2009-09-25 |        2
 240949 | 2010-10-25 |        2
 119116 | 2009-01-09 |        2
 219206 | 2010-09-29 |        1
 243121 | 2010-11-02 |        1

  z_id  |    data    | c_call_i
--------+------------+----------
 169456 | 2009-09-07 |        1
 148231 | 2009-09-25 |        2
 240949 | 2010-10-25 |        2
 119116 | 2009-01-09 |        2
 219206 | 2010-09-29 |        1
 243121 | 2010-11-02 |        1

そして、私はそれに参加してこのような結果になりたい

 z_id |       data       | c_email_i | c_call_o | c_call_i
------+------------------+-----------+----------+----------
119116|     2009-01-09   |         0 |        2 |        2
169456|     2009-09-07   |         0 |        1 |        1
276050|     2012-03-30   |         1 |        0 |        0
...........

UNION を使用しようとしましたが、(fake_column として 0) の列が多すぎて、気に入らなかったのです。(でも本当に速かった)

反対側では、(日付と z_id テーブルで) 左結合を使用すると、800k の結果で 40 分ほどかかるクエリがあります。

4

2 に答える 2

1

次の 2 つの方法があります。

3 つのクエリをすべて UNION し、GROUP BY を実行します。

select z_id, data, max(c_email_i), nax(c_call_o), max(c_call_i)
from
 (
   select z_id, data, c_email_i, null as c_call_o, null as c_call_i from...
   union all
   select z_id, data, null, c_call_o, null as c_call_i from...
   union all
   select z_id, data, null, null, c_call_i from...
 ) dt
group by z_id, data

または、クエリを派生テーブルに入れて結合しますが、同じ z_id/data 行を返さない場合、多くの COALESCE で見苦しくなります:

select
   coalesce(a.z_id, b.z_id, c.z_id),
   coalesce(a.data, b.data, c.data),
   coalesce(a.c_email_i, 0),
   coalesce(b.c_call_o, 0),
   coalesce(c.c_call_i, 0) 
from...
  (select ...) a
full outer join 
  (select ...) b
on a.z_id = b.z_id and a.data = b.data 
full outer join 
  (select ...) c
on coalesce(a.z_id,b.z_id) = c.z_id 
and coalesce(a.data,b.data) = c.data 
于 2013-09-16T07:16:51.563 に答える
0

を使用したくない場合はnull as ...、次のクエリを使用できます。

with cte as (
    select 'c_email_i' as type, z_id, data, c_email_i as value from ...
    union all
    select 'c_call_o' as type, z_id, data, c_call_o as value from ...
    union all
    select 'c_call_i' as type, z_id, data, c_call_i as value from ...
)
select
    z_id, data,
    max(case when type = 'c_email_i' then value end) as c_email_i,
    max(case when type = 'c_call_o' then value end) as c_call_o,
    max(case when type = 'c_call_i' then value end) as c_call_i
from cte
group by z_id, data

またはサブクエリで:

select
    z_id, data,
    max(case when type = 'c_email_i' then value end) as c_email_i,
    max(case when type = 'c_call_o' then value end) as c_call_o,
    max(case when type = 'c_call_i' then value end) as c_call_i
from (
    select 'c_email_i' as type, z_id, data, c_email_i as value from ...
    union all
    select 'c_call_o' as type, z_id, data, c_call_o as value from ...
    union all
    select 'c_call_i' as type, z_id, data, c_call_i as value from ...
) as c
group by z_id, data
于 2013-09-16T09:11:30.240 に答える