子查询,加入一个查询
Sub-queries, joins in one query
我有五张桌子。我需要从他们那里获取数据。 Table 'Tenancy_histories' 包含 move_in_date、move_out_date、租金列。 'Profiles' 包含 first_name、last_name、电子邮件、profile_id 等。'Referrals' 包含 referrer_bonus_amount 和类似的其他数据。最重要的是,它包含特定 profile_id 进行的推荐次数,即 profile_id 在 'referrer_id(same as profile id)' 列中出现的次数。 'Employment_details' 包含最新的雇主,职业类别
我需要编写查询以显示个人资料 ID、全名、phone、电子邮件 ID、城市、房屋 ID、move_in_date、move_out 日期、租金、总数2015 年 1 月至 2016 年 1 月期间居住在某个特定城市的所有租户的转介次数、最新雇主和职业类别按租金降序排列
试过这样的事情:
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"。内部连接部分有问题。也许 INNER JOIN 不是检索数据的正确方法
这是你想要的吗:
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
注意:您不应该使用 convert(date, 'Jan 2015')
,而是使用 convert(date,'20150101',112)
之类的东西,因为它可以在服务器上运行,并在另一个服务器上引发错误...搜索 "datetime implicit conversion" 有关此的更多详细信息。
我有五张桌子。我需要从他们那里获取数据。 Table 'Tenancy_histories' 包含 move_in_date、move_out_date、租金列。 'Profiles' 包含 first_name、last_name、电子邮件、profile_id 等。'Referrals' 包含 referrer_bonus_amount 和类似的其他数据。最重要的是,它包含特定 profile_id 进行的推荐次数,即 profile_id 在 'referrer_id(same as profile id)' 列中出现的次数。 'Employment_details' 包含最新的雇主,职业类别
我需要编写查询以显示个人资料 ID、全名、phone、电子邮件 ID、城市、房屋 ID、move_in_date、move_out 日期、租金、总数2015 年 1 月至 2016 年 1 月期间居住在某个特定城市的所有租户的转介次数、最新雇主和职业类别按租金降序排列 试过这样的事情:
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"。内部连接部分有问题。也许 INNER JOIN 不是检索数据的正确方法
这是你想要的吗:
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
注意:您不应该使用 convert(date, 'Jan 2015')
,而是使用 convert(date,'20150101',112)
之类的东西,因为它可以在服务器上运行,并在另一个服务器上引发错误...搜索 "datetime implicit conversion" 有关此的更多详细信息。