我正在尝试将这两个子查询内部连接起来(我认为这是它的所有内容),其中第一个查询的branchName等于第二个查询的branchName。
然而,他们似乎并不想加入一起,而且由于我对SQL的了解有限,我似乎无法找到解决这个问题的方法。我尝试在各种位置移动括号,但它也不喜欢。
SELECT *
FROM
(
SELECT B.branchName, A.type, AVG (T.amount), COUNT(A.accNumber)
FROM Branch B, Account A, Transactions T
WHERE
B.branchNumber = A.branchNumber AND
A.accNumber = T.accNumber
GROUP BY B.branchName, A.type
)
INNER JOIN
(
SELECT B1.branchName, COUNT(A1.accNumber)
FROM Account A1, Branch B1
WHERE
A1.branchNumber = B1.branchNumber
GROUP BY B1.branchName
HAVING COUNT(A1.accNumber) > 5
)
ON
B.branchName = B1.branchName
答案 0 :(得分:5)
为了使查询起作用,您需要命名内部选择(子查询):
SELECT *
FROM
(
SELECT B.branchName, A.type, AVG (T.amount), COUNT(A.accNumber)
FROM Branch B, Account A, Transactions T
WHERE
B.branchNumber = A.branchNumber AND
A.accNumber = T.accNumber
GROUP BY B.branchName, A.type
) q1
INNER JOIN
(
SELECT B1.branchName, COUNT(A1.accNumber)
FROM Account A1, Branch B1
WHERE
A1.branchNumber = B1.branchNumber
GROUP BY B1.branchName
HAVING COUNT(A1.accNumber) > 5
) q2
ON
q1.branchName = q2.branchName
答案 1 :(得分:1)
您应该为每个子查询设置名称列。试试这个:
SELECT *
FROM
(
SELECT B.branchName, A.type, AVG (T.amount) AS [AVG], COUNT(A.accNumber) AS [COUNT]
FROM Branch B, Account A, Transactions T
WHERE
B.branchNumber = A.branchNumber AND
A.accNumber = T.accNumber
GROUP BY B.branchName, A.type
) AS T1
INNER JOIN
(
SELECT B1.branchName, COUNT(A1.accNumber) AS [COUNT]
FROM Account A1, Branch B1
WHERE
A1.branchNumber = B1.branchNumber
GROUP BY B1.branchName
HAVING COUNT(A1.accNumber) > 5
) AS T2
ON
T1.branchName = T2.branchName