0
Table 1 - Deal ID, REF NOS, Type, Papa ID

Table 2 - Deal ID, Type

Method used という名前の新しいビューで列を作成します。フィールドの設定方法は次のとおりです(4条件)。

If Deal ID from table 1 Exists in Table 2 and Type is not Null from Table 2. 
Set Method used to be Y

If Deal ID does not exist in Table 1 and Type does not contain 27,42 or 55 in Table 1.
Set Method used to be Y

If Papa ID is null from Table 1, and Type does not contain 27,42 or 55 in Table 1.
Set Method used to be Y

Else 

Set to N

それを始めて、すごい!と思った..

create view Master as ( 

select Deal ID, REF NOS, Type, Papa ID

[Method used]=
        Case
            When


from Table 1 A 
)
4

1 に答える 1

1

このようなものが機能する可能性があります (これらのテーブルが DealId に参加すると仮定します)。質問で示した列名の一部からスペースを削除したことに注意してください。

これらのテーブルを考えると:

CREATE TABLE #Table1 (DealId INT, RefNos VARCHAR(100), [Type] VARCHAR(100), PapaId INT);
CREATE TABLE #Table2 (DealId INT, [Type] VARCHAR(100));

表示例:

WITH DealIds AS (
    SELECT DealId FROM #Table1
    UNION 
    SELECT DealId FROM #Table2
)
SELECT 
CASE 
    -- If Deal ID from table 1 Exists in Table 2 and Type is not Null from Table 2. 
    -- Set Method used to be Y
    WHEN t1.DealId IS NOT NULL AND t2.DealId IS NOT NULL AND t2.[Type] IS NOT NULL THEN 'Y'

    -- If Deal ID does not exist in Table 1 and Type does not contain 27,42 or 55 in Table 1.
    -- Set Method used to be Y
    -- Note: it is is redundant to have the type condition if DealId is the PK.
    WHEN t1.DealId IS NULL AND t1.[Type] NOT IN (27, 42, 55) THEN 'Y'

    -- If Papa ID is null from Table 1, and Type does not contain 27,42 or 55 in Table 1.
    -- Set Method used to be Y
    WHEN t1.PapaId IS NULL AND t1.[Type] NOT IN (27,42, 55) THEN 'Y'

    -- Else 
    -- Set to N
    ELSE 'N' 
END AS MethodUsed 
FROM DealIds d
LEFT JOIN #Table1 t1 ON d.DealId = t1.DealId
LEFT JOIN #Table2 t2 ON d.DealId = t2.DealId
于 2012-05-22T19:03:43.083 に答える