PostgreSQL - 如何从多个字段ID返回结果?

时间:2015-05-18 21:19:53

标签: sql postgresql

我想重写此查询,以便我可以返回四个字段ID的结果:行业,位置,电话号码,SFDC联系人ID。

在底部,你会看到我注释掉了我要表达的其他列。由于目前的编写方式,我一次只能显示一个,并且必须相应地取消注释/评论。一个重要的考虑因素是写这个,以便显示可用于行业,位置,电话号码或SFDC联系人ID的所有记录(而不是仅显示通过隐含的"和##全面提供数据的记录。 34。)

作为奖励,电话号码值确实包括类型。有什么方法可以删除吗?例如:" 123-456-7890 | work" ......我只想要这是" 123-456-7890"但最糟糕的情况是,我总是可以在Excel中删除它。

我很感激任何帮助。

select cmsuser.userid as "User ID", 
cmsuser.username as "Username", 
cmsuser.firstname as "First Name", 
cmsuser.lastname as "Last Name", 
cmsuser.email as "Email", 
cmsuser.userenabled as "Enabled?",
to_char(date(to_timestamp(cmsuser.creationdate/1000)),'YYYY-MM-DD') as "Creation Date",
to_char(date(to_timestamp(cmsuser.modificationdate/1000)),'YYYY-MM-DD') as "Modification Date", 
to_char(date(to_timestamp(cmsuser.lastloggedin/1000)),'YYYY-MM-DD') as "Last Logged In", 
to_char(date(to_timestamp(cmsuser.lastprofileupdate/1000)),'YYYY-MM-DD') as "Last Profile Update",
---****** Industry ******
cmsuserprofile.value as "Industry"
from cmsuser, cmsuserprofile 
where cmsuser.userid = cmsuserprofile.userid and cmsuserprofile.fieldid = '5011' and cmsuser.userenabled = '1';
---****** Location ******
---cmsuserprofile.value as "Location"
---from cmsuser, cmsuserprofile 
---where cmsuser.userid = cmsuserprofile.userid and cmsuserprofile.fieldid = '6' and cmsuser.userenabled = '1';
---****** Phone Number ******
---cmsuserprofile.value as "Phone Number"
---from cmsuser, cmsuserprofile 
---where cmsuser.userid = cmsuserprofile.userid and cmsuserprofile.fieldid = '1' and cmsuser.userenabled = '1';
---****** SFDC Contact ID ******
---cmsuserprofile.value as "SFDC Contact ID"
---from cmsuser, cmsuserprofile 
---where cmsuser.userid = cmsuserprofile.userid and fieldid = '5004' and cmsuser.userenabled = '1';

1 个答案:

答案 0 :(得分:0)

select 
  u.userid as "User ID", 
  u.username as "Username", 
  u.firstname as "First Name", 
  u.lastname as "Last Name", 
  u.email as "Email", 
  u.userenabled as "Enabled?",
  to_char(date(to_timestamp(u.creationdate/1000)),'YYYY-MM-DD') as "Creation Date",
  to_char(date(to_timestamp(u.modificationdate/1000)),'YYYY-MM-DD') as "Modification Date", 
  to_char(date(to_timestamp(u.lastloggedin/1000)),'YYYY-MM-DD') as "Last Logged In", 
  to_char(date(to_timestamp(u.lastprofileupdate/1000)),'YYYY-MM-DD') as "Last Profile Update",
  (select p.value from cmsuserprofile p where u.userid = p.userid and p.fieldid = '5011') as "Industry",
  (select p.value from cmsuserprofile p where u.userid = p.userid and p.fieldid = '6') as "Location",
  (select p.value from cmsuserprofile p where u.userid = p.userid and p.fieldid = '1') as "Phone Number",
  (select p.value from cmsuserprofile p where u.userid = p.userid and p.fieldid = '5004') as "SFDC Contact ID"
from cmsuser u where u.userenabled = '1';