Some small summaries about oracle views How to implement SQL: add similar data row by row?

Recently, there is a write view on the project. The function is to display the balance of the current year to today according to the insurance type X code X currency X company category.

The problem is, these different types of businesses don't make payments every day, some have several. Some haven't been there for a long time. But the result that the view has to provide is a balance display that needs to show all types of transactions that have ever occurred.


--这段sql也比较正常,显示的就是普通的查询
create or replace view v_n_test_jine as  -- 视图和查询语句没什么差别,怎么写都行
SELECT h.urancecode C_PROD_NO,  --险种编码
b.accountcode C_SBJT_NO,  --科目类型
f.code C_DPTACC_NO,  --做账机构
w.name C_CUR_NO,   --币种localcreditamountsum --本币贷方  localdebitamountsum, -- 本币借方
(case when b.accountcode in('6031') then sum(b.localcreditamountsum - b.localdebitamountsum)  when b.accountcode in('6541','6542','6421','651101','651105')  then sum(b.localdebitamountsum - b.localcreditamountsum) end ) as N_JINE, 
--sum(b.localcreditamountsum - b.localdebitamountsum) N_AMT ,  --合计金额
substr(b.prepareddatev, 1, 10) T_DUE_TM,  --凭证时间
--to_char(sysdate , 'yyyy-mm-dd') T_CRT_TM     --生成时间
to_char(sysdate , 'yyyy-mm-dd hh24:mi:ss') T_CRT_TM --生成时间
from (select * from 
                    (select
                                                     gl_detail.pk_voucher pk_voucher,
                                                   --  gl_detail.accountcode accountcode,
                                                  (case when substr(gl_detail.accountcode,0,4) = '6031' then '6031'
                                                   when substr(gl_detail.accountcode,0,4)  = '6541' then '6541'
                                                   when substr(gl_detail.accountcode,0,4)  = '6542' then '6542'
                                                   when substr(gl_detail.accountcode,0,4)  = '6421' then '6421'
                                                   when substr(gl_detail.accountcode,0,6)  = '651101' then '651101'
                                                   when substr(gl_detail.accountcode,0,6)  = '651105' then '651105'
                                                   end
                                                   )as accountcode,
                                                     gl_detail.assid assid,
                                                     gl_detail.pk_currtype pk_currtype,
                                                     gl_detail.pk_accasoa pk_accasoa,

                                                     gl_detail.prepareddatev prepareddatev,
                                                     sum(gl_detail.debitamount) localdebitamountsum, -- 原币借方
                                                     sum(gl_detail.creditamount) localcreditamountsum --原币贷方

                                                      from gl_detail gl_detail
                                                     where  gl_detail.dr = 0
                                                    -- and gl_detail.accountcode in ('6031%','6541%','6542%','6421%','651101%','651105%')

                            and gl_detail.accountcode  like '6031%' or 

                            gl_detail.accountcode  like '6541%' or 
                                                    gl_detail.accountcode  like '6542%' or 
                                                    gl_detail.accountcode  like '6421%' or 
                                                    gl_detail.accountcode  like '651101%' or 
                                                    gl_detail.accountcode  like '651105%'

                                                     and gl_detail.adjustperiod <> to_char('12A')
                                                    group by
                                                              gl_detail.pk_voucher,
                                                              gl_detail.accountcode,
                                                              gl_detail.assid,
                                                             gl_detail.pk_currtype,
                                                              gl_detail.pk_accasoa,
                                                              gl_detail.prepareddatev)hh where    substr(hh.prepareddatev, 1, 4) >= to_char(sysdate,'yyyy')   
                                                               ) b,
     (select s.urancecode urancecode,g.assid assid from ins_bd_urancekind s,(select l.F18  F18, l.assid assid from gl_docfree1 l) g where s.pk_urancekind in g.F18) h,
     (select p.name name,p.pk_currtype pk_currtype from bd_currtype p) w, 
     (select code,pk_org from org_orgs)f,
     (select pk_org,pk_voucher from gl_voucher)v
where w.pk_currtype = b.pk_currtype
      and h.assid = b.assid
      and f.pk_org=v.pk_org
      and b.pk_voucher = v.pk_voucher
      group by h.urancecode,b.accountcode,w.name,substr(b.prepareddatev, 1, 10),f.code
      order by T_DUE_TM desc

Then this is just to realize that different types sum up the balances of the same day, so how to make the different types of each day equal to all the previous balances is a problem. The headache has not been resolved for a day, and a friend sent me such a reference.

How to implement SQL: add data in the same column row by row?

The data of the first row of the column is placed in the first row of the latter column, the sum of the first row and the second row is placed in the second row of the latter column, the sum of the first three columns is placed in the third row of the latter column, and so on.

For example:
column A column B
11.25 11.25
25.25 36.5
50 86.5
60 146.5
To achieve the data of column B

oracle实现的sql为:
SELECT t2.A, SUM(t1.A)
FROM (SELECT a, rownum RK from tab) t1
(SELECT a, rownum RK from tab) t2
WHERE t1.rk <= t2.rk
group by t2.a
ORDER BY T2.A

DB2实现的为:
SELECT t2.A, SUM(t1.A)
FROM (SELECT a, ROW_NUMBER() OVER(ORDER BY A) RK from tab) t1, (SELECT a, ROW_NUMBER() OVER(ORDER BY A) RK from tab) t2
WHERE t1.rk <= t2.rk
group by t2.a
ORDER BY T2.A

This shows two problems. First, I am still not very good at using search, and the direction of search is not right. The second is that I am not familiar enough with SQL queries.
The main idea of ​​​​this sql is, how to make a chicken kiss yourself? The answer is to clone one.
By reading this sql and transforming it,

Nest a view like this:

create or replace view web_Res_NC as
select t2.C_PROD_NO, --险种编码
t2.C_SBJT_NO,  --科目类型
t2.C_DPTACC_NO, --做账机构
t2.C_CUR_NO,   --币种
--t2.N_JINE,   --合计金额
sum(t1.N_JINE)  N_AMT,  --累计合计金额
t2.T_DUE_TM,  --业务时间
to_char(sysdate , 'yyyy-mm-dd') T_CRT_TM  --生成时间
from 
(select C_PROD_NO,
C_SBJT_NO,
C_DPTACC_NO, --做账机构
C_CUR_NO,
sum(N_JINE)  N_JINE,
T_DUE_TM  --业务时间
 --T_CRT_TM,     --生成时间
from v_n_test_jine group by C_PROD_NO,C_SBJT_NO,C_DPTACC_NO,C_CUR_NO,T_DUE_TM )t1,
(select C_PROD_NO,
C_SBJT_NO,
C_DPTACC_NO, --做账机构
C_CUR_NO,
sum(N_JINE) N_JINE,
T_DUE_TM  --业务时间
 --T_CRT_TM,     --生成时间
from v_n_test_jine group by C_PROD_NO,C_SBJT_NO,C_DPTACC_NO,C_CUR_NO,T_DUE_TM )t2
where to_date(t1.T_DUE_TM||'00:00:01','yyyy-mm-dd hh24:mi:ss')<=to_date(t2.T_DUE_TM||'00:00:01','yyyy-mm-dd hh24:mi:ss') 
and t1.C_PROD_NO=t2.C_PROD_NO 
and t1.C_SBJT_NO=t2.C_SBJT_NO 
and t1.C_CUR_NO=t2.C_CUR_NO
and t1.C_DPTACC_NO=t2.C_DPTACC_NO
group by t2.C_PROD_NO,
t2.C_SBJT_NO,
t2.C_DPTACC_NO,
t2.C_CUR_NO,
t2.T_DUE_TM
order by  t2.T_DUE_TM desc

The key content here is that each item of the same type with data on that day can have the addition of the balance to the beginning of the year, and the daily records are also displayed.
write picture description here
So, what the implementation allows me to achieve is to use the ordinary select * from view name where T_DUE_TM=”the date to be queried”;
I can’t figure this out after thinking about it for a long time. Allow the view to query any date, and find out the balance and sum of all existing types of business before the query date, only from the first day of the beginning of the year. Classes and, as long as there are new additions the next day, have to be added and displayed in the table. It really can't be done.

In the end I can only give a piece of sql to achieve such a function:

select a.C_PROD_NO,a.C_SBJT_NO,a.C_DPTACC_NO, a.C_CUR_NO, n.n_amt,n.t_due_tm, n.t_crt_tm from web_Res_NC n,
(select r.C_PROD_NO C_PROD_NO ,r.C_SBJT_NO C_SBJT_NO,r.C_DPTACC_NO C_DPTACC_NO,r.C_CUR_NO C_CUR_NO, max(t_due_tm) t_due_tm from  web_Res_NC r 
where substr(r.T_DUE_TM, 1, 10) <=to_char('2018-03-15') group by  r.C_PROD_NO,r.C_SBJT_NO,r.C_DPTACC_NO,r.C_CUR_NO) a 
where a.C_PROD_NO= n.C_PROD_NO and a.C_SBJT_NO=n.C_SBJT_NO and a.C_DPTACC_NO=n.C_DPTACC_NO and a.C_CUR_NO=n.C_CUR_NO and a.t_due_tm= n.t_due_tm

There is also a problem that has not been solved in this sql, that is, when group by is used, because the table fields that need to be queried all need group by, and the balance does not need to be aggregated, it can only be nested again, and then write = . I have little experience, so I don't know any other way yet.
Later, I used the function to write the sql of the function implemented by the segment, which is much simpler than this. However, this learning experience is still very useful.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324687733&siteId=291194637