如何将查询结果的列结果与表列值进行匹配,并从同一个表中获取更多列?

时间:2017-03-01 10:35:47

标签: sql postgresql

这是我之前的question

的延续

查询如下,取自this answer

select distinct on (fa_identifier) fa_identifier, fas.case_no
from   filter_analysis_store fas
left join   share_analysis_store sas using (fa_identifier)
where  fas.created_by = 'a@a.com'
or     sas.shared_to  = 'a@a.com';

结果

FA000173    02029418
FA000179    01998455
FA000180    01757560
FA000183    01998455
FA000184    02039960
FA000185    02039960
FA000187    02039596
FA000189    02039960
FA000190    02029418
FA000191    02029418
FA000192    02039596
FA000193    02039596
FA000194    02039596
FA000195    01912596
FA000196    01912596
FA000198    01995271
FA000199    02039596

另一个表名是“LOG_METADATA”,为简单起见,我只为这个表提供了几列

id,PROJECT_ID,CASE_NO
1,2-0007362,02029418
2,2-0007362,01998455
3,1-5379513,02039596
...

因此,对于给定的项目,我需要列出所有案例,这里项目是一个数组,如果我们在视觉上解释它,每个项目都可以有多个案例。在UI中截图如下所示。 enter image description here

更新

这是我正在尝试的内容

SELECT "PROJECT_ID" AS PROJECT_NO,
  "CASE_NO"
FROM "LOG_METADATA" lm
JOIN
  (SELECT DISTINCT
  ON (fa_identifier) fa_identifier,
    fas.case_no AS "CASE_NO"
  FROM filter_analysis_store fas
  LEFT JOIN share_analysis_store sas USING (fa_identifier)
  WHERE fas.created_by = 'a@a.com'
  OR sas.shared_to     = 'a@a.com'
  ) res USING ("CASE_NO");

2 个答案:

答案 0 :(得分:0)

您可以在没有group

的情况下使用distinct
select fa_identifier, fas.case_no
from   filter_analysis_store fas
left join   share_analysis_store sas using (fa_identifier)
where  fas.created_by = 'a@a.com'
or     sas.shared_to  = 'a@a.com'
group by fa_identifier, fas.case_no;

答案 1 :(得分:0)

您不需要进行子选择,只需仔细选择输出列:

select    distinct lm.project_id, case_no
from      filter_analysis_store fas
left join share_analysis_store sas using (fa_identifier)
join      log_metadata lm using (case_no)
where     fas.created_by = 'a@a.com'
or        sas.shared_to  = 'a@a.com';

如果您不想在多个case_no下显示project_id,请改为select distinct on (case_no) lm.project_id, case_no。在这种情况下,您可以控制project_id case_no ORDER BY@Echo off&SetLocal EnableExtensions EnableDelayedExpansion set cnt=0 For /F "Skip=1 Delims=" %%A In ( '"WMIC diskdrive Where (MediaType="Fixed hard disk media") Get Size"' ) Do For /F "Tokens=1-2" %%B In ("%%A") Do ( Set /A cnt+=1 set _hdd[!cnt!]=%%B ) Set _hdd[ pause 一起显示的位置。