Oracle - 值更改的天数
Oracle - Count of number of days a value has changed
我需要在 Oracle 中编写查询,如下面的屏幕截图所示。任何帮助是极大的赞赏。非常感谢。瓦迪
Table 样本数据:
CREATE TABLE fee_check (
trans_date DATE,
fee1 NUMBER(6,3),
fee2 NUMBER(6,3)
);
INSERT INTO fee_check(trans_date, fee1, fee2) VALUES (to_date('18/04/2022','dd/mm/yyyy'), 0.74, 0.87);
INSERT INTO fee_check(trans_date, fee1, fee2) VALUES (to_date('19/04/2022','dd/mm/yyyy'), 0.75, 0.87);
INSERT INTO fee_check(trans_date, fee1, fee2) VALUES (to_date('20/04/2022','dd/mm/yyyy'), 0.75, 0.87);
INSERT INTO fee_check(trans_date, fee1, fee2) VALUES (to_date('21/04/2022','dd/mm/yyyy'), 0.73, 0.87);
INSERT INTO fee_check(trans_date, fee1, fee2) VALUES (to_date('22/04/2022','dd/mm/yyyy'), 0.73, 0.87);
INSERT INTO fee_check(trans_date, fee1, fee2) VALUES (to_date('23/04/2022','dd/mm/yyyy'), 0.73, 0.87);
INSERT INTO fee_check(trans_date, fee1, fee2) VALUES (to_date('24/04/2022','dd/mm/yyyy'), 0.73, 0.87);
INSERT INTO fee_check(trans_date, fee1, fee2) VALUES (to_date('25/04/2022','dd/mm/yyyy'), 0.76, 0.87);
COMMIT;
这就是您的问题的解决方案
SELECT MIN(trans_date) trans_date, COUNT(*) DayCount, fee1, fee2
FROM fee_check
GROUP BY fee1,fee2
ORDER BY trans_date
如果您只想计算连续的行(而不是将具有相同费用值的 non-consecutive 组聚合在一起),那么在 Oracle 12 中,您可以使用 MATCH_RECOGNIZE
来执行 row-by-row 处理:
SELECT *
FROM fee_check
MATCH_RECOGNIZE(
ORDER BY trans_date
MEASURES
FIRST(trans_date) AS trans_date,
COUNT(trans_date) AS day_count,
FIRST(fee1) AS fee1,
FIRST(fee2) AS fee2
PATTERN (same_fees+)
DEFINE same_fees AS fee1 = FIRST(fee1) AND fee2 = FIRST(fee2)
)
或者,在早期版本中,您可以使用解析函数:
SELECT MIN(trans_date) AS trans_date,
COUNT(*) AS day_count,
MIN(fee1) AS fee1,
MIN(fee2) AS fee2
FROM (
SELECT f.*,
ROW_NUMBER() OVER (ORDER BY trans_date) -
ROW_NUMBER() OVER (PARTITION BY fee1, fee2 ORDER BY trans_date) AS grp
FROM fee_check f
)
GROUP BY grp
其中,对于示例数据:
CREATE TABLE fee_check (trans_date, fee1, fee2) AS
SELECT DATE '2022-04-18', 0.74, 0.87 FROM DUAL UNION ALL
SELECT DATE '2022-04-19', 0.75, 0.87 FROM DUAL UNION ALL
SELECT DATE '2022-04-20', 0.75, 0.87 FROM DUAL UNION ALL
SELECT DATE '2022-04-21', 0.73, 0.87 FROM DUAL UNION ALL
SELECT DATE '2022-04-22', 0.73, 0.87 FROM DUAL UNION ALL
SELECT DATE '2022-04-23', 0.73, 0.87 FROM DUAL UNION ALL
SELECT DATE '2022-04-24', 0.73, 0.87 FROM DUAL UNION ALL
SELECT DATE '2022-04-25', 0.76, 0.87 FROM DUAL UNION ALL
SELECT DATE '2022-04-26', 0.75, 0.87 FROM DUAL;
注意:在末尾添加了一个额外的行,该行在数据集中前面具有相同的 fee1
和 fee2
值。
双输出:
TRANS_DATE
DAY_COUNT
FEE1
FEE2
2022-04-18 00:00:00
1
.74
.87
2022-04-19 00:00:00
2
.75
.87
2022-04-21 00:00:00
4
.73
.87
2022-04-25 00:00:00
1
.76
.87
2022-04-26 00:00:00
1
.75
.87
db<>fiddle here
我需要在 Oracle 中编写查询,如下面的屏幕截图所示。任何帮助是极大的赞赏。非常感谢。瓦迪
Table 样本数据:
CREATE TABLE fee_check (
trans_date DATE,
fee1 NUMBER(6,3),
fee2 NUMBER(6,3)
);
INSERT INTO fee_check(trans_date, fee1, fee2) VALUES (to_date('18/04/2022','dd/mm/yyyy'), 0.74, 0.87);
INSERT INTO fee_check(trans_date, fee1, fee2) VALUES (to_date('19/04/2022','dd/mm/yyyy'), 0.75, 0.87);
INSERT INTO fee_check(trans_date, fee1, fee2) VALUES (to_date('20/04/2022','dd/mm/yyyy'), 0.75, 0.87);
INSERT INTO fee_check(trans_date, fee1, fee2) VALUES (to_date('21/04/2022','dd/mm/yyyy'), 0.73, 0.87);
INSERT INTO fee_check(trans_date, fee1, fee2) VALUES (to_date('22/04/2022','dd/mm/yyyy'), 0.73, 0.87);
INSERT INTO fee_check(trans_date, fee1, fee2) VALUES (to_date('23/04/2022','dd/mm/yyyy'), 0.73, 0.87);
INSERT INTO fee_check(trans_date, fee1, fee2) VALUES (to_date('24/04/2022','dd/mm/yyyy'), 0.73, 0.87);
INSERT INTO fee_check(trans_date, fee1, fee2) VALUES (to_date('25/04/2022','dd/mm/yyyy'), 0.76, 0.87);
COMMIT;
这就是您的问题的解决方案
SELECT MIN(trans_date) trans_date, COUNT(*) DayCount, fee1, fee2
FROM fee_check
GROUP BY fee1,fee2
ORDER BY trans_date
如果您只想计算连续的行(而不是将具有相同费用值的 non-consecutive 组聚合在一起),那么在 Oracle 12 中,您可以使用 MATCH_RECOGNIZE
来执行 row-by-row 处理:
SELECT *
FROM fee_check
MATCH_RECOGNIZE(
ORDER BY trans_date
MEASURES
FIRST(trans_date) AS trans_date,
COUNT(trans_date) AS day_count,
FIRST(fee1) AS fee1,
FIRST(fee2) AS fee2
PATTERN (same_fees+)
DEFINE same_fees AS fee1 = FIRST(fee1) AND fee2 = FIRST(fee2)
)
或者,在早期版本中,您可以使用解析函数:
SELECT MIN(trans_date) AS trans_date,
COUNT(*) AS day_count,
MIN(fee1) AS fee1,
MIN(fee2) AS fee2
FROM (
SELECT f.*,
ROW_NUMBER() OVER (ORDER BY trans_date) -
ROW_NUMBER() OVER (PARTITION BY fee1, fee2 ORDER BY trans_date) AS grp
FROM fee_check f
)
GROUP BY grp
其中,对于示例数据:
CREATE TABLE fee_check (trans_date, fee1, fee2) AS
SELECT DATE '2022-04-18', 0.74, 0.87 FROM DUAL UNION ALL
SELECT DATE '2022-04-19', 0.75, 0.87 FROM DUAL UNION ALL
SELECT DATE '2022-04-20', 0.75, 0.87 FROM DUAL UNION ALL
SELECT DATE '2022-04-21', 0.73, 0.87 FROM DUAL UNION ALL
SELECT DATE '2022-04-22', 0.73, 0.87 FROM DUAL UNION ALL
SELECT DATE '2022-04-23', 0.73, 0.87 FROM DUAL UNION ALL
SELECT DATE '2022-04-24', 0.73, 0.87 FROM DUAL UNION ALL
SELECT DATE '2022-04-25', 0.76, 0.87 FROM DUAL UNION ALL
SELECT DATE '2022-04-26', 0.75, 0.87 FROM DUAL;
注意:在末尾添加了一个额外的行,该行在数据集中前面具有相同的 fee1
和 fee2
值。
双输出:
TRANS_DATE DAY_COUNT FEE1 FEE2 2022-04-18 00:00:00 1 .74 .87 2022-04-19 00:00:00 2 .75 .87 2022-04-21 00:00:00 4 .73 .87 2022-04-25 00:00:00 1 .76 .87 2022-04-26 00:00:00 1 .75 .87
db<>fiddle here