查询以拾取某些条件记录



我有以下表格数据

ID         Type Code     Opt    Line    Status
26985444    1   100        1       1    S0
26987422    1   25         1       1    S0
26987422    1   25         2       1    S1
26987422    1   25         2       2    S2
26987422    4   25         2       3    S0
26987422    2   30         1       1    S1
26987422    2   30         1       2    S2
26987422    2   30         1       3    S0
26987422    3   35         1       1    S0
26985333    1   75         1       1    S0
26985000    1   55         1       1    S0
26985000    1   65         1       1    S0

在以上内容中,我只需要选择以下记录

26985444    1   100 1   1   S0
26985333    1   75  1   1   S0

如何对此编写SQL查询。

感谢

我将您的问题解释为查找只在数据中出现一次的ID

你可以通过聚合来实现这一点,寻找单体:

select ID, min(Type) as Type, min(Code) as Code, min(Opt) as Opt,
       min(Line) as Line, min(Status) as Status
from t
group by id
having count(*) = 1;

如果您的Sybase版本支持窗口功能:

select ID, Type, Code, Opt, Line, Status
from (select t.*,
             count(*) over (partition by id) as cnt
      from t
     ) t
where cnt = 1

我在上表中看到的唯一排序是,如果在将Code字段转换为char()时按其进行排序。以下是使用max()min():可能要查找的内容的猜测

select y.* 
from yourtable y
  join (
    select max(cast(Code as char(3))) maxCode,
      min(cast(Code as char(3))) minCode
    from yourtable
    ) t on y.code = t.maxCode or y.code = t.minCode
  • 浓缩Fiddle示例

最新更新