20180516统计系统利率债的历史成交

问题点:

1.求近1年内 银行、基金、证券、保险、其它 机构的成交量的统计信息 ,并按照此顺序进行排序;

2.成交双方列转行 (buyer   union all  seller) 

3.1年( dealdate  between   add_months(sysdate,-12)  and trunc(sysdate));

4.指定分组

case when b.membertype in(1,2,8,9)  then 1 -- 银行
      when b.membertype in (10,11) then 3  --证券
      when b.membertype in (6,7) then 4  -- 保险
      when b.membertype in (5,12) then 2  --基金
         else 5  end membertype ,    --其它

5.排序 (1,2,3,4,5)

CFGNAME CFGVALUE GROUP
外资商业银行 1 银行
内资商业银行 2
外资财务公司 3 其它
内资财务公司 4
内资基金公司 5 基金
外资保险公司 6 保险
内资保险公司 7
农联社(农村商业银行) 8 银行
国有银行 9
外资证券公司 10 证券
内资证券公司 11
社保基金 12 基金
信托公司 13 其它
内资其他 20

实现:

 1 ----buyer 方 :
 2 with buyer  as (
 3 select to_char(dealdate,'yyyymmdd') dealdate,
 4 decode(membertype,1,'银行',2,'基金',4,'保险',3,'证券',5,'其它','false') mtype,
 5 sum(dealprice) sumBuyerPrice
 6 from (
 7 select m.dealdate, m.buyerid,
 8  case when b.membertype in(1,2,8,9)  then 1 -- 银行
 9       when b.membertype in (10,11) then 3  --证券
10       when   b.membertype in (6,7) then 4  -- 保险
11       when   b.membertype in (5,12) then 2 --基金
12                 else 5 --其它
13                   end membertype ,
14  s.cfgname, m.dealprice
15   from his_product_deal_main m, base_member b,sys_configs s
16  where m.buyerid = b.id 
17   and to_number(s.cfgvalue) =b.membertype
18    and m.state = 9
19    and s.cfgtype=8
20   -- and b.state=0
21   and b.membertype is not null
22    and m.dealdate between  trunc(add_months(sysdate ,-12)) and
23        trunc(sysdate))  
24        group by membertype , dealdate 
25        order by dealdate ,membertype ) , seller as (
26 
27 
28 --498
29 select to_char(dealdate,'yyyymmdd') dealdate,
30 decode(membertype,1,'银行',2,'基金',4,'保险',3,'证券',5,'其它','false') mtype,
31 sum(dealprice) sumSellerPrice
32 from (
33 select m.dealdate, m.sellerid,
34  case when b.membertype in(1,2,8,9)  then 1 -- 银行
35       when b.membertype in (10,11) then 3  --证券
36       when   b.membertype in (6,7) then 4  -- 保险
37       when   b.membertype in (5,12) then 2 --基金
38                 else 5 --其它
39                   end membertype ,
40  s.cfgname, m.dealprice
41   from his_product_deal_main m, base_member b,sys_configs s
42  where m.sellerid = b.id 
43   and to_number(s.cfgvalue) =b.membertype
44    
45    and m.state = 9
46    and s.cfgtype=8
47   -- and b.state=0
48   and b.membertype is not null 
49      and m.dealdate between  trunc(add_months(sysdate ,-12)) and
50        trunc(sysdate)) 
51        group by membertype , dealdate 
52        order by dealdate ,membertype 
53        )
54        select b.dealdate,
55        s.dealdate ,
56        b.mtype ,
57        s.mtype ,
58        b.sumBuyerPrice,
59         s.sumSellerPrice
60        -- ,sum(b.sumBuyerPrice) over (partition by b.dealdate ) bb,
61       --  sum(s.sumSellerPrice) over (partition by s.dealdate ) bs
62        from buyer  b left join  seller s on b.dealdate =s.dealdate and    b.mtype =s.mtype
63    --  where b.dealdate =s.dealdate(+) 
64      --     and b.mtype =s.mtype(+)
原文地址:https://www.cnblogs.com/linbo3168/p/9049797.html