2015-01-06 40 views
0

我需要從月的第一天到第15天(包括)和從第16天到月底(包括最後一天)提取和分組時間戳信息如何從postgresql中的時間戳中提取句點

我每週只做這個。下面是代碼

SELECT 
    extract(week from dttrnreqdate) AS period, 
    SUM(bonusesCharged - bonusesCharged_rev) AS bonusesChargedForPeriod, 
    SUM(bonusesWithdrawn - bonusesWithdrawn_rev) AS bonusesWithdrawnForPeriod 
FROM 
    ls.vstatement AS v 
WHERE 
    v.dttrnreqdate >= to_timestamp('2014-12-01 00:00:00', 'yyyy-mm-dd 00:00:00') AND 
    v.dttrnreqdate < to_timestamp('2015-01-07 00:00:00', 'yyyy-mm-dd 00:00:00') 
GROUP BY 
    extract(week from dttrnreqdate) 
ORDER BY 
    extract(week from dttrnreqdate) 
ASC 

其中 dttrnreqdatetimestamp bonusesCharged, bonusesCharged_rev, bonusesWithdrawn, bonusesWithdrawn_rev - int

那麼如何提取信息1-15和時間戳字段的16-30/31

回答

1

我敢確定這回答你的問題,但我不確定它會解決你的問題。

SELECT 
    case when extract(day from dttrnreqdate) <= 15 then 'grp1' 
     else 'grp2' 
    end as grp, 
    SUM(bonusesCharged - bonusesCharged_rev) AS bonusesChargedForPeriod, 
    SUM(bonusesWithdrawn - bonusesWithdrawn_rev) AS bonusesWithdrawnForPeriod 
FROM 
    ls.vstatement AS v 
WHERE 
    v.dttrnreqdate >= to_timestamp('2014-12-01 00:00:00', 'yyyy-mm-dd 00:00:00') AND 
    v.dttrnreqdate < to_timestamp('2015-01-07 00:00:00', 'yyyy-mm-dd 00:00:00') 
GROUP BY 
    grp 
ORDER BY 
    grp 
ASC; 
相關問題