![](/img/trans.png)
[英]get the column value for the two consecutive rows having the same other column values
[英]Select unique Records based on a column by conditionally removing the other record having same column value based on some other coumn in sql
我正在開發一個 Android 應用程序,我在其中使用 Room 持久性庫。 在其中一個表(房間實體)中,我有一種情況,我想通過有條件地刪除基於其他列的具有相同列值的其他記錄來基於列 select 唯一記錄。
考慮下表作為基礎。
S_ID | 地位 |
---|---|
1 | 待辦的 |
1 | 被拒絕 |
3 | 得到正式認可的 |
4 | 得到正式認可的 |
5 | 待辦的 |
6 | 被拒絕 |
7 | 被拒絕 |
SQL 查詢的預期結果,如果其 S_ID 與另一個“待定”狀態記錄沖突,我希望從結果中刪除具有“ Rejected'
狀態的記錄
S_ID | 地位 |
---|---|
1 | 待辦的 |
3 | 得到正式認可的 |
4 | 得到正式認可的 |
5 | 待辦的 |
6 | 被拒絕 |
7 | 被拒絕 |
到目前為止我所嘗試的:
select DISTINCT s_id OR s_id = 0, * from TABLE1
我認為您可以嘗試以下查詢:
SELECT * FROM table1 WHERE table1.S_ID in (SELECT S_ID FROM table1 GROUP BY S_ID HAVING COUNT(S_ID)>0) and table1.[status]<>'Rejected'
編輯:
SELECT table1.S_ID,
(select Alias.[Status] from Table1 Alias where Alias.S_ID=table1.S_ID
and ((COUNT(table1.S_ID)>1 and Alias.[Status]<>'Rejected') or COUNT(table1.S_ID)=1)
) as [Status]
FROM table1 GROUP BY S_ID HAVING COUNT(S_ID)>0
您可以為此使用聚合:
select s_id, min(status) as status
from table_id
group by s_id;
這使用了字母順序用於max()
和'Pending' < 'Rejecdted'
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.