1

因此,我为同事编写了以下查询(见下文):

SELECT
        t.tender_id as "Tender ID",
        t.check_number as "Check Number",
        t.check_type_id as "Check Type",
        t.server_id as "Server ID",
        t.cashier_id as "Cashier ID",
        t.terminal_id as "Terminal ID",
        t.tendered_date_time as "Tendered Date and Time",
        t.tender_amount as "Tender Amount",
        t.change_amount as "Change Amount",
        g.account_name as "Account Name",
        g.account_number as "Account Number"
FROM CheckTender AS t
INNER JOIN AcountActivity AS g
        ON t.check_number = g.check_number
WHERE t.tender_id NOT in (5,14,4,9,15,16);

一个支票号码可以有多个tender_ID。因此,例如,您可以为支票编号 20001 设置三行,它们都具有不同的招标 ID。

最初,他只想要删除了tender_id 的任何条目5,14,4,9,15,16。但是后来我们发现我们需要修改 where 条件。

它需要更新,以便如果支票号码的投标 ID 为5,14,4,9,15,16,则删除该支票号码的所有实例。我知道这很可能是一个子查询,但我整个早上都在努力解决它,却无法弄清楚。:(

4

3 回答 3

3

您可以EXISTS为此使用:

SELECT
        t.tender_id as "Tender ID",
        t.check_number as "Check Number",
        t.check_type_id as "Check Type",
        t.server_id as "Server ID",
        t.cashier_id as "Cashier ID",
        t.terminal_id as "Terminal ID",
        t.tendered_date_time as "Tendered Date and Time",
        t.tender_amount as "Tender Amount",
        t.change_amount as "Change Amount",
        g.account_name as "Account Name",
        g.account_number as "Account Number"
FROM CheckTender AS t
INNER JOIN AcountActivity AS g
        ON t.check_number = g.check_number
WHERE NOT EXISTS (  SELECT * FROM CheckTender WHERE check_number =  t.check_number
                    AND tender_id in (5,14,4,9,15,16))
于 2013-01-18T16:01:31.223 回答
2

像这样的东西(未经测试)?

SELECT
        t.tender_id as "Tender ID",
        t.check_number as "Check Number",
        t.check_type_id as "Check Type",
        t.server_id as "Server ID",
        t.cashier_id as "Cashier ID",
        t.terminal_id as "Terminal ID",
        t.tendered_date_time as "Tendered Date and Time",
        t.tender_amount as "Tender Amount",
        t.change_amount as "Change Amount",
        g.account_name as "Account Name",
        g.account_number as "Account Number"
FROM CheckTender AS t
INNER JOIN AcountActivity AS g
        ON t.check_number = g.check_number
LEFT JOIN (SELECT check_number  FROM CheckTender WHERE tender_id in (5,14,4,9,15,16)) t2 on t.tender_id = t2.tender_id
WHERE t2.tender_id is null
于 2013-01-18T16:01:50.913 回答
2

这是一种使用窗口函数的方法:

select *
from (SELECT
             t.tender_id as "Tender ID",
             t.check_number as "Check Number",
             t.check_type_id as "Check Type",
             t.server_id as "Server ID",
             t.cashier_id as "Cashier ID",
             t.terminal_id as "Terminal ID",
             t.tendered_date_time as "Tendered Date and Time",
             t.tender_amount as "Tender Amount",
             t.change_amount as "Change Amount",
             g.account_name as "Account Name",
             g.account_number as "Account Number",
             MAX(case when t.tender_id in (5,14,4,9,15,16) then 1 else 0 end) over (partition by t.check_number) as HasBadTender
      FROM CheckTender AS t
      INNER JOIN AcountActivity AS g
                 ON t.check_number = g.check_number
     ) t
where HasBadTender = 0

这适用于 SQL Server 2005 及更高版本。对于早期版本,您需要某种形式的连接或相关子查询。

对原始 where 子句的细微变化也适用:

WHERE t.check_number not in (select check_number from CheckTender where tender in (5,14,4,9,15,16))
于 2013-01-18T16:03:42.753 回答