如何ceil / floor日期时间的浮点值

时间:2014-12-25 03:17:07

标签: sql postgresql

最小案例

如果min(ticktime)::TIMESTAMP2014-01-02 01:14:45.5,我想将其转换为2014-01-02 01:14:45

最大案例

如果max(ticktime)::TIMESTAMP2014-01-02 01:14:45.5,我想将其转换为2014-01-02 01:14:46

    SELECT DISTINCT ON (1) generate_series ( min(ticktime)::TIMESTAMP, max(ticktime)::TIMESTAMP, '1 second'::interval) AS ticktime
    FROM cffexes

2 个答案:

答案 0 :(得分:0)

您可以将结果转换为第二个timestamp,如下所示:

SELECT DISTINCT ON (1) generate_series ( 
    min(ticktime)::TIMESTAMP(0), 
    max(ticktime)::TIMESTAMP(0)+1, 
    '1 second'::interval) AS ticktime
FROM cffexes;

如果您想要除秒之外的时间/时间,请使用date_trunc()

当然,这将留下一个如此轻微的漏洞,如果max(ticktime)完全是偶数秒,你将产生太多。所以你可以试试这个,但是要意识到浮点值对于平等来说是非常不友好的。

SELECT DISTINCT ON (1) generate_series ( 
    min(ticktime)::TIMESTAMP(0), 
    max(ticktime)::TIMESTAMP(0) + (max(ticktime)::TIMESTAMP(0)=max(ticktime)::TIMESTAMP)::int,
    '1 second'::interval) AS ticktime
FROM cffexes;

答案 1 :(得分:0)

我只是在generate_series的开头添加 date_trunc

它适用于我的

      SELECT DISTINCT ON (1) generate_series
      (
        date_trunc('second', min(ticktime)::TIMESTAMP),
        max(ticktime)::TIMESTAMP,
        '1 #{frequence}'::interval
      ) AS ticktime FROM #{market} WHERE product_type ='#{product_type}' AND contract_month = '#{contract_month}'::timestamp