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

时间:2015-01-11 11:44:02

标签: sql sql-server

我需要在下面的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个客户的帐户。

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

我写了如下查询,我想确认它是最好的方法,意图是减少我们接近表格和做联接的次数

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
                        )

2 个答案:

答案 0 :(得分:0)

试试这个。

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 

答案 1 :(得分:0)

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

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;