Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.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

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_Datetime_Grouping - Fatal编程技术网

对PostgreSQL中两个日期的记录进行分组

对PostgreSQL中两个日期的记录进行分组,postgresql,date,datetime,grouping,Postgresql,Date,Datetime,Grouping,鉴于下表结构,我有以下问题: Table "public.search" Column | Type | Modifiers ------------------+-----------------------------+----------------------

鉴于下表结构,我有以下问题:

                                    Table "public.search"

 Column           |            Type             |                       Modifiers                       
------------------+-----------------------------+-------------------------------------------------------
 id               | bigint                      | not null default nextval('search_id_seq'::regclass)
 date             | timestamp without time zone | 
 datesolicitation | timestamp without time zone | 
 reason           | character varying(255)      | 
 notice           | real                        | not null
 situation        | character(1)                | not null
 urgent           | boolean                     | not null
 expiring         | timestamp without time zone | 
 bond             | character(1)                | not null
 company_id       | bigint                      | 
 vehicle_id       | bigint                      | 
 driver_id        | bigint                      | 
 locality_id      | bigint                      | 
 set_id           | bigint                      | 
 composition_id   | bigint                      | 
 responsible_id   | bigint                      | 
 fleet            | character varying(255)      | 
每天我都会运行一个查询,根据表过期列返回未来五天内要过期的所有记录:

SELECT
  COUNT(id) AS total
FROM search 
WHERE
  bond IN ('F', 'A', 'G', 'J')
  AND expiring - interval '5 day' between '2017-09-25 12:00:00' and '2017-09-26 12:00:00';

 total 
-------
   504
(1 row)
当有问题的查询在另一天执行时,或者当我们有多个返回的“基准”日期时,就会出现问题

SELECT
  COUNT(DISTINCT id) AS total
  TO_CHAR(expiring, 'DD/MM/YYYY')
FROM search 
WHERE
  vinculo IN ('F', 'A', 'G', 'J')
  AND expiring - interval '5 day' between '2017-09-25 12:00:00' AND '2017-09-28 12:00:00'
GROUP BY expiring;

 total |  to_char   
-------+------------
     1 | 30/09/2017
     1 | 30/09/2017
     1 | 30/09/2017
     1 | 30/09/2017
     1 | 30/09/2017
     1 | 30/09/2017
由于咨询期为每天12:00:00至另一天12:00:00或9月25日12:00:00,因此查询中显示的咨询期也将被视为9月26日至12:00:00的所有记录。 9月26日也将连续被视为9月27日的记录。 有人有什么想法吗


谢谢大家!

那你怎么办呢

WHERE expiring BETWEEN CURRENT_TIMESTAMP 
                   AND CURRENT_TIMESTAMP + INTERVAL '5 days'
GROUP BY TO_CHAR(expiring, 'DD/MM/YYYY');

类似于
按日期分组\u trunc('day',expiring-'11:59:59'::time)