Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sybase SQL基于多个ID为的列选择Distinct_Sql_Sybase_Distinct - Fatal编程技术网

Sybase SQL基于多个ID为的列选择Distinct

Sybase SQL基于多个ID为的列选择Distinct,sql,sybase,distinct,Sql,Sybase,Distinct,我正在尝试查询sybase服务器,以获取用于测试目的的不同类型数据的示例 我有一张如下所示的表格(摘要) 正如我所提到的,我想要每种类型的一个示例,因此对于上表,我想要一个结果集,如(实际上,我只想要ID): 我尝试了多种查询组合,例如下面的查询,但它们要么是无效的SQL(对于sybase),要么返回无效的结果 SELECT id, DISTINCT ON type, breed FROM animals SELECT id, DISTINCT(type, breed) FROM ani

我正在尝试查询sybase服务器,以获取用于测试目的的不同类型数据的示例

我有一张如下所示的表格(摘要)

正如我所提到的,我想要每种类型的一个示例,因此对于上表,我想要一个结果集,如(实际上,我只想要ID):

我尝试了多种查询组合,例如下面的查询,但它们要么是无效的SQL(对于sybase),要么返回无效的结果

  SELECT id, DISTINCT ON type, breed FROM animals
  SELECT id, DISTINCT(type, breed) FROM animals
  SELECT id FROM animals GROUP BY type, breed
我还发现了其他问题,例如,但这只涉及一个专栏


您知道如何实现此查询吗?

对于列ID,您可能必须使用聚合函数
max
min
。对于分组的列,它将只返回一个ID

select max(Id), type, breed 
from animals
group by type, breed 
编辑:

其他不同的方法:

具有和聚合功能

select id, type, breed  
from animals 
group by type, breed  
having id = max(Id)
具有并聚合子查询

select id, type, breed 
from animals a1
group by type, breed 
having id = (
               select max(id)
               from animals a2
               where a2.type = a1.type
               and   a2.breed = a1.breed
            )

试试这个,让我知道它是否有效:

select distinct breed, max(id) as id , max(type) as type
from animals
您可能需要使用
max()
这里的任意选项是max(),但您可以任意使用min()。
返回该列的最大值,最小的

此操作按预期工作,但运行大约需要20分钟,这将使问题悬而未决,以搜索更优雅(和快速)的解决方案,但如果没有人有任何其他建议,则将接受!我找到了其他的方法。我不确定它是否会更快,也许是第二个建议。也许第一个建议更具可读性。我编辑了这篇文章。
select id, type, breed 
from animals a1
group by type, breed 
having id = (
               select max(id)
               from animals a2
               where a2.type = a1.type
               and   a2.breed = a1.breed
            )
select distinct breed, max(id) as id , max(type) as type
from animals