![](/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.