简体   繁体   English

Select 基于列的唯一记录,通过有条件地删除基于 sql 中的某些其他列值的具有相同列值的其他记录

[英]Select unique Records based on a column by conditionally removing the other record having same column value based on some other coumn in sql

I am developing an Android app in which I am using the Room persistence library.我正在开发一个 Android 应用程序,我在其中使用 Room 持久性库。 In one of the Tables (Room Entity), I have a case where I want to select unique Records based on a column by conditionally removing the other record having the same column value based on some other column.在其中一个表(房间实体)中,我有一种情况,我想通过有条件地删除基于其他列的具有相同列值的其他记录来基于列 select 唯一记录。

Consider the following table as a base.考虑下表作为基础。

S_ID S_ID STATUS地位
1 1 Pending待办的
1 1 Rejected被拒绝
3 3 Approved得到正式认可的
4 4 Approved得到正式认可的
5 5 Pending待办的
6 6 Rejected被拒绝
7 7 Rejected被拒绝

the expected result of the SQL query where I want a record with ' Rejected' status to be removed from results if its S_ID collides with another 'Pending' status record SQL 查询的预期结果,如果其 S_ID 与另一个“待定”状态记录冲突,我希望从结果中删除具有“ Rejected'状态的记录

S_ID S_ID STATUS地位
1 1 Pending待办的
3 3 Approved得到正式认可的
4 4 Approved得到正式认可的
5 5 Pending待办的
6 6 Rejected被拒绝
7 7 Rejected被拒绝

what I have tried so far:到目前为止我所尝试的:

select DISTINCT s_id OR s_id = 0, * from TABLE1

I think You can try below query:我认为您可以尝试以下查询:

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'

Edited:编辑:

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

You can use aggregation for this:您可以为此使用聚合:

select s_id, min(status) as status
from table_id
group by s_id;

This uses the fact that alphabetical ordering is used for max() and 'Pending' < 'Rejecdted' .这使用了字母顺序用于max()'Pending' < 'Rejecdted'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 获取具有相同其他列值的两个连续行的列值 - get the column value for the two consecutive rows having the same other column values Android Room TypeConverter 可选择基于其他列值加密字符串列 - Android Room TypeConverter to encrypt String column optionally based on other column value 如何根据Salesforce中其他某个选择列表的选定值过滤选择列表 - How to filter a picklist based on the selected value of some other picklist in salesforce 根据相同活动的其他片段提供的输入数据在片段中添加或删除项目 - Adding or removing items in fragment based on input data given by other fragment of same activity 在sqlite android中按其他列搜索列值 - Searching a column value by other column in sqlite android 如何使用uiautomator从同一父对象中基于其他对象选择特定对象 - How to select a specific object based on other object from the same parent using uiautomator 根据其他TextView更改TextView值 - Change the TextView value based on other TextView 根据字符串列选择不同的第一个字符 - Select distinct first character based on the string column 根据两个条件删除sqlite表中的特定记录:_id和column - Delete specific record in sqlite table based on two criteria: _id and column 如何在Android中的sqlite3中基于一列获取记录 - How to get records based on one column in sqlite3 in android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM