简体   繁体   English

如何计算 PostgreSQL 中每一天的每个条目

[英]How to count every entries for each day in PostgreSQL

I currently have a query that formats bigint values into dates like this "01-01-1970".我目前有一个查询,将 bigint 值格式化为类似“01-01-1970”的日期。

My results look like this:我的结果是这样的:

date    
28-01-2021
14-07-2021
15-08-2021
14-07-2021
....

I have duplicate dates in my result, and what I need is to get something like this (and ordered by date descending):我的结果中有重复的日期,我需要的是得到这样的东西(并按日期降序排列):

   count    date    
    1        28-01-2021
    2        14-07-2021
    1        15-08-2021
    ....

I tried this:我试过这个:

  SELECT count(to_char(to_timestamp(last_downloaded/1000),
  'DD-MM-YYYY')), to_char(to_timestamp(last_downloaded/1000) from stats;

But it does not give me what I want..但它没有给我我想要的..

Just use group by .只需使用group by However as @jarlh noted you have to first convert the strings to date or - better - store data as the native type and not as formatted text..但是,正如@jarlh 指出的那样,您必须首先将字符串转换为date或 - 更好 - 将数据存储为本机类型而不是格式化文本。

with t(d) as
(
 values
 ('28-01-2021'),
 ('14-07-2021'),
 ('15-08-2021'),
 ('14-07-2021')
)
select to_date(d, 'dd-mm-yyyy'), count(1)
from t
group by 1
order by 1 desc;

In case your bigint values are epoch timestamps in milliseconds then如果您的 bigint 值是以毫秒为单位的纪元时间戳,那么

with t(dts) as
(
 values
 (1632837719969),
 (1632837719969),
 (1632700800123),
 (1632614400234),
 (1630330448696)
)
select to_timestamp(dts/1000)::date, count(1)
from t
group by 1 order by 1 desc;

Result:结果:

to_timestamp到_时间戳 count数数
2021-09-28 2021-09-28 2 2个
2021-09-27 2021-09-27 1 1个
2021-09-26 2021-09-26 1 1个
2021-08-30 2021-08-30 1 1个

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

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