2017-09-30 62 views
0

我有五个表。我需要从所有这些数据中获取数据。表'Tenancy_histories'包含move_in_date,move_out_date,租金列。 'Profiles'包含名字,姓氏,电子邮件,profile_id等。'Referrals'包含referrer_bonus_amount和类似的其他数据。最重要的是,它包含由特定的profile_id所做的推荐数量,这是“referrer_id(与配置文件ID相同)”列中该profile_id的发生次数。 'Employment_details'包含最新的雇主,职业类别子查询,加入一个查询

我需要编写一个查询来显示个人资料ID,全名,电话,电子邮件ID,城市,房屋ID,move_in_date,move_out日期,租金,最新的雇主,都生活在2015年1月至jan2016通过他们的租金排序的时间段一些特定城市住户的降序排列 试过像这样的occupationalcategory:

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”不能是b ound。内部连接部分有问题。也许INNER JOIN是不检索数据

+1

你为什么要混合隐式和显式'join'语法?将隐式连接更改为显式,然后尝试运行您的查询。同时避免使用隐式连接语法。 – zarruq

回答

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, 
     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”以获取更多关于此的详细信息。