我有一个UI和服务器组件。在UI中,我需要显示一个project_id到case_no关系(一对多),以便选择类似下面的内容
project_id_1
-case_no_1
-case_no_2
project_id_2
_case_no_3
...
在服务器端,我有groovy使用下面的查询查询postgresql。此查询无法为project_id
提供多个case_nosSELECT DISTINCT case_no,
project_no
FROM
(SELECT lm."PROJECT_ID" AS project_no,
case_no
FROM filter_analysis_store fas
LEFT JOIN share_analysis_store sas USING (fa_identifier)
JOIN "LOG_METADATA" lm
ON fas.case_no = lm."CASE_NO"
WHERE fas.created_by = 'a@a.com'
OR sas.shared_to = 'a@a.com'
OR lm."UPLOADED_BY" like 'a@a.com'
ORDER BY lm."PROJECT_ID"
) tmp;
查询输出
0203996, 1-1532464
02029418, 1-53795134
01912591, 1-22432322
0199845, 2-00073626
0203959, 1-2225633
01995271, 1-1755865
01757560, 0-10060 <-- This project has multiple casenos
在postgresql中有3个表
filter_analysis_store
fa_identifier,case_no,created_by
FA000179, 0199845, a@a.com
FA000183, 0199845, a@a.com
FA000184, 0203996, a@a.com
FA000185, 0203996, a@a.com
FA000187, 0203959, a@a.com
FA000189, 0203996, a@a.com
FA000190, 02029418, a@a.com
FA000191, 02029418, a@a.com
FA000192, 0203959, a@a.com
FA000193, 0203959, a@a.com
FA000194, 0203959, a@a.com
FA000195, 01912591, a@a.com
FA000199, 0203959, a@a.com
share_analysis_store
fa_identifier,shared_to,shared_by
FA000173, a@a.com, p@p.com
FA000196, a@a.com, p@p.com
FA000180, a@a.com, r@r.com
FA000198, a@a.com, r@r.com
FA000203, a@a.com, r@r.com
FA000204, a@a.com, r@r.com
“LOG_METADATA”
id,"PROJECT_ID","CASE_NO"
1,0-10000, 01757560
2,0-10000, 02021680
3,1-1532464,0203996
使用上述3个表我需要在created_by“a@a.com”或shared_to“a@a.com”上显示与where子句的项目和案例关系
注意:project_id
下可能有多个case nos请帮我查询一个可以容纳project_id的多个case_nos的查询。
答案 0 :(得分:0)
您的查询对我来说没问题。也许您没有看到预期的结果,因为order by
正在应用于临时表而不会在结果中进行转换。否则,必须存在一些未在示例数据中表示的其他问题。
例如,此查询应该给出相应的结果(只要案例编号具有匹配的“a@a.com”):
SELECT DISTINCT project_no, case_no
FROM (
SELECT lm."PROJECT_ID" AS project_no, fas.case_no
FROM "LOG_METADATA" lm
JOIN filter_analysis_store fas ON fas.case_no = lm."CASE_NO"
LEFT JOIN share_analysis_store sas USING (fa_identifier)
WHERE (fas.created_by = 'a@a.com' OR sas.shared_to = 'a@a.com')
) tmp
where project_no = '0-10000'
ORDER BY project_no
0-10000, 01757560
0-10000, 02021680
这样的查询可能会帮助您查看应该导致多个条目的内容:
select PROJECT_ID, string_agg(CASE_NO, ',') as cases, count(*) as count
from LOG_METADATA
group by PROJECT_ID
having count(*) > 1