比较 Postgres 中两个日期范围的计数
Comparing counts of two date ranges in Postgres
我正在尝试比较来自单个 PostgreSQL 9.6.5 table 的两个日期范围。这两个日期范围的长度相同,并且采用 本周 与 上周、本月的样式 对比 上个月 、 过去 180 天 对比 前 180 天 。 table 捕获客户交互,每行记录一次交互 - 日期、start_time、stop_time、位置等
我已经为单个日期范围构建了 SQL。
SELECT
to_char(date, 'day') as day,
extract(dow from date) as dow,
count(*) AS curr_count
FROM schema.table
WHERE (date between :date_start AND :date_stop)
GROUP BY day, dow
ORDER BY dow ASC
start_date 设置为“2018-08-08”,stop_date 设置为'2018-08-15',我得到这个记录集:
Array (
[0] => Array ( [day] => monday [0] => monday [dow] => 1 [1] => 1 [curr_count] => 78 [2] => 78 )
[1] => Array ( [day] => tuesday [0] => tuesday [dow] => 2 [1] => 2 [curr_count] => 75 [2] => 75 )
[2] => Array ( [day] => wednesday [0] => wednesday [dow] => 3 [1] => 3 [curr_count] => 62 [2] => 62 )
[3] => Array ( [day] => thursday [0] => thursday [dow] => 4 [1] => 4 [curr_count] => 68 [2] => 68 )
[4] => Array ( [day] => friday [0] => friday [dow] => 5 [1] => 5 [curr_count] => 81 [2] => 81 )
[5] => Array ( [day] => saturday [0] => saturday [dow] => 6 [1] => 6 [curr_count] => 3 [2] => 3 )
)
我可以很容易地计算上一期间(2018-08-01 到 2018-08-07)的日期,但我不明白如何将这些添加到 SQL 中以获得所需的日期结果。我想要的最终结果是:
array (
array (day, dow, curr_count, prev_count)
array (day, dow, curr_count, prev_count)
array (day, dow, curr_count, prev_count)
array (day, dow, curr_count, prev_count)
)
非常感谢任何帮助。
假设您有两组参数,定义了您要比较的两个时期:
with p1 as (
select date::date as date,
extract(dow from date) as dow,
count(*) AS curr_count,
row_number() over (order by date::date) as seqnum
from schema.table
where date between :date_start_1 and :date_stop_1
group by date::date
),
p2 as (
select date::date as date,
extract(dow from date) as dow,
count(*) AS curr_count,
row_number() over (order by date::date) as seqnum
from schema.table
where date between :date_start_2 and :date_stop_2
group by date::date
)
select p1.*, p2.curr_count as prev_count
from p1 join
p2
on p2.seqnum = p1.seqnum;
我将第一列更改为明确的日期。我怀疑您是否真的只想将月中的某一天和星期几作为结果集的关键字(当然,如果这是您真正想要的,您可以调整逻辑)。
我正在尝试比较来自单个 PostgreSQL 9.6.5 table 的两个日期范围。这两个日期范围的长度相同,并且采用 本周 与 上周、本月的样式 对比 上个月 、 过去 180 天 对比 前 180 天 。 table 捕获客户交互,每行记录一次交互 - 日期、start_time、stop_time、位置等
我已经为单个日期范围构建了 SQL。
SELECT
to_char(date, 'day') as day,
extract(dow from date) as dow,
count(*) AS curr_count
FROM schema.table
WHERE (date between :date_start AND :date_stop)
GROUP BY day, dow
ORDER BY dow ASC
start_date 设置为“2018-08-08”,stop_date 设置为'2018-08-15',我得到这个记录集:
Array (
[0] => Array ( [day] => monday [0] => monday [dow] => 1 [1] => 1 [curr_count] => 78 [2] => 78 )
[1] => Array ( [day] => tuesday [0] => tuesday [dow] => 2 [1] => 2 [curr_count] => 75 [2] => 75 )
[2] => Array ( [day] => wednesday [0] => wednesday [dow] => 3 [1] => 3 [curr_count] => 62 [2] => 62 )
[3] => Array ( [day] => thursday [0] => thursday [dow] => 4 [1] => 4 [curr_count] => 68 [2] => 68 )
[4] => Array ( [day] => friday [0] => friday [dow] => 5 [1] => 5 [curr_count] => 81 [2] => 81 )
[5] => Array ( [day] => saturday [0] => saturday [dow] => 6 [1] => 6 [curr_count] => 3 [2] => 3 )
)
我可以很容易地计算上一期间(2018-08-01 到 2018-08-07)的日期,但我不明白如何将这些添加到 SQL 中以获得所需的日期结果。我想要的最终结果是:
array (
array (day, dow, curr_count, prev_count)
array (day, dow, curr_count, prev_count)
array (day, dow, curr_count, prev_count)
array (day, dow, curr_count, prev_count)
)
非常感谢任何帮助。
假设您有两组参数,定义了您要比较的两个时期:
with p1 as (
select date::date as date,
extract(dow from date) as dow,
count(*) AS curr_count,
row_number() over (order by date::date) as seqnum
from schema.table
where date between :date_start_1 and :date_stop_1
group by date::date
),
p2 as (
select date::date as date,
extract(dow from date) as dow,
count(*) AS curr_count,
row_number() over (order by date::date) as seqnum
from schema.table
where date between :date_start_2 and :date_stop_2
group by date::date
)
select p1.*, p2.curr_count as prev_count
from p1 join
p2
on p2.seqnum = p1.seqnum;
我将第一列更改为明确的日期。我怀疑您是否真的只想将月中的某一天和星期几作为结果集的关键字(当然,如果这是您真正想要的,您可以调整逻辑)。