[英]How to calculate in PL/SQL Oracle?
I have 2 columns, each columns have 2 rows. 我有2列,每列有2行。 For example 例如
A B
100 200
200 300
So that I would like to calculate like this 所以我想像这样计算
Total A Total B
200-100 = 100 300-200=100
Show 节目
Total A Total B
100 100
Using MAX()
and MIN()
aggregate functions: 使用MAX()
和MIN()
聚合函数:
SELECT MAX(A) - MIN(A) AS "Total A",
MAX(B) - MIN(B) AS "Total B"
FROM TableName
Depends on what you exactly want to manage. 取决于您想要管理的内容。 If the problem is related to the difference between consecutive rows, using lead()
or lag()
window analytic functions might be a smart way : 如果问题与连续行之间的差异有关,则使用lead()
或lag()
窗口分析函数可能是一种聪明的方法:
with t( a, b ) as
(
select 100, 200 from dual union all
select 200, 300 from dual
), t2 as
(
select lead(a) over (order by 1) - a as "Total A",
lead(b) over (order by 1) - b as "Total B",
row_number() over (order by 1) as rn
from t
)
select *
from t2
where rn = 1;
Total A Total B RN
100 100 1
You can query it like this, 你可以这样查询,
WITH your_table AS
(SELECT 100 a, 200 b
FROM DUAL
UNION ALL
SELECT 200 a, 300 b
FROM DUAL
ORDER BY 1 DESC)
SELECT SUM(DECODE(ROWNUM, 1, a, -1*a)) TOTAL_A, SUM(DECODE(ROWNUM, 1, b, -1*b)) TOTAL_B
FROM your_table;
You can try this as below 您可以尝试如下
Select * , `entity1`-`entity2` As `entity3`
FROM tbl
Hope this helps. 希望这可以帮助。
Bro their is two understanding in your question first you want to delete from max value and which solution provided to you by someone else and other is you want to delete from last row/second row. 在他们的问题中,他们首先想要从最大值中删除以及由其他人提供给您的解决方案,并且您想要从最后一行/第二行删除。 So the solution is provided to you for second approach. 因此,第二种方法为您提供了解决方案。 please discuss if any query. 请讨论是否有任何疑问。
select sum(Result.A) as A, sum(Result.B) as B, from (
select top 1 A , B from Table_name order by id_row_order desc
union all
select top 1 - A , -B from Table_name order by id_row_order Asc
) as Result
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.