[英]Compare two tables with sorted and grouped rows with MySQL/MariaDB
[英]Rollup grouped rows in mariadb
我的數據是這樣的; 必須為每個主機名計算最終狀態,control_id -pass 如果為該主機名的所有 control_id 傳遞狀態
----------
hostname control_id status
abc standard pass
abc standard fail
abc premium pass
abc premium pass
abc classic fail
abc classic fail
xyz standard pass
xyz standard fail
xyz premium pass
xyz premium pass
xyz classic fail
xyz classic fail
I am not able to write a proper query
SELECT hostname, control_id,
MIN(status) AS overall_status
GROUP BY hostname, control_id
那是一個雜物。 它假定“失敗”<“通過”。
如果在我的第一個查詢中,您只需要失敗的查詢,請添加此子句:
HAVING overall_status = 'fail'
還有更復雜的方法,可能涉及使用LEFT
和/或NOT EXISTS
的自聯接。 撫摸我的自我,我會揮動我的魔杖來召喚出這樣的東西。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.