我有一个查询,我想使用结果来搜索另一个表中的其他详细信息并一起显示结果
我的初步查询是显示所有帐户以及指定日期的预订数量
SELECT ACCOUNT, COUNT(ACCOUNT) Bookings
FROM dbo.bookings
WHERE DATEADDED = 101
GROUP BY ACCOUNT
然后我想使用"帐户"这是在dbo.accounts中搜索帐户名称的代码.....
ACCOUNT等于dbo.accounts表中的CODE,我需要从此表中获取NAME
答案 0 :(得分:2)
听起来你只想把两张桌子连在一起,所以:
SELECT a.NAME, b.ACCOUNT, COUNT(*) Bookings
FROM dbo.bookings b
INNER JOIN dbo.accounts a ON a.CODE = b.ACCOUNT
WHERE b.DATEADDED = 101
GROUP BY a.NAME, b.ACCOUNT
答案 1 :(得分:0)
你一定在寻找这样的东西:
SELECT b.ACCOUNT
,a.NAME
,COUNT(ACCOUNT) Bookings
FROM dbo.bookings b
JOIN dbo.accounts a ON b.account = a.code
WHERE DATEADDED = 101
GROUP BY b.ACCOUNT
,a.NAME
或者,如果您使用的是SQL Server,则可以使用CTE:
WITH b(SELECT ACCOUNT, COUNT(ACCOUNT) Bookings
FROM dbo.bookings
WHERE DATEADDED = 101
GROUP BY ACCOUNT)
SELECT a.NAME
,b.*
FROM b
JOIN dbo.accounts a ON b.account = a.code;