繁体   English   中英

有没有更有效的方式来编写此sql查询?

[英]Is there more efficient way to write this sql query?

我需要在下面的2个表上写一个有效的查询,条件如下表:第一个表

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

第二表

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.不显示客户来自“罗马”的帐户
5.显示超过1位客户的帐户。

(*)-可能没有记录返回。

我写了如下查询,我想确认这样做的最佳方法, 目的是减少我们处理表和执行Joins的次数

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
                       );

这可能会有所帮助。 您也可以使用窗口函数编写此函数,这可能会更有效。 我认为以下查询记录了您的原始情况:

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;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM