如何在PL / SQL Oracle中计算?

时间:2019-06-13 04:06:08

标签: sql oracle

我有2列,每列有2行。例如

A     B
100   200
200   300

所以我想这样计算

Total A          Total B

200-100 = 100   300-200=100

显示

Total A     Total B
100         100

5 个答案:

答案 0 :(得分:1)

使用MAX()MIN()聚合函数:

SELECT MAX(A) - MIN(A) AS "Total A",
       MAX(B) - MIN(B) AS "Total B"
FROM TableName

答案 1 :(得分:1)

取决于您要完全管理的内容。如果问题与连续的行之间的差异有关,则使用sec.axis# dummy data df_dummy = data.frame('Plan_code'=c('A','B','C','D','E','F','G'), 'Total'=c(191432,180241,99164,58443,56616,29579,19510),'STP'=c(41,40,44,37,37,37,45)) # creation of plot [![g <- ggplot(data = df_dummy, aes(x = Plan_code, y = Total)) + geom_col(aes(fill = 'Total')) + geom_line(data = df_dummy, aes(x = Plan_code, y = STP,group=1)) + geom_point(data = df_dummy, aes(x = Plan_code,y=STP)) + geom_label(data = df_dummy, aes(x = Plan_code, y = STP, fill = Plan_code, label = paste0('%', STP)), color = 'white', vjust = 1.6, size = 3) + scale_y_continuous(sec.axis = sec_axis(~. / 2000, name = 'PERCENT')) + labs(fill = NULL, color = NULL) + theme_minimal() print(g)][1]][1] 窗口分析函数可能是一种聪明的方法:

lead()

答案 2 :(得分:0)

您可以像这样查询它,

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;

答案 3 :(得分:-1)

您可以尝试以下操作

Select * , `entity1`-`entity2` As `entity3` 
FROM tbl

希望这会有所帮助。

答案 4 :(得分:-1)

在您的问题中,他们有两个理解,首先是您要从最大值中删除,而其他人提供给您的解决方案是您要从最后一行/第二行中删除。因此,为您提供了第二种解决方案。请讨论是否有任何查询。

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