我有一个表,该表在多个时间戳上具有4种类型的ordes大约有5000条记录,我一直试图尝试计算多个时间戳(如00到04小时,04到24小时)的订单数量。我可以在1个时间戳中的任何一个上获得结果。 寻求帮助:)
SELECT
DECODE(order_type,1,'Multi Sim',2,'Single Sim',3,'Sim Replacement',7,'Data SIM with Router') AS order_type,
COUNT(order_type) AS total0hrs_4hrs,
COUNT(order_type) AS total4hrs_24hrs,
COUNT(order_type) AS grand_total
FROM
sids
WHERE
order_time >= to_timestamp('2020-09-01 00:00:00.0000','YYYY-MM-DD HH24:MI:SS.FF4')
AND order_time <= to_timestamp('2020-09-01 03:59:59.9999','YYYY-MM-DD HH24:MI:SS.FF4')
AND order_type IN (
1,
2,
3,
7
)
GROUP BY
order_type
HAVING
COUNT(order_type) > 0
ORDER BY
1;
答案 0 :(得分:0)
在聚合函数中使用CASE
表达式:
SELECT DECODE(
order_type,
1, 'Multi Sim',
2, 'Single Sim',
3, 'Sim Replacement',
7, 'Data SIM with Router'
) AS order_type,
COUNT(
CASE
WHEN order_time - TRUNC( order_time ) < INTERVAL '4' HOUR
THEN 1
ELSE NULL
END
) AS total0hrs_4hrs,
COUNT(
CASE
WHEN order_time - TRUNC( order_time ) < INTERVAL '4' HOUR
THEN NULL
ELSE 1
END
) AS total4hrs_24hrs,
COUNT(order_type) AS grand_total
FROM sids
WHERE order_time >= TIMESTAMP '2020-09-01 00:00:00.0000'
AND order_time < TIMESTAMP '2020-09-02 00:00:00.0000'
AND order_type IN ( 1, 2, 3, 7 )
GROUP BY
order_type
ORDER BY
1;