Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL:查找在“Adam Rivera”拥有帐户的所有分支机构中拥有帐户的客户_Sql - Fatal编程技术网

SQL:查找在“Adam Rivera”拥有帐户的所有分支机构中拥有帐户的客户

SQL:查找在“Adam Rivera”拥有帐户的所有分支机构中拥有帐户的客户,sql,Sql,我需要找到在Adam Rivera拥有账户的所有分支机构拥有账户的客户ID、姓氏和出生日期 这是银行模式: § Customer = {customerID, firstName, lastName, income, birthDate } § Account = {accNumber, type, balance, branchNumberFK-Branch} § Owns = {customerIDFK-Customer, accNumberFK-Account} § Transaction

我需要找到在Adam Rivera拥有账户的所有分支机构拥有账户的客户ID、姓氏和出生日期

这是银行模式:

§ Customer = {customerID, firstName, lastName, income, birthDate }
§ Account = {accNumber, type, balance, branchNumberFK-Branch}
§ Owns = {customerIDFK-Customer, accNumberFK-Account}
§ Transactions = {transNumber, accNumberFK-Account, amount}
§ Employee = {sin, firstName, lastName, salary, branchNumberFK-Branch}
§ Branch = {branchNumber, branchName, managerSINFK-Employee, budget}
Adam Rivera拥有的所有分行编号,查询结果如下:

branchNumber
1
4

SELECT b2.branchNumber -- all branches that Adam Rivera owns
FROM Customer c2 join Owns o2 on c2.customerID = o2.customerID join Account a2 on a2.accNumber = o2.accNumber join Branch b2 on b2.branchNumber = a2.branchNumber
WHERE c2.firstName = 'Adam' and c2.lastName = 'Rivera'
客户在Adam Rivera分支机构拥有账户的分支机构编号列表,其名称为:

cusID   lstName birthDate   branch#
10839   Hayes   1977-06-09  1
10839   Hayes   1977-06-09  4
11790   Green   1920-11-19  1
11790   Green   1920-11-19  1
13230   Brooks  1967-05-04  1
13423   Simmons 1955-03-21  1
13423   Simmons 1955-03-21  4
13697   Hill    1951-03-07  1
13697   Hill    1951-03-07  4
14295   Ramirez 1983-06-30  4
18166   Barnes  1951-03-07  1
19973   Kelly   1987-07-15  1
22050   Sanchez 1935-01-08  1
.......


SELECT c1.customerID, c1.lastName, c1.birthDate, b1.branchNumber

FROM Customer c1 join Owns o1 on c1.customerID = o1.customerID join Account a1 on a1.accNumber = o1.accNumber join Branch b1 on b1.branchNumber = a1.branchNumber

WHERE EXISTS 
      SELECT b3.branchNumber
      FROM Customer c3 join Owns o3 on c3.customerID = o3.customerID join Account a3 on a3.accNumber = o3.accNumber join Branch b3 on b3.branchNumber = a3.branchNumber
      WHERE c3.firstName = 'Adam' and c3.lastName = 'Rivera' and b1.branchNumber = b3.branchNumber 
以下结果应返回客户没有账户的所有分行,如果客户在每个分行都有账户,则此结果为空

WHERE NOT EXISTS
    (branch numbers that Adam Rivera owns) 
    EXCEPT 
    (a list of branch numbers that a customer has account at Adam Rivera's branches) 
使用WHERE后不存在,应返回拥有Adam Rivera所有帐户的客户

这就是我写的问题

SELECT c1.customerID, c1.lastName, c1.birthDate

FROM Customer c1 join Owns o1 on c1.customerID = o1.customerID join Account 
a1 on a1.accNumber = o1.accNumber join Branch b1 on b1.branchNumber = 
a1.branchNumber

WHERE NOT EXISTS

  (-- all branch numbers that Adam Rivera owns
  (SELECT b2.branchNumber 
  FROM Customer c2 join Owns o2 on c2.customerID = o2.customerID join 
Account a2 on a2.accNumber = o2.accNumber join Branch b2 on b2.branchNumber = a2.branchNumber
  WHERE c2.firstName = 'Adam' and c2.lastName = 'Rivera') 

  EXCEPT

  -- a list of branch numbers that a customer has account at Adam Rivera's branches
  (SELECT b3.branchNumber 
  FROM (Customer c3 join Owns o3 on c3.customerID = o3.customerID join Account a3 on a3.accNumber = o3.accNumber join Branch b3 on b3.branchNumber = a3.branchNumber)
  WHERE c3.firstName = 'Adam' and c3.lastName = 'Rivera' and b1.branchNumber = b3.branchNumber )
  )
查询应返回Hayes、Simmons和Hill。但是结果没有返回任何错误。我想不出这里出了什么问题

这里有一种方法:

with ar as (
      select distinct a.branchNumber
      from Customer c join
           Owns o 
           on c.customerID = o.customerID join
           Account a
           on a.accNumber = o.accNumber 
      where c.firstName = 'Adam' and c.lastName = 'Rivera'
     )
select o.customerId
from Owns o 
     Account a
     on a.accNumber = o.accNumber join
     ar
     on ar.branchNumber = a.branchNumber
group by o.customerId
having count(distinct a.branchNumber) = (select count(*) from ar);

子查询是Rivera先生拥有的帐户。联接匹配每个帐户。分组并计算每个客户的不同分支机构的数量,并检查该数量是否与Rivera先生的编号匹配。

用您正在使用的数据库标记您的问题。