首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SQL Sever 2019 on Server Management Studio 2019

SQL Sever 2019 on Server Management Studio 2019
EN

Stack Overflow用户
提问于 2022-11-08 07:38:12
回答 1查看 37关注 0票数 0

我已经创建了下面的查询,但似乎没有工作,因为它显示了无效的对象名称“P”

编写查询,显示2015年1月至2016年1月期间所有居住在班加罗尔或浦那的租户的个人资料id、全名、电话、电子邮件id、城市、房屋id、move_in_date、move_out日期、租金、转介总数、最新雇主和所有租户的职业类别。

我已经创建了下面的查询,但似乎没有工作,因为它显示无效的对象名称'P‘。请帮帮忙。

代码语言:javascript
复制
With P as
(
select profile_id, first_name+ ' '+ last_name as Full_Name, phone, email_id, city
from Profiles$ 
where city in ('Bangalore','Pune')),
TH as (
select profile_id, house_id, move_in_date, move_out_date, rent
from Tenancy_History
where move_in_date >= '2015-01-01' AND move_in_Date<= '2016-01-31'),
ES as (
select profile_id, latest_employer, occupational_category
from Employment_Details$),
R as (
select profile_id, sum(referral_valid) as "Total Referral"
from Referral$
group by profile_id)


select P.profile_id, P.Full_Name, P.phone, P.email_id, P.city, TH.house_id, TH.move_in_date, TH.move_out_date, TH.rent, R.Total Referral, ES.latest_employer, ES.occupational_category
from P
inner join TH on 
P.profile_id=TH.profile_id
inner join ES on
P.profile_id=ES.profile_id
inner join R on
P.profile_id=R.profile_id
EN

回答 1

Stack Overflow用户

发布于 2022-11-08 16:39:06

不需要使用公共表表达式

代码语言:javascript
复制
select   P.profile_id
      ,  P.Full_Name
      ,  P.phone
      ,  P.email_id
      ,  P.city
      ,  TH.house_id
      ,  TH.move_in_date
      ,  TH.move_out_date
      ,  TH.rent
      ,  sum (R.referral_valid) as [Total Referral]
      ,  ES.latest_employer
      ,  ES.occupational_category
from     Profiles$            P
join     Tenancy_History      TH on P.profile_id=TH.profile_id
join     Employment_Details$  ES on P.profile_id=ES.profile_id
join     Referral$            R  on P.profile_id=R.profile_id
where    city in ('Bangalore','Pune')
  and    TH.move_in_date >= '2015-01-01' 
  AND    TH.move_in_Date <= '2016-01-31'
GROUP BY P.profile_id
    ,    P.Full_Name
    ,    P.phone
    ,    P.email_id
    ,    P.city
    ,    TH.house_id
    ,    TH.move_in_date
    ,    TH.move_out_date
    ,    TH.rent
    ,    ES.latest_employer
    ,    ES.occupational_category
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74357137

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档