搜索墙首页  编程技术  java  asp  ajax  php  c/c#/c++  数据库  oracle  mysql  db2  操作系统  windows  linux  股吧
首页 > 编程 > 数据库 > oracle
 1         
1楼  OpenSourceBolg 2009-12-30

I am sure using SQL analytical function, the following can be achieved using a single query:

Date_value | Cust_id | Customer_tenue | avg_bal 
 
01-aug-09 | 111 | 0 | 1000
01-aug-09 | 112 | 1 | 2000
01-aug-09 | 113 | 2 | 900
01-aug-09 | 114 | 3 | 1250
 
 
01-sep-09 | 111 | 1 | 1200
01-sep-09 | 112 | 2 | 2000
01-sep-09 | 113 | 3 | 1900
01-sep-09 | 114 | 4 | 1250
 
01-oct-09 | 111 | 2 | 1100
01-oct-09 | 112 | 3 | 2200
01-oct-09 | 113 | 4 | 1900



Expected result

If customer’s tenure is 0 then mark as ‘New’,
If customer’s balance is increased from last month then mark as ‘Augment’
If customer’s balance is same as last month then mark as ‘Maintain’
If customer’s balance is decreased from last month then mark as ‘Diminish’
Else ‘Left’

Help please....

 

 

The answer is :

with t as (
    select DATE '2009-08-01' Date_value, 111 Cust_id, 0 Customer_tenue, 1000 avg_bal from dual union all
    select DATE '2009-08-01', 112 , 1 , 2000 from dual union all
    select DATE '2009-08-01', 113 , 2 , 900 from dual union all
    select DATE '2009-08-01', 114 , 3 , 1250 from dual union all
    select DATE '2009-09-01', 111 , 1 , 1200 from dual union all
    select DATE '2009-09-01', 112 , 2 , 2000 from dual union all
    select DATE '2009-09-01', 113 , 3 , 1900 from dual union all
    select DATE '2009-09-01', 114 , 4 , 1250 from dual union all
    select DATE '2009-10-01', 111 , 2 , 1100 from dual union all
    select DATE '2009-10-01', 112 , 3 , 2200 from dual union all
    select DATE '2009-10-01', 113 , 4 , 1900 from dual)
   select date_value, cust_id, avg_bal, oldbal,
          case when Customer_tenue=0 then 'NEW'
               when oldbal<avg_bal then 'Augment'
               when oldbal=avg_bal then 'Maintain'
               when oldbal>avg_bal then 'Diminish'
               else 'Left' end status
     from (select date_value, cust_id, customer_tenue, avg_bal, LEAD(avg_bal) over (partition by cust_id order by date_value desc) oldbal
             from t)
   order by cust_id, date_value;

 1         
您的发言将按有关规定都会存档,您须为所发表后果负责,请您遵纪守法并注意语言文明。
标题:A example using analytical function
热门关注
标题回复点击
Oracle 10g (ASM):Sample Implementation0815
Oracle 正版用户授权0778
动态性能视图(Dynamic perfermance view V$)0768
EM 无法启动&&重新完全配置EM0635
OracleDBConsole 服务因 2 (0x2) 服务性错误而停止。请问一下如何解决!2455
小布老师Oracle 9i DBA Fundamentals II0404
ORA-1652: unable to extend temp segment诊断以及解决0389
Oracle Linux 和 iSCSI 上构建您自己的 Oracle RAC集群10377
locate pl/sql bottleneck0345
缓冲处理器 buffer handle0338
搜索墙@2009 www.pkwall.com all rights reserved QQ:276471788 [京ICP备09111534号]
声明:本站部分数据来源于网络,仅供参考,如有版权问题,请联系我们,我们将及时删除!转载本站请注明来源