1

我想知道如何使用WHERE基于 a 的不同子句CASEor IF。我更喜欢 a CASE,因为该语句的其余部分很复杂,而且我不喜欢这种复杂性在两个地方只有微小差异的想法。但是,我知道案例仅用于值。我在下面复制了我的问题的一个简单版本。

基本上,我有三张桌子。第一个包含主信息(MasterTable)。第二个包含属于主表(Table1)的一对多关系。第三个是选择器列表,指示要在此实例中使用 Table1 中的哪些记录。我希望 Table2 的最新记录驱动从 Table1 中选择的内容,优先考虑 SubID 而不是 OrderNum。

主表 | MasterID,其他信息

表1 | T1UniqueId、MasterID、SubID、Text、OrderNum

表2 | T2UniqueId、MasterID、SubID、OrderNum、日期

SELECT MasterID, OtherInfo, SubID
FROM MasterTable
OUTER APPLY(
    SELECT TOP 1 SubID FROM Table1
    WHERE Table1.MasterID=MasterTable.MasterID
    CASE
        WHEN 
        (
            SELECT TOP 1 SubID FROM Table2 
            WHERE Table2.MasterID=MasterTable.MasterID 
            ORDER BY Date DESC
        ) Is NULL
        THEN Table1.OrderNum=
            (
                SELECT TOP 1 OrderNum 
                FROM Table2
                WHERE Table2.MasterId=MasterTable.MasterId
                ORDER BY Date DESC
            )
        ELSE Table1.SubId=
            (
                SELECT TOP 1 SubId
                FROM Table2
                WHERE Table2.MasterId=MasterTable.MasterId
                ORDER BY Date DESC
            )
    END

) SubData
4

2 回答 2

1

对此的快速重写将导致以下结果:

IF ((SELECT TOP 1 SubID FROM Table2 WHERE Table2.MasterID=MasterTable.MasterID ORDER BY Date DESC) IS NULL)
BEGIN
    SELECT
            MasterID, OtherInfo, SubID
        FROM MasterTable
        OUTER APPLY(
            SELECT TOP 1 SubID FROM Table1
            WHERE
                Table1.MasterID=MasterTable.MasterID
                AND Table1.OrderNum =
                (
                    SELECT TOP 1 OrderNum 
                    FROM Table2
                    WHERE Table2.MasterId=MasterTable.MasterId
                    ORDER BY Date DESC
                )
        ) SubData
END
ELSE
BEGIN
    SELECT
            MasterID, OtherInfo, SubID
        FROM MasterTable
        OUTER APPLY(
            SELECT TOP 1 SubID FROM Table1
            WHERE
                Table1.MasterID=MasterTable.MasterID
                AND Table1.SubId=
                (
                    SELECT TOP 1 SubId
                    FROM Table2
                    WHERE Table2.MasterId=MasterTable.MasterId
                    ORDER BY Date DESC
                )
        ) SubData
END

但正如你所说,这让它看起来很难看,因为你现在在两个地方都有这种复杂性......

我想你也可以这样表述(未经测试,但这应该将你的复杂逻辑保留在一个地方):

SELECT
        MasterID, OtherInfo, SubID
    FROM MasterTable
    OUTER APPLY(
        SELECT TOP 1 SubID FROM Table1
        WHERE Table1.MasterID=MasterTable.MasterID
        AND
        (
            (
                (
                    SELECT
                            TOP 1 SubID
                        FROM Table2 
                        WHERE Table2.MasterID=MasterTable.MasterID 
                        ORDER BY Date DESC
                ) IS NULL
                AND
                Table1.OrderNum =
                (
                    SELECT TOP 1 OrderNum 
                    FROM Table2
                    WHERE Table2.MasterId=MasterTable.MasterId
                    ORDER BY Date DESC
                )
            )
            OR
            (
                Table1.SubId =
                (
                    SELECT
                            TOP 1 SubId
                        FROM Table2
                        WHERE Table2.MasterId=MasterTable.MasterId
                        ORDER BY Date DESC
                )
            )
        )
    ) SubData
于 2013-09-27T15:05:16.810 回答
0

如果 Table1 和 Table2 中的 SubID 和 OrderNum 相同,您可以使用嵌套 select 语句的简单查询:

select m.MasterID, m.OtherInfo, (
  select top 1 coalesce(t2.SubID, t2.OrderNum) from Table2 t2
  where t2.MasterID = m.MasterID order by date desc
) as SubID
from MasterTable m;
于 2013-09-27T13:32:19.080 回答