2017-12-04 4 views
0

特定の時刻に毎月1日にアクティブな取引の数を計算しようとしています。私はそれが私が望む時間の特定の日のために計算することができます。たとえば、私は実行することができます:「開始日」と「終了日」を使用して、毎日特定の時間に実行されているアクティブな取引

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)にすべてのアクティブなトランザクションを取得するようにそれを拡張したいと考えています。

はありがとう

あなただけの時間を取得し、月の日付を取得するために extractを使用するための時間として、タイムスタンプをキャストすることができ
+0

'nvl'?postgresは' coalesce'を使用して自分で関数を作成しましたか? –

答えて

0

where 
    cast (start_date as time) = '08:01:03' and 
    extract (day from start_date) = 1 
+0

ありがとうございました。しかし、私はRedshiftテーブルを照会しているので、 'cast(start_date as time)'を使用することはできません。 また、特定の時間にアクティブなトランザクションを取得するには、終了日以外に開始日が必要です –

0

を持って来ることによってそれを整理して管理複雑な結合を作成する:

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 
関連する問題