如何计算Oracle中行的月数
How to count number of months of row in Oracle
我正在使用 ORACLE 来检查 'DATE_PERIOS' 中的条件以及当前时间和上次时间
我的 ORD0011 table:
-------------------------------------------------------------
ORDER_ID | BS_NO | DATE_PERIOS | STATUS
-------------------------------------------------------------
3000003 HS00001 4-2021 COMPLETE
3000003 HS00183 5-2021 COMPLETE
3000003 HS00776 10-2021 FALSE
3000003 HS00559 11-2021 COMPLETE
3000003 HS00221 12-2021 ACTIVE
3000003 HS00222 1-2022 COMPLETE
--------------------------------------------------------------
当i select 'ORDER_ID' = 3000003时,输出数据如下:
------------------------------
ORDER_ID | HS_TIME
------------------------------
3000003 4
------------------------------
这是我的食谱:
如果i select DATE_PERIOS is: 1/2022, 则显示HS_TIME is 4 (3 times COMPLETE before: 4-2021, 5-2021, 11-2021 + 1)
如果i select DATE_PERIOS is: 11/2021, 则显示HS_TIME is 3 (2 times COMPLETE before: 4-2021, 5-2021 + 1)
注意:仅当状态为 COMPLETE 时 +1
如何统计有条件输出结果如上的月数?非常感谢
with cte as(
select *,row_number()over(partition by ORDER_ID order by ORDER_ID) as seq
from tb
),
cte2 as(
select *,
case
when DATE_PERIOS = '1-2022'
then (select count(*) + 1 from cte t2 where t1.seq > t2.seq and t2.STATUS = 'COMPLETE')
else 0 end as HS_TIME_1_2022,
case
when DATE_PERIOS = '11-2021'
then (select count(*) + 1 from cte t2 where t1.seq > t2.seq and t2.STATUS = 'COMPLETE')
else 0 end as HS_TIME_11_2022
from cte t1)
select ORDER_ID,max(HS_TIME_1_2022)HS_TIME_1_2022,max(HS_TIME_11_2022)HS_TIME_11_2022
from cte2
group by ORDER_ID
下面的输出是在sql-server中得到的,但在Oracle中也可以是运行。
您可以使用以下带有 case 语句的查询来处理状态 = 'COMPLETE' -
SELECT CASE WHEN COUNT(CASE WHEN status = 'COMPLETE' THEN 1 ELSE NULL END) > 0 THEN
COUNT(CASE WHEN status = 'COMPLETE' THEN 1 ELSE NULL END) + 1
ELSE NULL
END HS_TIME
FROM tb
WHERE TO_DATE(DATE_PERIOS, 'MM-YYYY') < TO_DATE('01-2020', 'MM-YYYY');
一个选项是有条件地(即CASE
表达式)添加1
(即SUM
函数)如果status
已完成。
WHERE
子句要求 TO_DATE
具有适当的日期格式。否则,您将比较会导致错误结果的字符串;如果 date_perios
以 YYYYMM
格式存储, 可能 可以;另一方面,也许您想考虑将日期值存储到 DATE
数据类型列中。
示例数据
SQL> with ord0011 (order_id, date_perios, status) as
2 (select 303, '4-2021' , 'COMPLETE' from dual union all
3 select 303, '5-2021' , 'COMPLETE' from dual union all
4 select 303, '10-2021', 'FALSE' from dual union all
5 select 303, '11-2021', 'COMPLETE' from dual union all
6 select 303, '12-2021', 'ACTIVE' from dual union all
7 select 303, '1-2022' , 'COMPLETE' from dual
8 )
查询本身
9 select order_id,
10 sum(case when status = 'COMPLETE' then 1 else 0 end) hs_time
11 from ord0011
12 where to_date(date_perios, 'mm-yyyy') <= to_date('&par_date', 'mm-yyyy')
13 group by order_id;
Enter value for par_date: 1-2022
ORDER_ID HS_TIME
---------- ----------
303 4
SQL> /
Enter value for par_date: 11-2021
ORDER_ID HS_TIME
---------- ----------
303 3
SQL>
我正在使用 ORACLE 来检查 'DATE_PERIOS' 中的条件以及当前时间和上次时间
我的 ORD0011 table:
-------------------------------------------------------------
ORDER_ID | BS_NO | DATE_PERIOS | STATUS
-------------------------------------------------------------
3000003 HS00001 4-2021 COMPLETE
3000003 HS00183 5-2021 COMPLETE
3000003 HS00776 10-2021 FALSE
3000003 HS00559 11-2021 COMPLETE
3000003 HS00221 12-2021 ACTIVE
3000003 HS00222 1-2022 COMPLETE
--------------------------------------------------------------
当i select 'ORDER_ID' = 3000003时,输出数据如下:
------------------------------
ORDER_ID | HS_TIME
------------------------------
3000003 4
------------------------------
这是我的食谱:
如果i select DATE_PERIOS is: 1/2022, 则显示HS_TIME is 4 (3 times COMPLETE before: 4-2021, 5-2021, 11-2021 + 1)
如果i select DATE_PERIOS is: 11/2021, 则显示HS_TIME is 3 (2 times COMPLETE before: 4-2021, 5-2021 + 1)
注意:仅当状态为 COMPLETE 时 +1
如何统计有条件输出结果如上的月数?非常感谢
with cte as(
select *,row_number()over(partition by ORDER_ID order by ORDER_ID) as seq
from tb
),
cte2 as(
select *,
case
when DATE_PERIOS = '1-2022'
then (select count(*) + 1 from cte t2 where t1.seq > t2.seq and t2.STATUS = 'COMPLETE')
else 0 end as HS_TIME_1_2022,
case
when DATE_PERIOS = '11-2021'
then (select count(*) + 1 from cte t2 where t1.seq > t2.seq and t2.STATUS = 'COMPLETE')
else 0 end as HS_TIME_11_2022
from cte t1)
select ORDER_ID,max(HS_TIME_1_2022)HS_TIME_1_2022,max(HS_TIME_11_2022)HS_TIME_11_2022
from cte2
group by ORDER_ID
下面的输出是在sql-server中得到的,但在Oracle中也可以是运行。
您可以使用以下带有 case 语句的查询来处理状态 = 'COMPLETE' -
SELECT CASE WHEN COUNT(CASE WHEN status = 'COMPLETE' THEN 1 ELSE NULL END) > 0 THEN
COUNT(CASE WHEN status = 'COMPLETE' THEN 1 ELSE NULL END) + 1
ELSE NULL
END HS_TIME
FROM tb
WHERE TO_DATE(DATE_PERIOS, 'MM-YYYY') < TO_DATE('01-2020', 'MM-YYYY');
一个选项是有条件地(即CASE
表达式)添加1
(即SUM
函数)如果status
已完成。
WHERE
子句要求 TO_DATE
具有适当的日期格式。否则,您将比较会导致错误结果的字符串;如果 date_perios
以 YYYYMM
格式存储, 可能 可以;另一方面,也许您想考虑将日期值存储到 DATE
数据类型列中。
示例数据
SQL> with ord0011 (order_id, date_perios, status) as
2 (select 303, '4-2021' , 'COMPLETE' from dual union all
3 select 303, '5-2021' , 'COMPLETE' from dual union all
4 select 303, '10-2021', 'FALSE' from dual union all
5 select 303, '11-2021', 'COMPLETE' from dual union all
6 select 303, '12-2021', 'ACTIVE' from dual union all
7 select 303, '1-2022' , 'COMPLETE' from dual
8 )
查询本身
9 select order_id,
10 sum(case when status = 'COMPLETE' then 1 else 0 end) hs_time
11 from ord0011
12 where to_date(date_perios, 'mm-yyyy') <= to_date('&par_date', 'mm-yyyy')
13 group by order_id;
Enter value for par_date: 1-2022
ORDER_ID HS_TIME
---------- ----------
303 4
SQL> /
Enter value for par_date: 11-2021
ORDER_ID HS_TIME
---------- ----------
303 3
SQL>