Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 query (get customer with the same set of loans)

Author  Topic 

nael123
Starting Member

2 Posts

Posted - 2014-05-16 : 11:13:01
Hello Guys,

Please help me with this query.

I have this table below with customers having loans. Nou I want to write a query which retrieves all customers with exactly the same set of Loans.


Rules:

If I give C1 then I should get C2 (same set of loans)
If I give C2 then I should get C1 (same set of loans)
If I give C3 then I should get C4 (same set of loans)
If I give C4 then I should get C3 (same set of loans)


If I give C5 then I should NOT get any row because there is no other Customer with the same set of loans.
If I give C6 then I should NOT get any row because there is no other Customer with the same set of loans.
If I give C7 then I should NOT get any row because there is no other Customer with the same set of loans.



Table LoanCustomer:
[KEY] [LOAN] [Customer]
1 L1 C5
2 L2 C1
3 L2 C2
4 L3 C1
5 L3 C2
6 L4 C3
7 L4 C4
8 L5 C6
9 L5 C7
10 L6 C7




I tried this query below but it doesn't give me always the same right results. Please give me the right query

SELECT t1.LOAN, t1.CUSTOMER
FROM LoanCustomer t1
WHERE EXISTS (
SELECT t2.LOAN, t2.CUSTOMER
FROM LoanCustomer t2
WHERE CUSTOMER= 'C1'
and t1.CUSTOMER != t2.CUSTOMER and t1.LOAN = t2.LOAN
)




Thank you in advance.

Ifor
Aged Yak Warrior

700 Posts

Posted - 2014-05-16 : 12:41:01
There are probably better ways, but something like the following should work:

DECLARE @LikeCust char(2);
SET @LikeCust = 'C1';

WITH CustList
AS
(
SELECT T1.Customer
FROM LoanCustomer T1
JOIN LoanCustomer T2
ON T1.LOAN = T2.LOAN
WHERE T1.Customer <> T2.Customer
AND T2.Customer = @LikeCust
GROUP BY T1.Customer
-- Same number as @LikeCust
HAVING COUNT(*) = (SELECT COUNT(*) FROM LoanCustomer WHERE Customer = @LikeCust)
-- No more loans for T1
AND COUNT(*) = (SELECT COUNT(*) FROM LoanCustomer T3 WHERE T3.Customer = T1.Customer)
)
SELECT T.LOAN, T.Customer
FROM LoanCustomer T
JOIN CustList C
ON T.Customer = C.Customer;

Go to Top of Page

nael123
Starting Member

2 Posts

Posted - 2014-05-18 : 14:29:23
Thank you very much Ifor. It works fine for me so I'm happy with it.
Go to Top of Page

MuralikrishnaVeera
Posting Yak Master

129 Posts

Posted - 2014-05-19 : 01:53:54
Following single line Query generates same result set


---------------------------------------------Create Table--------------------------------------------------------
CREATE TABLE LoanCustomer([KEY] INT,[LOAN] VARCHAR(10),[Customer] VARCHAR(10))
INSERT INTO LoanCustomer
SELECT 1,'L1','C5' UNION ALL
SELECT 2,'L2','C1' UNION ALL
SELECT 3,'L2','C2' UNION ALL
SELECT 4,'L3','C1' UNION ALL
SELECT 5,'L3','C2' UNION ALL
SELECT 6,'L4','C3' UNION ALL
SELECT 7,'L4','C4' UNION ALL
SELECT 8,'L5','C6' UNION ALL
SELECT 9,'L5','C7' UNION ALL
SELECT 10,'L6','C7'

DECLARE @Cus VARCHAR(10)= 'C1'

SELECT * FROM LoanCustomer WHERE LOAN IN (SELECT Loan FROM LoanCustomer WHERE Customer=@Cus) AND Customer <>@Cus
---------------------------------------------Drop Table ----------------------------------------------------------
DROP TABLE LoanCustomer
------------------------------------------------------------------------------------------------------------------






---------------
Murali Krishna

You live only once ..If you do it right once is enough.......
Go to Top of Page
   

- Advertisement -