有没有更有效的方法来编写这个 sql 查询?

Is there more efficient way to write this sql query?

我需要对下面的 2 个表编写一个高效的查询,条件如下表所示: 首先Table

CityCode    CustomerID  AccountID 
Paris       1           1
Roma        2           1
London      1           2
Paris       3           2
Roma        4           3
Berlin      5           4

第二个Table

Credit_card_ind Credit  AccountID 
0               1000    1
1               5000    2
0               2300    3
1               30000   4

0 - 没有卡
1 - 有一张卡片

在查询中我们需要满足以下条件:
1.所有信用5000以上的客户
2. 不显示其中一位客户没有信用卡的帐户
3.信用额度30000以下的所有账户
4. 不显示其中一位客户来自 'Roma'
的帐户 5. 显示超过 1 个客户的帐户。

(*) - 可能没有记录 return.

我按如下方式编写查询,我想确认它是这样做的最佳方法,目的是减少我们处理表和执行连接的次数:

Select  c.AccountID,
        c.CustomerID        
From    Customers as c 
Join    credit_cards as ca on c.AccountID = ca.AccountID
Where   ca.credit > 5000 And ca.credit < 30000
And     c.AccountID not in  (
                            Select  Distinct newTBL.AccountID
                            From    (
                                    Select  c1.CustomerID,
                                            c1.AccountID
                                    From    customers as c1
                                    Join    credit_cards as ca1 on c.AccountID = ca.AccountID
                                    Where   ca1.credit_card_ind = 0
                                    Or  c1.CityCode like ‘Roma’
                                    ) as newTBL
                            )
And    c.AccountID in ( 
                        Select  newCus.AccountID
                        From    (
                                Select  AccountID,
                                        Count(CustomerID) as [Num_of_Cus]
                                From customers
                                Group by AccountID
                                ) as newCus
                        Where newCus.[Num_of_Cus] > 1
                        )

试试这个。

SELECT cs.AccountID
FROM   customer cs
       JOIN credit cr
         ON cs.AccountID = cr.AccountID
WHERE  CityCode <> 'Roma'
       AND Credit_card_ind = 1
       AND Credit > 5000
       AND Credit < 30000
GROUP  BY cs.AccountID
HAVING Count(cs.CustomerID) > 1 

首先,您可以通过删除嵌套子查询来简化查询:

Select  c.AccountID, c.CustomerID        
From    Customers c Join
        credit_cards ca
        on c.AccountID = ca.AccountID
Where   ca.credit > 5000 And ca.credit < 30000 And
        c.AccountID not in  (Select  c1.CustomerID
                             From customers c1 Join
                                  credit_cards ca1
                                  on c.AccountID = ca.AccountID
                             Where  ca1.credit_card_ind = 0 Or c1.CityCode like 'Roma'
                            ) And
        c.AccountID in (Select AccountID
                        From customers
                        Group by AccountID
                        Having Count(CustomerID) > 1
                       );

这可能会有所帮助。您也可以使用 window 函数编写此代码,这可能更有效。我认为以下查询捕获了您的原始条件:

select c.AccountID, c.CustomerID 
from (select c.*, count(*) over (partition by c.accountid) as cnt,
             max(case when c.CityCode like 'Roma' then 1 else 0 end) as cnt_Roma
      from customers c
     ) c join
     credit cr
     on c.accountid = cr.accountid
where ca.credit > 5000 And ca.credit < 30000 and
      c.cnt > 0 and c.cnt_Roma = 0 and
      ca.credit_card_ind <> 0;