[英]Sql query find all rows where value is null but another row wih same value not null (Mariadb)
[英]Sql query find rows with value of 2 dependent of count of another value in row
我有一個員工表,上面列出了他們的職位編號和一個名為 position_sequence 的字段,該字段將員工的職位分配為 1 或 2。因此,只有當員工有 2 個職位時,才應分配 1 個職位,另一行的值應為 2。
但是,我想找到一個查詢來查找員工只有1個職位的數據的所有問題,但職位序列是2而不是1。
正確數據(2個位置):
employee position sequence
------------------------------------
bob builder 1
bob cleaner 2
不正確的示例(1 個位置但標記為 2 而不是 1):
employee position sequence
------------------------------------
sally builder 2
您可以使用not exists
:
select t.*
from t
where sequence = 2 and
not exists (select 1 from t t2 where t2.employee = t.employee and t2.sequence = 1);
如果您想要一般的“序列是否丟失”,則生成序列並顯示異常:
select t.*
from (select t.*,
row_number() over (partition by employee order by sequence) as imputed_sequence
from t
) t
where imputed_sequence <> sequence;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.