使用 TOP 子句更新长 SQL 查询

Update long SQL query with TOP clause

我不熟悉 sql 服务器。我从其他技术人员那里得到了很长的查询。我需要使用 top 子句更新此查询以过滤前 10 个结果。我在第一个 SELECT 之后添加了 TOP 10,但没有按预期工作。

SELECT SalesTranHeader.id, CustID, OrgName, BillNo, TranDate, TranDesc, db = CASE
            WHEN trancode = 'T5'
            OR trancode LIKE 'F2'
            OR trancode LIKE 'F3'
            OR trancode LIKE 'F4'
            OR TranCode LIKE 'F11' THEN
                0
            ELSE
                [amount] - [OtherTranAmount] - [HignTemp] - [Penalty]
            END,
             cr = CASE
            WHEN [trancode] LIKE 't5'
            OR [trancode] LIKE 'F2'
            OR [trancode] LIKE 'F3'
            OR [trancode] LIKE 'F4'
            OR [trancode] LIKE 'F11' THEN
                [amount] - [OtherTranAmount] - [higntemp] - [Penalty]
            ELSE
                0
            END,
             ' ' AS bankID,
             [trancode]
            FROM
                salesTranHeader
            WHERE
                id <> 0
            AND CustID = '0002'
            AND TranDate BETWEEN CONVERT (
                DATETIME,
                '2014-01-01 00:00:00',
                102
            )
            AND CONVERT (
                DATETIME,
                '2015-01-01 00:00:00',
                102
            )
            UNION
                SELECT
                    IncomeTransactions.id,
                    IncomeTransactions.CustID AS CustID,
                    IncomeTransactions.CustName,
                    Str(IncomeTransactions.BillDoc),
                    IncomeTransactions.TranDate,
                    IncomeTranType.TranDesc,
                    0 AS db,
                    IncomeTransactions.Amount AS Cr,
                    BankID,
                    TranCode = CASE
                WHEN TranType = 32 THEN
                    'F3'
                ELSE
                    CASE
                WHEN TranType = 27 THEN
                    'F2'
                ELSE
                    CASE
                WHEN TranType = 42 THEN
                    'F11'
                ELSE
                    'C'
                END
                END
                END
                FROM
                    IncomeTransactions,
                    IncomeTranType
                WHERE
                    IncomeTransactions.TranType = IncomeTranType.id
                AND (
                    IncomeTransactions.TranType = 17
                    OR IncomeTransactions.TranType = 32
                    OR IncomeTransactions.TranType = 42
                    OR IncomeTransactions.TranType = 27
                )
                AND CustID = '0002'
                AND TranDate BETWEEN CONVERT (
                    DATETIME,
                    '2014-01-01 00:00:00',
                    102
                )
                AND CONVERT (
                    DATETIME,
                    '2015-01-01 00:00:00',
                    102
                )
                ORDER BY
                    5

编辑: 下面是我现在的(只加了TOP 10)。但是结果是一样的。

SELECT TOP 10 SalesTranHeader.id, CustID, OrgName, BillNo, TranDate, TranDesc, db = CASE
            WHEN trancode = 'T5'
            OR trancode LIKE 'F2'
            OR trancode LIKE 'F3'
            OR trancode LIKE 'F4'
            OR TranCode LIKE 'F11' THEN
                0
            ELSE
                [amount] - [OtherTranAmount] - [HignTemp] - [Penalty]
            END,
             cr = CASE
            WHEN [trancode] LIKE 't5'
            OR [trancode] LIKE 'F2'
            OR [trancode] LIKE 'F3'
            OR [trancode] LIKE 'F4'
            OR [trancode] LIKE 'F11' THEN
                [amount] - [OtherTranAmount] - [higntemp] - [Penalty]
            ELSE
                0
            END,
             ' ' AS bankID,
             [trancode]
            FROM
                salesTranHeader
            WHERE
                id <> 0
            AND CustID = '0002'
            AND TranDate BETWEEN CONVERT (
                DATETIME,
                '2014-01-01 00:00:00',
                102
            )
            AND CONVERT (
                DATETIME,
                '2015-01-01 00:00:00',
                102
            )
            UNION
                SELECT
                    IncomeTransactions.id,
                    IncomeTransactions.CustID AS CustID,
                    IncomeTransactions.CustName,
                    Str(IncomeTransactions.BillDoc),
                    IncomeTransactions.TranDate,
                    IncomeTranType.TranDesc,
                    0 AS db,
                    IncomeTransactions.Amount AS Cr,
                    BankID,
                    TranCode = CASE
                WHEN TranType = 32 THEN
                    'F3'
                ELSE
                    CASE
                WHEN TranType = 27 THEN
                    'F2'
                ELSE
                    CASE
                WHEN TranType = 42 THEN
                    'F11'
                ELSE
                    'C'
                END
                END
                END
                FROM
                    IncomeTransactions,
                    IncomeTranType
                WHERE
                    IncomeTransactions.TranType = IncomeTranType.id
                AND (
                    IncomeTransactions.TranType = 17
                    OR IncomeTransactions.TranType = 32
                    OR IncomeTransactions.TranType = 42
                    OR IncomeTransactions.TranType = 27
                )
                AND CustID = '0002'
                AND TranDate BETWEEN CONVERT (
                    DATETIME,
                    '2014-01-01 00:00:00',
                    102
                )
                AND CONVERT (
                    DATETIME,
                    '2015-01-01 00:00:00',
                    102
                )
                ORDER BY
                    5

您应该在 UNION 的结果上添加 TOP

SELECT TOP 10 *
FROM (
    SELECT
        SalesTranHeader.id, 
        CustID, 
        OrgName, 
        BillNo, 
        TranDate, 
        TranDesc, 
        db = 
            CASE
                WHEN trancode IN('T5', 'F2,' ,'F3', 'F4', 'F11') THEN 0
                ELSE [amount] - [OtherTranAmount] - [HignTemp] - [Penalty]
            END,
        cr = 
            CASE
                WHEN trancode IN('T5', 'F2,' ,'F3', 'F4', 'F11') THEN [amount] - [OtherTranAmount] - [higntemp] - [Penalty]
                ELSE 0
            END,
        bankID = ' ',
        [trancode]
    FROM salesTranHeader
    WHERE
        id <> 0
        AND CustID = '0002'
        AND TranDate BETWEEN CONVERT(DATETIME, '2014-01-01 00:00:00', 102) AND CONVERT(DATETIME, '2015-01-01 00:00:00', 102)

    UNION

    SELECT
        IncomeTransactions.id,
        CustID = IncomeTransactions.CustID,
        IncomeTransactions.CustName,
        STR(IncomeTransactions.BillDoc),
        IncomeTransactions.TranDate,
        IncomeTranType.TranDesc,
        db = 0,
        Cr = IncomeTransactions.Amount,
        BankID,
        TranCode = 
            CASE
                WHEN TranType = 32 THEN 'F3'
                WHEN TranType = 27 THEN 'F2'
                WHEN TranType = 42 THEN 'F11'
                ELSE 'C'
            END
    FROM IncomeTransactions
    INNER JOIN IncomeTranType
        ON IncomeTransactions.TranType = IncomeTranType.id
    WHERE
        IncomeTransactions.TranType IN(17, 32, 42, 27)
        AND CustID = '0002'
        AND TranDate BETWEEN CONVERT(DATETIME, '2014-01-01 00:00:00', 102) AND CONVERT(DATETIME, '2015-01-01 00:00:00',102)
)t
ORDER BY 5

我也对查询进行了一些简化。