在 DB2 中用午夜秒舍入
Rounding with midnight seconds in DB2
也许这比我意识到的更简单,但我只是想简单地查看如下记录,然后将时间戳相加,但对于差异仅以秒为单位的分组,我的结果集是 0,但是我也想以小数形式计算秒数。
我在 AS400 iSeries 版本 7 上使用 DB2
示例数据
JOB_STATUS | JOB_STATUS_TIME
---------------------------------------------
P 2019-10-02 08:47:12.362261
P 2019-10-02 08:47:22.362261
P 2019-10-02 08:47:32.362261
P 2019-10-02 08:47:42.362261
P 2019-10-02 08:47:52.362261
查询:
SELECT
SUM
(
CASE A1.JOB_STATUS WHEN 'P' THEN
(DAYS(A2.JOB_STATUS_TIME) - DAYS(A1.JOB_STATUS_TIME)) * 86400
+ MIDNIGHT_SECONDS(A2.JOB_STATUS_TIME) -
MIDNIGHT_SECONDS(A2.JOB_STATUS_TIME)
END
) / 60 AS ACTIVE_MINUTES
FROM SCHEMA.TABLE;
我尝试添加 ROUND({sum query}, 2)
但这对我不起作用,仍然返回零
如果除法运算中使用的所有列和值都是 INGEGER
(或 BIGINT
或 SMALLINT
),则除法将是整数除法。
您需要将某些内容转换为 DECIMAL 或使用 DECFLOAT 值的小数浮点数来获得小数除法。例如。尝试除以 60.0
SELECT
SUM(CASE A1.JOB_STATUS WHEN 'P' THEN
(DAYS(A2.JOB_STATUS_TIME) - DAYS(A1.JOB_STATUS_TIME)) * 86400
+ MIDNIGHT_SECONDS(A2.JOB_STATUS_TIME)
- MIDNIGHT_SECONDS(A1.JOB_STATUS_TIME)
END
) / 60.0 AS ACTIVE_MINUTES
FROM
SCHEMA.TABLE
https://www.ibm.com/support/knowledgecenter/en/ssw_ibm_i_72/db2/rbafz2intoper.htm
If both operands of an arithmetic operator are integers with zero scale, the operation is performed in binary, and the result is a large integer unless either (or both) operand is a big integer, in which case the result is a big integer. Any remainder of division is lost
有点跑题了,不过我觉得你做的可以用
TIMESTAMPDIFF(2, MAX(JOB_STATUS_TIME) - MIN(JOB_STATUS_TIME) )
也许这比我意识到的更简单,但我只是想简单地查看如下记录,然后将时间戳相加,但对于差异仅以秒为单位的分组,我的结果集是 0,但是我也想以小数形式计算秒数。
我在 AS400 iSeries 版本 7 上使用 DB2
示例数据
JOB_STATUS | JOB_STATUS_TIME
---------------------------------------------
P 2019-10-02 08:47:12.362261
P 2019-10-02 08:47:22.362261
P 2019-10-02 08:47:32.362261
P 2019-10-02 08:47:42.362261
P 2019-10-02 08:47:52.362261
查询:
SELECT
SUM
(
CASE A1.JOB_STATUS WHEN 'P' THEN
(DAYS(A2.JOB_STATUS_TIME) - DAYS(A1.JOB_STATUS_TIME)) * 86400
+ MIDNIGHT_SECONDS(A2.JOB_STATUS_TIME) -
MIDNIGHT_SECONDS(A2.JOB_STATUS_TIME)
END
) / 60 AS ACTIVE_MINUTES
FROM SCHEMA.TABLE;
我尝试添加 ROUND({sum query}, 2)
但这对我不起作用,仍然返回零
如果除法运算中使用的所有列和值都是 INGEGER
(或 BIGINT
或 SMALLINT
),则除法将是整数除法。
您需要将某些内容转换为 DECIMAL 或使用 DECFLOAT 值的小数浮点数来获得小数除法。例如。尝试除以 60.0
SELECT
SUM(CASE A1.JOB_STATUS WHEN 'P' THEN
(DAYS(A2.JOB_STATUS_TIME) - DAYS(A1.JOB_STATUS_TIME)) * 86400
+ MIDNIGHT_SECONDS(A2.JOB_STATUS_TIME)
- MIDNIGHT_SECONDS(A1.JOB_STATUS_TIME)
END
) / 60.0 AS ACTIVE_MINUTES
FROM
SCHEMA.TABLE
https://www.ibm.com/support/knowledgecenter/en/ssw_ibm_i_72/db2/rbafz2intoper.htm
If both operands of an arithmetic operator are integers with zero scale, the operation is performed in binary, and the result is a large integer unless either (or both) operand is a big integer, in which case the result is a big integer. Any remainder of division is lost
有点跑题了,不过我觉得你做的可以用
TIMESTAMPDIFF(2, MAX(JOB_STATUS_TIME) - MIN(JOB_STATUS_TIME) )