Sybase,显示所有行,但在重复

时间:2017-03-22 16:03:32

标签: sql sybase-ase

产品 :Sybase ASE 11/12/15/16

我希望更新由不同应用程序调用的存储过程,因此更改应用程序不是一个选项。所需内容最好在例子中解释:

目前的结果:

type | breed            | name
------------------------------------
dog  | german shepherd  | Bernie
dog  | german shepherd  | James
dog  | husky            | Laura
cat  | british blue     | Mr Fluffles
cat  | other            | Laserchild
cat  | british blue     | Sleepy head
fish | goldfish         | Goldie

我需要的是第一列的数据在重复上被清除。例如,上述数据应如下所示:

type | breed            | name
------------------------------------
dog  | german shepherd  | Bernie
     | german shepherd  | James
     | husky            | Laura
cat  | british blue     | Mr Fluffles
     | other            | Laserchild
     | british blue     | Sleepy head
fish | goldfish         | Goldie

我知道我可以做一个光标,但是大约有10,000条记录并且看起来并不精通。要查找选择命令,不要更改数据库中的数据。

1 个答案:

答案 0 :(得分:0)

在仔细考虑之后,我找到了一个可行的解决方案而不使用游标。

select Type,breed,name
    into #DontDisplay
    from #MyDataList as a1
    group by breed
    Having breed= (select max(name)
                            from #MyDataList a2
                                where a1.breed= a2.breed)
    order by breed, name

    select n.Type,d.Breed,d.Name 
    from #MyDataList as d
    left join #DontDisplay as n
    on d.Breed= n.Breed and d.Name= n.Name
    order by Breed

效果很好,解决方案基于另一种解决方案Sybase SQL Select Distinct Based on Multiple Columns with an ID