Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/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
Postgresql 创建日期序列并将每个日期插入查询_Postgresql_Date - Fatal编程技术网

Postgresql 创建日期序列并将每个日期插入查询

Postgresql 创建日期序列并将每个日期插入查询,postgresql,date,Postgresql,Date,我需要在本月的第一天到本月的最后一天查找某些数据 select count(*) from q_aggr_data as a where a.filial_='fil1' and a.operator_ like 'unit%' and date_trunc('day',a.s_end_)='"+ date_to_search+ "' group by a.s_name_,date_trunc('day',a.s_end_) 此处

我需要在本月的第一天到本月的最后一天查找某些数据

 select count(*) from q_aggr_data as a 
       where a.filial_='fil1'
       and a.operator_ like 'unit%' 
       and date_trunc('day',a.s_end_)='"+ date_to_search+ "' 
       group by a.s_name_,date_trunc('day',a.s_end_)
此处的日期为2014年9月1日、2014年9月2日、2014年9月3日、2014年9月30日


我试着在I=0…30之间循环并进行30次查询,但这需要太长时间,而且非常幼稚。对于没有条目的日期,也应返回0。我已经看到了如何生成日期序列,但是我还不知道如何将这些日子一个接一个地注入到查询中

解决这个问题的一种方法是按截断的日期分组

select count(*)
from q_aggr_data as a 
where a.filial_='fil1'
and a.operator_ like 'unit%' 
group by date_trunc('day',a.s_end_), a.s_name_;

另一种方法是使用,例如获取截断日期的计数。

请检查此查询是否满足您的要求:

select sum(matched) -- include s_name_, s_end_ if you want to verify the results
from
(select a.filial_
, a.operator_
, a.s_name_
, generate_series s_end_
, (case when a.filial_ = 'fil1' then 1 else 0 end) as matched
from q_aggr_data as a 
right join generate_series('2014-09-01', '2014-09-30', interval '1 day')
     on a.s_end_ = generate_series
     and a.filial_ = 'fil1'
     and a.operator_ like 'unit%') aa
group by s_name_, s_end_
order by s_end_, s_name_

通过不仅创建一个系列,而且创建一组1天范围,任何时间戳数据都可以使用>=with<

特别要注意的是,这种方法避免了对数据执行函数(例如截断到目前为止的数据),因此它允许使用索引来辅助查询性能

如果某些数据如下所示:

CREATE TABLE my_data
    ("data_dt" timestamp)
;

INSERT INTO my_data
    ("data_dt")
VALUES
    ('2014-09-01 08:24:00'),
    ('2014-09-01 22:48:00'),
    ('2014-09-02 13:12:00'),
    ('2014-09-03 03:36:00'),
    ('2014-09-03 18:00:00'),
然后,可以使用
外部连接将其连接起来
,以便将不匹配的范围报告给生成的范围集(dt_开始和dt_结束对)


有什么原因不能使用convert/cast+BETWEEN吗?它会给出日期在几天之内的任何东西的计数。虽然我需要日期计数行,但在日期中,每个月的每一天都是一列
SELECT
        r.dt_start
      , count(d.data_dt)
FROM (
      SELECT
            dt_start
          , dt_start + INTERVAL '1 Day' dt_end 
      FROM
           generate_series('2014-09-01 00:00'::timestamp,
                           '2014-09-30 00:00', '1 Day') AS dt_start
     ) AS r
LEFT OUTER JOIN my_data d ON d.data_dt >= r.dt_start
                         AND d.data_dt <  r.dt_end
GROUP BY
        r.dt_start
ORDER BY
        r.dt_start
;
|                         DT_START | COUNT |
|----------------------------------|-------|
| September, 01 2014 00:00:00+0000 |     2 |
| September, 02 2014 00:00:00+0000 |     1 |
| September, 03 2014 00:00:00+0000 |     2 |
| September, 04 2014 00:00:00+0000 |     2 |
 ...
| September, 29 2014 00:00:00+0000 |     0 |
| September, 30 2014 00:00:00+0000 |     0 |