使用“开始日期”和“结束日期”每天在特定时间进行活动交易

时间:2017-12-04 11:38:55

标签: postgresql

我正在尝试计算特定时间每月第一天的活动交易数量。我可以计算出我想要的特定日期。例如,我可以运行:

SELECT 
A.country_code,
A.transaction_type,
COUNT (*)

FROM table A
JOIN table_history h ON h.listing_id = A.listing_id

WHERE h.start_date <= '2017-12-01 08:01:03' and NVL(h.end_date, '31-DEC-2099') >= '2017-12-01 08:01:03'

GROUP BY 
A.country_code,
A.transaction_type

该代码适用于12月1日。但是,我希望扩展它以在该特定时间(08:01:03)获取该月的每个月的所有活动事务。

谢谢

2 个答案:

答案 0 :(得分:1)

通过引入Dim Date表并创建复杂的连接来管理它以进行排序:

SELECT 
A.country_code,
A.transaction_type,
COUNT (*)

FROM table A
JOIN table_history h ON h.listing_id = A.listing_id

JOIN dim_date dd  on h.start_date <= DATEADD(m,1,DATEADD(hour,8,dd.date)) and NVL(h.end_date, '31-DEC-2099') >= DATEADD(m,1,DATEADD(hour,8,dd.date))

WHERE dd.month_day_no = 1
--WHERE h.start_date <= '2017-12-01 08:01:03' and NVL(h.end_date, '31-DEC-2099') >= '2017-12-01 08:01:03'

GROUP BY 
A.country_code,
A.transaction_type

答案 1 :(得分:0)

您可以投放时间戳作为时间来获取时间并使用where cast (start_date as time) = '08:01:03' and extract (day from start_date) = 1 来获取当月的日期:

{{1}}
相关问题