Sql 子查询,在一个查询中联接
我有五张桌子。我需要从他们那里获取数据。表“租约历史记录”包含“入住日期”、“搬出日期”和“租金”列“个人资料”包含名字、姓氏、电子邮件、个人资料id等。“推荐”包含推荐人奖金金额和类似的其他数据。最重要的是,它包含特定配置文件id所做的转介数量,即该配置文件id在“referer\u idname as profile id”列中出现的数量Employment_details'包含最新的雇主、职业类别 我需要写一个查询来显示个人资料id、全名、电话、电子邮件id、城市、房屋id、入住日期、迁出日期、租金、推荐总数、最近的雇主以及2015年1月至2016年1月期间居住在某个特定城市的所有租户的职业类别,按租金降序排列 试过这样的方法:Sql 子查询,在一个查询中联接,sql,sql-server,sql-server-2005,subquery,inner-join,Sql,Sql Server,Sql Server 2005,Subquery,Inner Join,我有五张桌子。我需要从他们那里获取数据。表“租约历史记录”包含“入住日期”、“搬出日期”和“租金”列“个人资料”包含名字、姓氏、电子邮件、个人资料id等。“推荐”包含推荐人奖金金额和类似的其他数据。最重要的是,它包含特定配置文件id所做的转介数量,即该配置文件id在“referer\u idname as profile id”列中出现的数量Employment_details'包含最新的雇主、职业类别 我需要写一个查询来显示个人资料id、全名、电话、电子邮件id、城市、房屋id、入住日期、迁出
select pr.first_name+' '+pr.last_name as full_name,
pr.email,
pr.phone,
pr.profile_id,
th.house_id,
th.move_in_date,
th.move_out_date,
th.rent,
ed.latest_employer,
ed.Occupational_category,
ref.cnt
from Profiles pr,
Tenancy_histories th,
Employment_details ed
INNER JOIN (select [referrer_id(same as profile id)],
count([referrer_id(same as profile id)]) as cnt
from Referrals
group by [referrer_id(same as profile id)]) as ref
on pr.profile_id = ref.[referrer_id(same as profile id)]
where pr.profile_id = th.profile_id
and th.profile_id = ed.profile_id
and pr.profile_id IN
(select profile_id
from Tenancy_histories
where move_in_date >= convert(date, 'Jan 2015')
and move_out_date <= convert(date, 'Jan 2016'))
获取错误:
无法绑定多部分标识符pr.profile_id。内部连接部分有问题。也许内部联接不是检索数据的正确方法这就是您想要的:
SELECT pr.first_name+' '+pr.last_name as full_name,
pr.email,
pr.phone,
pr.profile_id,
th.house_id,
th.move_in_date,
th.move_out_date,
th.rent,
ed.latest_employer,
ed.Occupational_category,
count(ref.profile_id)
FROM Profiles pr
INNER JOIN Tenancy_histories th ON (pr.profile_id = th.profile_id AND move_in_date >= convert(date, 'Jan 2015') AND move_out_date <= convert(date, 'Jan 2016'))
INNER JOIN Employment_details ed ON (th.profile_id = ed.profile_id)
LEFT JOIN Referrals as ref ON (pr.profile_id = ref.profile_id)
GROUP BY pr.first_name+' '+pr.last_name,
pr.email,
pr.phone,
pr.profile_id,
th.house_id,
th.move_in_date,
th.move_out_date,
th.rent,
ed.latest_employer,
ed.Occupational_category
注意:您不应该使用convertdate,'Jan 2015',而应该使用类似convertdate,'20150101',112的内容,因为它可以在服务器上工作,并在另一台服务器上引发错误。。。搜索datetime隐式转换以了解有关此操作的详细信息。为什么要混合隐式和显式连接语法?将隐式联接更改为显式联接,然后尝试运行查询。还要避免使用隐式连接语法。