Query to find customers that have deposit in all branches located in a specific city
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{
margin-bottom:0;
}
I have 3 tables as follow:
Customer (CustomerName , CustomerCity , CustomerStreet)
Branch (BranchName , BranchCity)
Deposit (CustomerName , BranchName , AccountNumber , Balance)
Here I show you sample data of the above tables :
Customer ('John','City1','Street1')
('Lili','City2','street2')
Branch ('Branch1','Kaj')
('Branch2','Bahar')
('Branch3','Bahar')
('Branch4','Bahar')
Deposit ('John','Branch1',12,1000)
('Lili','Branch1',123,1222)
('Lili','Branch2',124,45233)
('Lili','Branch3',345,6000)
('Lili','Branch4',567,5600)
So based on the information above, I need a query to give me CustomerName
that have a deposit in all branches that are in Bahar.
I should see 'Lili' in the output because it has deposits for all branches that are in Bahar but I should not see John cause it has a deposit only in one branch of Bahar.
I did my best to write the query but I could not. Could you help me with this please?
sql-server t-sql query
add a comment
|
I have 3 tables as follow:
Customer (CustomerName , CustomerCity , CustomerStreet)
Branch (BranchName , BranchCity)
Deposit (CustomerName , BranchName , AccountNumber , Balance)
Here I show you sample data of the above tables :
Customer ('John','City1','Street1')
('Lili','City2','street2')
Branch ('Branch1','Kaj')
('Branch2','Bahar')
('Branch3','Bahar')
('Branch4','Bahar')
Deposit ('John','Branch1',12,1000)
('Lili','Branch1',123,1222)
('Lili','Branch2',124,45233)
('Lili','Branch3',345,6000)
('Lili','Branch4',567,5600)
So based on the information above, I need a query to give me CustomerName
that have a deposit in all branches that are in Bahar.
I should see 'Lili' in the output because it has deposits for all branches that are in Bahar but I should not see John cause it has a deposit only in one branch of Bahar.
I did my best to write the query but I could not. Could you help me with this please?
sql-server t-sql query
add a comment
|
I have 3 tables as follow:
Customer (CustomerName , CustomerCity , CustomerStreet)
Branch (BranchName , BranchCity)
Deposit (CustomerName , BranchName , AccountNumber , Balance)
Here I show you sample data of the above tables :
Customer ('John','City1','Street1')
('Lili','City2','street2')
Branch ('Branch1','Kaj')
('Branch2','Bahar')
('Branch3','Bahar')
('Branch4','Bahar')
Deposit ('John','Branch1',12,1000)
('Lili','Branch1',123,1222)
('Lili','Branch2',124,45233)
('Lili','Branch3',345,6000)
('Lili','Branch4',567,5600)
So based on the information above, I need a query to give me CustomerName
that have a deposit in all branches that are in Bahar.
I should see 'Lili' in the output because it has deposits for all branches that are in Bahar but I should not see John cause it has a deposit only in one branch of Bahar.
I did my best to write the query but I could not. Could you help me with this please?
sql-server t-sql query
I have 3 tables as follow:
Customer (CustomerName , CustomerCity , CustomerStreet)
Branch (BranchName , BranchCity)
Deposit (CustomerName , BranchName , AccountNumber , Balance)
Here I show you sample data of the above tables :
Customer ('John','City1','Street1')
('Lili','City2','street2')
Branch ('Branch1','Kaj')
('Branch2','Bahar')
('Branch3','Bahar')
('Branch4','Bahar')
Deposit ('John','Branch1',12,1000)
('Lili','Branch1',123,1222)
('Lili','Branch2',124,45233)
('Lili','Branch3',345,6000)
('Lili','Branch4',567,5600)
So based on the information above, I need a query to give me CustomerName
that have a deposit in all branches that are in Bahar.
I should see 'Lili' in the output because it has deposits for all branches that are in Bahar but I should not see John cause it has a deposit only in one branch of Bahar.
I did my best to write the query but I could not. Could you help me with this please?
sql-server t-sql query
sql-server t-sql query
edited May 28 at 10:40
Michael Green
16.3k8 gold badges36 silver badges73 bronze badges
16.3k8 gold badges36 silver badges73 bronze badges
asked May 27 at 5:56
PTTTPTTT
1601 silver badge10 bronze badges
1601 silver badge10 bronze badges
add a comment
|
add a comment
|
4 Answers
4
active
oldest
votes
I have two solutions for your question :
solution 1 , I use Not Exists , Except
Select distinct C.CustomerName
From customers C
where NOT EXISTS ((Select BranchName
From Branch
Where BranchCity='Bahar')
EXCEPT
(Select BranchName
From Deposit a
where A.CustomerName = C.CustomerName))
In solution 2 I use Not Exists ,
Select Distinct C.CustomerName
From Customers C
Where NOT EXISTS (Select BranchName
From Branch b
Where BranchCity='Bahar'
and NOT EXISTS (select BranchName
from Deposit a
where a.CustomerName=C.CustomerName
and a.BranchName=b.branchName))
Each one of the query above will answer your query and give you the desired result.
add a comment
|
select CustomerName
from Deposit
group by CustomerName
having count(distinct BranchName) = (select count(*) from Branch)
Note that distinct is only used if there are multiple deposits by the same customer and branch.
However, it's possible to use join with Customer table or nested lookup queries for more information.
add a comment
|
;with cte as(
select c.Name as Cname,b.Name as bName,
ROW_NUMBER() over(partition by c.Name order by b.Name,c.Name) RowID
from Customer c
inner join Deposit d on c.Name=d.CustomerName
inner join Branch b on b.Name=d.Branch
where b.City='Bahar'
)
select Cname from cte
group by cName
having max(RowID)=(select count(*) from Branch where City='Bahar')
add a comment
|
SELECT c.CustomerName
FROM Customer c
CROSS JOIN Branch b
LEFT JOIN Deposit d
ON d.CustomerName = c.CustomerName
AND d.BranchName = b.BranchName
WHERE b.BranchCity = 'Bahar'
GROUP BY c.CustomerName
HAVING SUM(CASE WHEN d.AccountNumber IS NULL THEN 1 ELSE 0 END) = 0
add a comment
|
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "182"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f239115%2fquery-to-find-customers-that-have-deposit-in-all-branches-located-in-a-specific%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
I have two solutions for your question :
solution 1 , I use Not Exists , Except
Select distinct C.CustomerName
From customers C
where NOT EXISTS ((Select BranchName
From Branch
Where BranchCity='Bahar')
EXCEPT
(Select BranchName
From Deposit a
where A.CustomerName = C.CustomerName))
In solution 2 I use Not Exists ,
Select Distinct C.CustomerName
From Customers C
Where NOT EXISTS (Select BranchName
From Branch b
Where BranchCity='Bahar'
and NOT EXISTS (select BranchName
from Deposit a
where a.CustomerName=C.CustomerName
and a.BranchName=b.branchName))
Each one of the query above will answer your query and give you the desired result.
add a comment
|
I have two solutions for your question :
solution 1 , I use Not Exists , Except
Select distinct C.CustomerName
From customers C
where NOT EXISTS ((Select BranchName
From Branch
Where BranchCity='Bahar')
EXCEPT
(Select BranchName
From Deposit a
where A.CustomerName = C.CustomerName))
In solution 2 I use Not Exists ,
Select Distinct C.CustomerName
From Customers C
Where NOT EXISTS (Select BranchName
From Branch b
Where BranchCity='Bahar'
and NOT EXISTS (select BranchName
from Deposit a
where a.CustomerName=C.CustomerName
and a.BranchName=b.branchName))
Each one of the query above will answer your query and give you the desired result.
add a comment
|
I have two solutions for your question :
solution 1 , I use Not Exists , Except
Select distinct C.CustomerName
From customers C
where NOT EXISTS ((Select BranchName
From Branch
Where BranchCity='Bahar')
EXCEPT
(Select BranchName
From Deposit a
where A.CustomerName = C.CustomerName))
In solution 2 I use Not Exists ,
Select Distinct C.CustomerName
From Customers C
Where NOT EXISTS (Select BranchName
From Branch b
Where BranchCity='Bahar'
and NOT EXISTS (select BranchName
from Deposit a
where a.CustomerName=C.CustomerName
and a.BranchName=b.branchName))
Each one of the query above will answer your query and give you the desired result.
I have two solutions for your question :
solution 1 , I use Not Exists , Except
Select distinct C.CustomerName
From customers C
where NOT EXISTS ((Select BranchName
From Branch
Where BranchCity='Bahar')
EXCEPT
(Select BranchName
From Deposit a
where A.CustomerName = C.CustomerName))
In solution 2 I use Not Exists ,
Select Distinct C.CustomerName
From Customers C
Where NOT EXISTS (Select BranchName
From Branch b
Where BranchCity='Bahar'
and NOT EXISTS (select BranchName
from Deposit a
where a.CustomerName=C.CustomerName
and a.BranchName=b.branchName))
Each one of the query above will answer your query and give you the desired result.
edited May 28 at 11:40
answered May 27 at 6:08
Pantea TourangPantea Tourang
56716 bronze badges
56716 bronze badges
add a comment
|
add a comment
|
select CustomerName
from Deposit
group by CustomerName
having count(distinct BranchName) = (select count(*) from Branch)
Note that distinct is only used if there are multiple deposits by the same customer and branch.
However, it's possible to use join with Customer table or nested lookup queries for more information.
add a comment
|
select CustomerName
from Deposit
group by CustomerName
having count(distinct BranchName) = (select count(*) from Branch)
Note that distinct is only used if there are multiple deposits by the same customer and branch.
However, it's possible to use join with Customer table or nested lookup queries for more information.
add a comment
|
select CustomerName
from Deposit
group by CustomerName
having count(distinct BranchName) = (select count(*) from Branch)
Note that distinct is only used if there are multiple deposits by the same customer and branch.
However, it's possible to use join with Customer table or nested lookup queries for more information.
select CustomerName
from Deposit
group by CustomerName
having count(distinct BranchName) = (select count(*) from Branch)
Note that distinct is only used if there are multiple deposits by the same customer and branch.
However, it's possible to use join with Customer table or nested lookup queries for more information.
edited May 27 at 9:46
answered May 27 at 8:08
Amir BahramiAmir Bahrami
12 bronze badges
12 bronze badges
add a comment
|
add a comment
|
;with cte as(
select c.Name as Cname,b.Name as bName,
ROW_NUMBER() over(partition by c.Name order by b.Name,c.Name) RowID
from Customer c
inner join Deposit d on c.Name=d.CustomerName
inner join Branch b on b.Name=d.Branch
where b.City='Bahar'
)
select Cname from cte
group by cName
having max(RowID)=(select count(*) from Branch where City='Bahar')
add a comment
|
;with cte as(
select c.Name as Cname,b.Name as bName,
ROW_NUMBER() over(partition by c.Name order by b.Name,c.Name) RowID
from Customer c
inner join Deposit d on c.Name=d.CustomerName
inner join Branch b on b.Name=d.Branch
where b.City='Bahar'
)
select Cname from cte
group by cName
having max(RowID)=(select count(*) from Branch where City='Bahar')
add a comment
|
;with cte as(
select c.Name as Cname,b.Name as bName,
ROW_NUMBER() over(partition by c.Name order by b.Name,c.Name) RowID
from Customer c
inner join Deposit d on c.Name=d.CustomerName
inner join Branch b on b.Name=d.Branch
where b.City='Bahar'
)
select Cname from cte
group by cName
having max(RowID)=(select count(*) from Branch where City='Bahar')
;with cte as(
select c.Name as Cname,b.Name as bName,
ROW_NUMBER() over(partition by c.Name order by b.Name,c.Name) RowID
from Customer c
inner join Deposit d on c.Name=d.CustomerName
inner join Branch b on b.Name=d.Branch
where b.City='Bahar'
)
select Cname from cte
group by cName
having max(RowID)=(select count(*) from Branch where City='Bahar')
answered May 28 at 12:24
Asrar Ahmad EhsanAsrar Ahmad Ehsan
991 silver badge9 bronze badges
991 silver badge9 bronze badges
add a comment
|
add a comment
|
SELECT c.CustomerName
FROM Customer c
CROSS JOIN Branch b
LEFT JOIN Deposit d
ON d.CustomerName = c.CustomerName
AND d.BranchName = b.BranchName
WHERE b.BranchCity = 'Bahar'
GROUP BY c.CustomerName
HAVING SUM(CASE WHEN d.AccountNumber IS NULL THEN 1 ELSE 0 END) = 0
add a comment
|
SELECT c.CustomerName
FROM Customer c
CROSS JOIN Branch b
LEFT JOIN Deposit d
ON d.CustomerName = c.CustomerName
AND d.BranchName = b.BranchName
WHERE b.BranchCity = 'Bahar'
GROUP BY c.CustomerName
HAVING SUM(CASE WHEN d.AccountNumber IS NULL THEN 1 ELSE 0 END) = 0
add a comment
|
SELECT c.CustomerName
FROM Customer c
CROSS JOIN Branch b
LEFT JOIN Deposit d
ON d.CustomerName = c.CustomerName
AND d.BranchName = b.BranchName
WHERE b.BranchCity = 'Bahar'
GROUP BY c.CustomerName
HAVING SUM(CASE WHEN d.AccountNumber IS NULL THEN 1 ELSE 0 END) = 0
SELECT c.CustomerName
FROM Customer c
CROSS JOIN Branch b
LEFT JOIN Deposit d
ON d.CustomerName = c.CustomerName
AND d.BranchName = b.BranchName
WHERE b.BranchCity = 'Bahar'
GROUP BY c.CustomerName
HAVING SUM(CASE WHEN d.AccountNumber IS NULL THEN 1 ELSE 0 END) = 0
answered May 28 at 9:35
VisakhVisakh
11 bronze badge
11 bronze badge
add a comment
|
add a comment
|
Thanks for contributing an answer to Database Administrators Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f239115%2fquery-to-find-customers-that-have-deposit-in-all-branches-located-in-a-specific%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown