SQL选择每组其他记录的第一行子查询设置为零

时间:2018-01-12 10:27:55

标签: sql oracle oracle11g

请检查打击查询。我希望通过oracle查询得到以下结果。 uPDATED COLUMN NAME AS PER QUERY

SQL选择每个组的第一行子查询,其他记录设置为零

查询:

SELECT a.SRNO, a.srno_dtl, a.REPORT_HEAD_NAME, a.current_gl_srno, a.Fixed_gl_srno,
    NVL((SELECT SUM(NVL(C.CURBAL,0))
    FROM GLMAS C
    WHERE C.BRANCH_CODE='2'
        AND  C.ACC_CODE=a.CURRENT_GL_SRNO
        AND (C.DATE_OP <='04-Sep-2017' )
        AND (C.DATE_CL IS NULL OR C.DATE_CL > '04-Sep-2017' )),0)
    AS MIN_LIMIT,

    NVL((SELECT SUM(NVL(C.CURBAL,0))
    FROM GLMAS C
    WHERE C.BRANCH_CODE='2'
         AND C.ACC_CODE=a.FIXED_GL_SRNO
         AND (C.DATE_OP <='04-Sep-2017' )

         AND (C.DATE_CL IS NULL OR C.DATE_CL > '04-Sep-2017' )),0)
    AS MAX_LIMIT
FROM INTER_BANK_EXP_HEAD a
ORDER BY a.SRNO,a.srno_dtl;

结果:

SRNO srno_dtl  REPORT_HEAD_NAME  current_gl_srno  Fixed_gl_srno  MIN_LIMIT  MAX_LIMIT
1    1         Axis Bank         330440           350130         -311510       -5012345
1    2         Axis Bank         330460           350130         -14028.2      -5012345
1    3         Axis Bank         330450           350130         -7734.76      -5012345
2    1         IDBI Bank         400080           350131         -390141       -6012345
2    2         IDBI Bank         200330           350131          10052.78     -6012345
2    3         IDBI Bank         330420           350131          347989.3     -6012345

我想要这个结果:

SRNO srno_dtl  REPORT_HEAD_NAME  current_gl_srno  Fixed_gl_srno  MIN_LIMIT  MAX_LIMIT
1    1         Axis Bank         330440           350130         -311510       -5012345
1    2         Axis Bank         330460           350130         -14028.2      0
1    3         Axis Bank         330450           350130         -7734.76      0
2    1         IDBI Bank         400080           350131         -390141       -6012345
2    2         IDBI Bank         200330           350131          10052.78     0
2    3         IDBI Bank         330420           350131          347989.3     0

2 个答案:

答案 0 :(得分:0)

SELECT a.SRNO, a.srno_dtl, a.REPORT_HEAD_NAME, a.current_gl_srno, 
a.Fixed_gl_srno,
    NVL((SELECT SUM(NVL(C.CURBAL,0))
    FROM GLMAS C
WHERE C.BRANCH_CODE='2'
    AND  C.ACC_CODE=a.CURRENT_GL_SRNO
    AND (C.DATE_OP <='04-Sep-2017' )
    AND (C.DATE_CL IS NULL OR C.DATE_CL > '04-Sep-2017' )),0)
AS MIN_LIMIT,

case when  a.srno_dtl = 0 
     then NVL((SELECT  SUM(NVL(C.CURBAL,0))
            FROM GLMAS C
            WHERE C.BRANCH_CODE='2'
                 AND C.ACC_CODE=a.FIXED_GL_SRNO
                 AND (C.DATE_OP <='04-Sep-2017' )

                 AND (C.DATE_CL IS NULL OR C.DATE_CL > '04-Sep-2017' )),0)         
     else 0
     end 
AS MAX_LIMIT
FROM INTER_BANK_EXP_HEAD a
ORDER BY a.SRNO,a.srno_dtl;

答案 1 :(得分:0)

假设您的结果集中有一个表格。要获得所需结果,请使用LAG函数将列值与上一行的列值进行比较:

select e.*, case when e.camount = nvl(lag(e.camount,1,null) over (order by e.id, e.sub_id),0) then e.camount - nvl(lag(e.camount,1,null) over (order by e.id, e.sub_id),0) 
            else e.camount
            end
  from INTER_BANK_EXP e;

D e m o 1

如果您的camount对于多个银行保持不变(感谢@MrSmithGoesToWashington),您可以使用以下sql:

select e.*, case when e.id = nvl(lag(e.id,1,null) over (order by e.id, e.sub_id),0) then e.camount - nvl(lag(e.camount,1,null) over (order by e.id, e.sub_id),0) 
            else e.camount
            end
  from INTER_BANK_EXP e;

D e m o 2