简体   繁体   English

PostgreSQL按日期排序但随机

[英]Postgresql Order By Date But At Random

I'm trying to pull data and arrange it by date but have it appear in a random order. 我正在尝试提取数据并按日期排列,但以随机顺序显示。 I've tried 我试过了

SELECT * FROM table ORDER BY article_date_added::DATE DESC, RANDOM() limit 30;

And I've tried 我试过了

SELECT * FROM table ORDER BY to_timestamp(to_char(article_date_added, \'DD Mon YYYY HH24\'), \'DD Mon YYYY\') DESC, RANDOM()

It gets the most recent articles but then it make it random by the day. 它会获得最新的文章,但随后会使其随机化。 So it will be like 所以会像

April 1
March 29
March 31
Apr 1

I'm trying to acheive results seperated by the date but then made random. 我正在尝试达到按日期分开的结果,然后将其随机化。

April 1
April 1
March 31
March29

Is there a way I can do this in SQL? 有没有办法可以在SQL中做到这一点?

Put the part where you get the 30 most recent articles into a subquery and select from the subquery ordering by random. 将获得30篇最新文章的部分放入子查询中,然后从子查询中随机选择。 I don't use postgresql so this example may have some errors: 我不使用postgresql,因此此示例可能会出现一些错误:

select * from 
(select somefields
from sometables
order by article_date_added desc
limit 30) temp
order by random()

So, you want every day grouped together but the dates presented in a random order, right? 因此,您希望每天分组在一起,但日期以随机顺序显示,对吗? We can use the built-in function generate_series which can take timestamp arguments to make a pseudo-table that can help. 我们可以使用内置函数generate_series ,该函数可以使用时间戳参数来创建可以提供帮助的伪表。

WITH t AS (
  SELECT gs::date AS dt, random() AS r 
  FROM generate_series('2010-01-01'::timestamp, '2015-01-01', '1 day') AS gs )
SELECT articles.* 
FROM articles JOIN t 
ON articles.article_date_added::date=t.dt 
ORDER BY r;

Note that it isn't necessary to have r in the output columns. 请注意,没有必要在输出列中包含r

Obviously this code will only work for another 2 years 9 months, but necessary modifications should be obvious. 显然,此代码只能再使用2年9个月,但是必须进行明显的修改。 You can even replace the hardwired dates with (SELECT min(article_date_added) FROM articles) [note extra parentheses!] and likewise for max . 您甚至可以将硬连线的日期替换为(SELECT min(article_date_added) FROM articles) [请注意额外的括号!],以及max替换方式。


[Edit after comments] In looking over your sample output, I see you don't want aggregation by date. [注释后编辑]在查看示例输出时,我看到您不希望按日期进行汇总。 I'm not sure the description matches the output. 我不确定说明是否与输出匹配。 What you are asking for is probably in one of the other suggestions, but you can do it easily with 您想要的可能是其他建议之一,但是您可以轻松地做到这一点

WITH t AS (
  SELECT * FROM articles
  ORDER BY article_created_date DESC LIMIT 30 )
SELECT * FROM t ORDER BY t.article_created_date desc, random();

I think the following will work: 我认为以下方法会起作用:

select t.*
from (select t.*,
             avg(random()) over (partition by article_date_added::DATE) as groupnum
      from t
     ) t
order by groupnum;

Or, if you want the dates descending and then random do: 或者,如果您希望日期递减然后随机执行:

select t.*
from t
order by article_date_added::DATE desc, random();

This is how your example data is structured. 这就是示例数据的结构。

select DATE
from TABLE 
order by date_trunc('month',DATE),
random()

This works by first truncating the date into the month and sorting it. 通过首先将日期截断为月份并对其进行排序,可以进行此操作。 So, 2012-01-25 and 2012-01-13 become 2012-01-01. 因此,2012-01-25和2012-01-13变为2012-01-01。

After that, sort the actual date randomly. 之后,随机排序实际日期。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM