WITH cudt AS (
--查询第一天的数据 数据存放到 cudt
SELECT
id,
name,
TOTAL,
TIME
FROM
table_name
WHERE
time = TO_CHAR (SYSDATE - 1 , 'YYYYMMDD')
),
--查询第二天的数据 数据存放到 cudt1
cudt1 AS (
SELECT
id,
name,
TOTAL,
TIME
FROM
table_name
WHERE
time = TO_CHAR (SYSDATE - 2, 'YYYYMMDD')
)
--计算两天数据的差
SELECT
cudt.id AS ID,
cudt.name AS NAME,
'1' AS TYPE,
-- NVL(cudt1.total, 0) 数据为如果为空 赋值0
---判断如果小于零赋值为0
case when (TO_NUMBER(cudt.total - NVL (cudt1.total, 0)) < '0') then '0' else TO_CHAR(cudt.total - NVL (cudt1.total, 0)) end,
case when (TO_NUMBER(NVL(cudt1.total, 0) - cudt.total) < '0') then '0' else TO_CHAR(NVL(cudt1.total, 0) - cudt.total) end,
cudt.time,
TO_CHAR (SYSDATE, 'YYYYMMDDHH24MISS')
FROM
cudt
LEFT JOIN cudt1 ON cudt.id = cudt1.id
查询一个表的数据两天差
最新推荐文章于 2024-02-27 21:23:56 发布