0

次のようなクエリを書くと:

with foo as (
    select
        'foo' as name,
        1 as value
    union all select
        'foo', 2
    union all select
        'foo', 3
    union all select
        'foo', 4
), bar as (
    select
        'bar' as name,
        3 as value
    union all select
        'bar', 4
    union all select
        'bar', 5
    union all select
        'bar', 6
)
select
    f.name,
    b.value,
    b.name
from
    foo as f
full outer join
    bar as b
    on b.value = f.value;

私はこれを得る:

name        value    name
foo         (null)  (null)
foo         (null)  (null)
foo         3       bar
foo         4       bar
(null)      5       bar
(null)      6       bar

これは私が期待するものです。しかし、次のようなテーブル値関数で同様のことをしようとすると:

with allSizes as (
        select cast('120X60' as character varying) as size
        union all select '160X600'
        union all select '1X1'
        union all select '300X250'
        union all select '728X90'
        union all select '88X32'
        union all select 'PEEL'
)
select
        msd.region,
        msd.market,
        s.size,
        msd.target,
        msd.actual
from 
        marketdata('2013-01-05') as msd
full outer join
        allSizes as s
        on s.size = msd.size
where
        msd.market = 'Foo, USA';

whereはテーブル値関数ですが、関数に存在しないmarketdata()列に対応する空の行は取得されません。なぜだめですか?sizemarketdata()

4

1 に答える 1

3

その理由は次のとおりです。

where
        msd.market = 'Foo, USA';

を生成し、full outer joinに存在しない行を除外します( =marketdata()があるため)msd.marketNULL

クエリを次のように変更できます。

with allSizes as (
        select cast('120X60' as character varying) as size
        union all select '160X600'
        union all select '1X1'
        union all select '300X250'
        union all select '728X90'
        union all select '88X32'
        union all select 'PEEL'
)
select
        msd.region,
        msd.market,
        s.size,
        msd.target,
        msd.actual
from 
        (select marketdata('2013-01-05')
         where  market = 'Foo, USA') as msd
full outer join
        allSizes as s
        on s.size = msd.size;

または単に書き直す

where
        msd.market = 'Foo, USA' OR msd.market IS NULL;

Ifmarketdata()は を含む行を返しませんmsd.market IS NULL

于 2013-01-08T16:50:52.523 に答える