SELECT DISTINCT的第一行

时间:2018-06-27 05:18:13

标签: sql postgresql

我在使用SQL查询时遇到了一些麻烦。我有一个表来存储日期的结果。我想选择最长(最近)日期的所有不同结果。我可以使用:

select distinct(r.data), max(c.committed_at) as timestamp
from results r
     inner join commits c on (r.commit_id=c.id) 
where r.repository_id=65 AND data_type='data_model'
group by data
order by timestamp DESC

这很好,一切正常。

但是,我希望能够选择第三列,该列是一个哈希。该哈希对于每个结果行都是唯一的。

但是当我将其添加到SELECT子句中时,我当然也必须将其添加到我的GROUP BY中。

select distinct(r.data), max(c.committed_at) as timestamp, c.hash
from results r
     inner join commits c on (r.commit_id=c.id) 
where r.repository_id=65 AND data_type='data_model'
group by data, c.hash
order by timestamp DESC

这会将查询排除在外。我不再得到明显的结果(当然)。

我在hash上尝试了聚合函数:

select distinct(r.data), max(c.long_hash), max(c.committed_at) as timestamp
from results r
    inner join commits c on (r.commit_id=c.id) 
where r.repository_id=65 AND data_type='data_model'
group by data
order by timestamp DESC

但是然后我没有得到最大的时间,我得到了最大的 hash

我觉得我只是想说“找到带有最大时间戳的不同结果,然后在结果中包含哈希值”,但是我不知道如何...

干杯, 本

编辑:我用DISTINCT ON尝试这样做的是:

select distinct on (data) data, c.committed_at as committed_at, c.long_hash
from results r 
    inner join commits c on (r.commit_id=c.id) 
where r.repository_id=65 AND data_type='complexity'
order by data, c.committed_at DESC

但这需要在data上进行ORDER BY,这又将结果的顺序排除在外...

编辑2:

执行此查询将得到以下结果:

'{"status":"supported","per_method":"10.2","total":"815.2","data"<snip>...}','2017-01-04 13:25:51','4a44bccca804c28c6a5e61b36b5ebcb716d4c11f'
'{"status":"supported","per_method":"8.8","total":"649.3","data":<snip>...}','2017-01-02 23:35:11','d747e657a81c5c6da4262a5298c3071082b2af41'
'{"status":"supported","per_method":"10.7","total":"944.3","data":<snip>...}','2017-01-08 17:28:57','ff4be5fa6dc88237e7855ed1b534baee69aa8800'

如您所见,data列是有序的(根据JSONB列规则),而timestamp列则不是(从1月4日到1月2日再到1月8日) )。

2 个答案:

答案 0 :(得分:1)

尝试一下:

var array1 = [ {"name":"sam","surname":"sameera","location":"mumbai","age":"22"},{"name":"sam123","surname":"sameera123","location":"mumbai","age":"22"} ];
var array2 =[ {"name":"SAM","surname":"SAMEERA"},{"name":"SAM123","surname":"SAMEERA123"}];

var ret = [];

for(var i = 0; i < array1.length; i += 1) {
  var newArray = array2.filter(function (el) {
      return el.name.toUpperCase() == array1[i].name.toUpperCase() &&                                                  el.surname.toUpperCase() ==array1[i].surname.toUpperCase();
  });
  if(newArray.length ==0)
  {
     ret.push(array1[i]);    
  }
  else
  {
     array1[i].name = newArray[0].name; //change valuse
     array1[i].surname = newArray[0].surname; //change valuse
     ret.push(array1[i]);
  }
};
   

console.log(ret);

答案 1 :(得分:1)

您不需要将group byselect distinct on一起使用。您只需调整order by。所以:

select distinct on (r.data) r.*, c.*
from results r inner join
     commits c
     on r.commit_id = c.id 
where r.repository_id = 65 and
      data_type = 'data_model'
order by r.data, c.committed_at desc;

distinct on完成了您想做的所有工作。它为distinct on列表中的每种值组合返回一行。该行是订单依据指定的第一行。

我认为这是distinct(r.data)的目标,因为distinct本身不是函数,并且您不会在其中使用括号。