开发者

How can I modify the following code to have the same results with "exists"?(SQL)

开发者 https://www.devze.com 2023-01-30 09:13 出处:网络
I have been trying for hours but I can\'t think of anything as exists only returns boolean and every time I am trying to use exists I either get back the entire list of results or none.

I have been trying for hours but I can't think of anything as exists only returns boolean and every time I am trying to use exists I either get back the entire list of results or none.

This is the following code that returns me the result I want.

 SELECT  DISTINCT Clients.Clients_Code

 FROM         Account 

 INNER JOIN Clients ON Account.Account_Number = Clients.Account_Number 
 INNER JOIN Credit_Card ON Account.Account_Number = Credit_Card.Account_Number 
 INNER JOIN Transactions ON Credit_Card.Credit_Number = Transactions.Credit_Number 
 INNER JOIN Transactions AS Transactions_1 ON Credit_Card.Credit_Number开发者_JAVA百科 = Transactions_1.Credit_Number 
 INNER JOIN Transactions AS Transactions_2 ON Credit_Card.Credit_Number = Transactions_2.Credit_Number 
 INNER JOIN Transactions AS Transactions_3 ON Credit_Card.Credit_Number = Transactions_3.Credit_Number

 WHERE (Transactions_3.Store_Code = '7182') 
 AND   (Transactions_2.Store_Code = '2019') 
 AND   (Transactions_1.Store_Code = '3121')

THANK YOU ALL FOR YOUR RESPONSE AND ESPECIALLY BENJAMIN. HIS RESPONSE WORKED PERFECTLY


From what I've figured out from your details, you're trying to find a client that has used the same credit card at three different stores. The following query would provide the same results.

SELECT Clients.Clients_Code
FROM Clients c
INNER JOIN Credit_Card cc ON cc.Account_Number = c.Account_Number
Where Exists(select * from Tranctions t with (nolock) where t.Store_Code = '7182' and t.Credit_Number = cc.Credit_Number) 
AND Exists(select * from Tranctions t with (nolock) where t.Store_Code = '2019' and t.Credit_Number = cc.Credit_Number) 
AND Exists(select * from Tranctions t with (nolock) where t.Store_Code = '3121' and t.Credit_Number = cc.Credit_Number) 

An alternative is:

SELECT Clients.Clients_Code
FROM Clients c
INNER JOIN Credit_Card cc ON cc.Account_Number = c.Account_Number
Where (select count(*) from Tranctions t with (nolock) where t.Store_Code in ('7182', '2019','3121') and t.Credit_Number = cc.Credit_Number) =3


I doubt you even need the Account table.

Does this work?

SELECT DISTINCT Clients.Clients_Code FROM Clients 
INNER JOIN Credit_Card on Clients.Account_Number = Credit_Card.Account_Number 
WHERE EXISTS ( 
    SELECT null FROM Transactions 
    WHERE Transactions.Credit_Number = Credit_Card.Credit_Number
    AND Transactions.Store_Code in ('7182','2019','3121')
) 


I'm not sure I understand your question completely. Are you asking how that query can return a boolean value rather than the actual records? If so:

IF EXISTS (SELECT DISTINCT Clients.Clients_Code
            FROM Account 
            INNER JOIN Clients 
              ON Account.Account_Number = Clients.Account_Number 
            INNER JOIN Credit_Card 
              ON Account.Account_Number = Credit_Card.Account_Number 
            INNER JOIN Transactions 
              ON Credit_Card.Credit_Number = Transactions.Credit_Number 
            INNER JOIN Transactions AS Transactions_1 
              ON Credit_Card.Credit_Number = Transactions_1.Credit_Number 
            INNER JOIN Transactions AS Transactions_2 
              ON Credit_Card.Credit_Number = Transactions_2.Credit_Number 
            INNER JOIN Transactions AS Transactions_3 
              ON Credit_Card.Credit_Number = Transactions_3.Credit_Number
            WHERE (Transactions_3.Store_Code = '7182') 
              AND (Transactions_2.Store_Code = '2019') 
              AND (Transactions_1.Store_Code = '3121'))
   SELECT 1 --True
ELSE
   SELECT 0 --False

If that's not what you are trying to accomplish can you please clarify your question?


Your query is... odd.

I think you can achieve the same results with this query:

SELECT DISTINCT c.Clients_Code
FROM Account AS a
INNER JOIN Clients AS c
    ON a.Account_Number = c.Account_Number
INNER JOIN Credit_Card AS cc
    ON a.Account_Number = cc.Account_Number
INNER JOIN Transactions AS t
    ON cc.Credit_Number = t0.Credit_Number
WHERE t.Store_Code IN ('7182', '2019', '3121')

If you want to know if any results exist for that query, then you want to just wrap the whole thing in an EXISTS([above_query]).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号