将结果显示为3个类别
create table test
(
Type varchar(10),
CheckFlag varchar(10),
ID varchar(10)
)
Insert into test
values
('A','Yes','1'),
('A','Yes','2'),
('A','No','5'),
('A','No','6'),
('A','No','7'),
('B','Yes','8'),
('B','No','8'),
('B','No','9'),
('C','Yes','10'),
('C','Yes','11'),
('C','Yes','12'),
('C','No','10'),
('C','No','11'),
('C','No','12'),
('D','Yes','13'),
('D','Yes','14'),
('D','Yes','17'),
('D','No','14'),
('D','No','15'),
('D','No','16'),
('E','Yes','18'),
('E','Yes','19'),
('E','No','20'),
('E','No','21')
select distinct T1.Type
,case when count(*) = count(T2.ID)
then 'In'
else
'Using in and out'
end
from test T1
left join (select * from test
where CheckFlag= 'No') T2
on T1.Type= T2.Type
and T1.ID = T2.ID
Where T1.CheckFlag= 'Yes'
group by T1.Type
预期产出:
Type Category
A Out
B In
C In
D Out & In
E Out
这是现有的逻辑,B,C和C的类别。 D是正确的,但A& E不准确。这里的目标是显示每个类型,例如A,B,C,D,E,每个ID用于一个检查标志是否存在于每个DI中,用于检查标记号。
例如,
答案 0 :(得分:0)
你关闭了。
如果"是" es中没有" No" s,则需要" OUT"。也就是说' S:
WHEN COUNT(T2.ID) = 0 THEN 'OUT'
当某些 - 但不是全部 - "是" es在" No" s中时,你想要" OUT& IN&#34 ;.也就是说' S:
WHEN COUNT(T2.ID) > 0 AND COUNT (T2.ID) < COUNT(*) THEN "OUT & IN"
当所有&#34;是&#34; es都在&#34; No&#34; s中时,你想要&#34; IN&#34;。这就是其他一切:
ELSE 'In'