如果列中包含特定值,如何从子查询中选择

时间:2018-08-30 03:46:31

标签: postgresql subquery resultset

我想问一问是否可以从结果集中再次选择列是否包含特定值?

例如,从下面的查询中,我想将其选择为子查询,并检查该子查询的第一列是否同时包含2和3结果。否则,将不返回任何值。

select e.evaluator_id, ROUND(avg(cast(e.rating_score as int))::numeric,1)::varchar, c.q_category_name 
from tms.t_evaluation e    
inner join tms.m_q_category c    
on e.nendo=c.nendo    
and e.q_category_id = c.q_category_id    
and c.delete_flg = '0'
inner join tms.m_q_subcategory qs
on e.q_category_id = qs.q_category_id
and e.q_subcategory_id = qs.q_subcategory_id
and c.nendo = qs.nendo
and qs.delete_flg = '0'
where e.nendo = '2018'
and e.empl_id = 'empl05'    
and e.delete_flg = '0'
and e.evaluator_id in ('2' , '3') 
group by e.empl_id, e.nendo, e.q_category_id, 
c.q_category_name, e.evaluator_id, e.history_no

结果在第一列中同时包含2和3。这可能吗?

1 个答案:

答案 0 :(得分:0)

select e.evaluator_id, ROUND(avg(cast(e.rating_score as int))::numeric,1)::varchar, c.q_category_name 
from tms.t_evaluation e    
inner join tms.m_q_category c    
on e.nendo=c.nendo    
and e.q_category_id = c.q_category_id    
and c.delete_flg = '0'
inner join tms.m_q_subcategory qs
on e.q_category_id = qs.q_category_id
and e.q_subcategory_id = qs.q_subcategory_id
and c.nendo = qs.nendo
and qs.delete_flg = '0'
where e.nendo = '2018'
and e.empl_id = 'empl05'    
and e.delete_flg = '0'
and e.evaluator_id in (select case when evaluator_id=2 or evaluator_id=3 then evaluator_id else null from t_evaluation order by evaluator_id asc) 
group by e.empl_id, e.nendo, e.q_category_id, 
c.q_category_name, e.evaluator_id, e.history_no