9

この結合操作を実行しようとしています。私はSQLが初めてなので、構文などを理解するのに問題があります。

次のクエリのどこが間違っていると思いますか。

select top 1 * 
from 
    (select * 
     from dbo.transaction_unrated 
        where transaction_date >= '2012/05/01' 
            and transaction_date < '2012/06/01' 
            and content_provider_code_id in (1)
    )   FULL OUTER JOIN 
    (select * 
     from dbo.transaction_rated 
        where transaction_date >= '2012/05/01' 
            and transaction_date < '2012/06/01' 
            and entity_id in (1) 
            and mapping_entity_id = 1)
    ) 
    ON dbo.transaction_unrated.cst_id = dbo.transaction_rated.unrated_transaction_id
4

1 に答える 1

12

派生テーブルに別名を付ける必要があります。

select top 1 * 
from 
(
     select * 
     from dbo.transaction_unrated 
        where transaction_date >= '2012/05/01' 
            and transaction_date < '2012/06/01' 
            and content_provider_code_id in (1)
) rsQuery1  
FULL OUTER JOIN 
(
     select * 
     from dbo.transaction_rated 
        where transaction_date >= '2012/05/01' 
            and transaction_date < '2012/06/01' 
            and entity_id in (1) 
            and mapping_entity_id = 1)
) rsQuery2 ON rsQuery1.cst_id = rsQuery2.unrated_transaction_id

FULL OUTER JOINも珍しい(私の経験では)。それがあなたが望むものだと確信していますか?通常INNER JOIN、両方のテーブルで条件に一致する行を戻す を実行するか、1 つのテーブルをドライバーにして、一致するかどうかに関係なく、駆動テーブルのすべての行を戻すLEFTorを実行します。RIGHT OUTER JOIN他のテーブル。AFULL OUTER JOINは、一致するかどうかに関係なく、両方のテーブルのすべての行を戻します。

于 2012-07-23T21:00:53.927 に答える