运行Postgres查询按一个字段分组并按另一个字段排序

时间:2016-01-10 05:39:10

标签: sql postgresql aggregate greatest-n-per-group window-functions

我有一个PostgreSQL表,其中包含以下相关字段:

[FindsBy(How = How.Id, Using = "SomeID")]
public IWebElement SearchButton { get; set; }

可能有许多行包含相同的网址但标题不同。以下是一些示例行:

url
title
created_at

我试图获取列出以下字段的输出:

1)www.nytimes.com | The New York Times | 2016-01-01 00:00:00` www.wsj.com | The Wall Street Journal | 2016-01-03 15:32:13` www.nytimes.com | The New York Times Online | 2016-01-06 07:19:08`
2)url对应于title的最高值 3)该唯一created_at

的所有title的计数

因此,上面示例的输出行看起来像这样:

url

根据我在类似问题上阅读过的众多SO帖子,看来我获得前两个字段(www.nytimes.com | The New York Times Online | 2 www.wsj.com | The Wall Street Journal | 1 和最新url)的最佳选择是使用{ {1}}:

title

同样,要获取第一个和第三个字段(DISTINCT ON和所有select distinct on (url) url, title from headlines order by url, created_at desc 的计数),我只需使用url

title

我无法弄清楚如何结合上述方法并获得上述三个我想要获得的价值。

(编辑提供更多清晰度。)

3 个答案:

答案 0 :(得分:5)

通过将query_stringSELECT结合起来,可以在一个DISTINCT ON内完成一次SELECT DISTINCT ON (url) url, title, count(*) OVER (PARTITION BY url) AS ct FROM headlines ORDER BY url, created_at DESC NULLS LAST;

var mergedYaml

gulp.task('LoadYamlFiles', function() {
mergedYaml = global.mergedYaml;
    try {
        //Load all the Yaml files available inside pom folder
        glob("tests/acceptance/wdio/utilities/pom/*.yml", function (er, files) {
            mergedYaml = yamlMerge.mergeFiles(files);
        })
    }
    catch (e){
        console.log(e);
    }
});

exports.mergedYaml = mergedYaml

window function

相关(详细说明):

答案 1 :(得分:3)

尝试;

select t1.url, t2.title, t1.cnt
from (
  select url, count(title) cnt 
  from headlines 
  group by url
) t1
join (
  select distinct on (url) url, title 
  from headlines 
  order by url, created_at desc
) t2 on t1.url = t2.url
order by t1.url

join url

上的两个查询

sql fiddle demo

答案 2 :(得分:1)

试试这个:

{{1}}

SQL小提琴:PostgreSQL: running count of rows for a query 'by minute'