在这上面花了一段时间仍然无法完全理解。我在这样的表中有一些行:
uuid, pos (R|S|N), number (1-10), account (string), xtype (string)
唯一行由uuid, pos,
数字标识
我正在尝试编写一个可以找到以下情况的查询:
希望我提供了足够的信息,如果您需要更多信息,请告诉我。
您正在寻找符合特定条件的记录的 uuid。所以你聚合每个 uuid (即GROUP BY
)。
select uuid
from mytable
group by uuid
having count(case when account = 'x' and pos = 'R' and number = 1 then 1 end) > 0
and count(case when account = 'x' and pos = 'S' and number = 1 then 1 end) > 0
and count(case when pos = 'N' then 1 end) = 0
and count(case when pos = 'R' and number <> 1 then 1 end) = 0;
(实际上uuid
+pos
是唯一的,您可以删除最后一个条件,因为它包含在第一个条件中,如果您觉得更清楚,可以更改> 0
为= 1
。)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句