Logic Works 06
Logic Works 06
I was curious to avoid global temporary table, and make the script concise.
The problem remained the same, but I am presenting 5 different ways to solve.There
are other ways, but these ways increased the complexity and my curiosity to solve
the problem.
CASE LOGIC 05
DATE 01-04-10
*************
--OPTION 01--
PROBLEM DEFINITION
--FOR EACH CODE IN THE FLAT FILE, THERE ARE VALUES.
--THE BUSINESS WANTS TO DISPLAY ONLY THE NON-ZERO VALUES IN ASCENDING ORDER.
--FROM THE DISPLAYED RECORDS DISCARD THE FIRST 1/3 RD RECORDS AND LAST 1/3 RD
RECORDS.
--FIND THE AVERAGE OF THE REMAINING MIDDLE 1/3 RD RECORDS ONLY FOR EACH CODE.
$ cat associate.txt
100|0|0|0|0|0|0|0|0|0|0|55474|0|0|0|47769|0|95915|0|0|61401|0|0|0|0|0|0|0|0|0|0|0|
0|0|0|0
111|0|0|0|0|41165|0|54744|0|0|0|55480|51781|0|0|54000|0|46481|0|0|64051|0|0|0|0|0|
0|0|0|0|0|0|0|0|0|0
101|0|0|0|0|0|0|0|0|0|0|0|0|0|0|51804|0|55116|0|0|54100|0|0|0|0|0|0|0|0|0|0|0|0|0|
0|0
104|0|57617|0|0|0|0|0|0|0|0|0|51781|0|49574|45448|0|0|0|0|49557|0|0|0|0|0|0|0|0|0|
0|0|0|0|0|0
114|0|0|0|0|0|0|0|0|0|0|61711|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0
105|0|0|0|0|0|0|0|0|0|0|0|0|0|49574|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0
106|0|0|0|0|0|0|0|0|0|55141|0|0|0|0|0|0|40546|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0
107|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0
108|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0
109|0|0|0|0|45844|0|54015|0|0|0|64480|0|0|44104|54618|0|41449|0|0|48745|0|0|0|0|0|
0|0|0|0|0|0|0|0|0|0
sqlplus / as sysdba
v_ct1 number(10);
v_ct2 number(10);
v_sum number(10);
v_avg number(10);
begin
v_ct1 :=0;
v_ct2 :=0;
v_sum :=0;
v_avg :=0;
execute immediate ' select sum(col1), avg(col1) from ( select rownum rnum , t1.*
from ('||
' select col1 from counter_jp order by col1) t1) t2 where t2.rnum >'
||v_ct2||' and t2.rnum <=('||v_ct1||' - '||v_ct2||') ' into v_sum, v_avg;
dbms_output.put_line('-');
end if;
commit;
execute immediate 'truncate table counter_jp';
exception
when others then
dbms_output.put_line(c1.code||' '||sqlerrm);
end;
end loop;
end;
/
100 4 1
100 4 1 116875 58438
-
101 3 1
101 3 1 54100 54100
-
104 5 1
104 5 1 150912 50304
-
109 7 2
109 7 2 148604 49535
-
111 7 2
111 7 2 160525 53508
-
References:
http://www.adp-gmbh.ch/ora/misc/ext_table_2.html
Acknowledgements:
Thanks Vineela, for designing test case and scripting.
*******************Written 09/14/2018****************
--OPTION 02--
connect veeksha/saketh
Elapsed: 00:00:00.51
References:
http://docshare.tips/logic-works-01_575867e2b6d87fb1268b45f2.html --CASE
LOGIC
05
https://www.scribd.com/doc/59684433/Logic-Works-01 --CASE LOGIC 05
Acknowledgements:
Thanks Vineela, for setting up vbox node and scripting.
*****************Written 09/14/2018***************************
--OPTION 03--
import pandas as pd
import math
#df=pd.DataFrame(pd.read_csv('c:/jpscripts/associate.csv',sep=',',index_col=0,heade
r=None))
df=pd.read_csv('c:/jpscripts/associate.csv',sep=',',index_col=0,header=None)
'''
df[0:1].unstack().value_counts().shape
[y for y in df[:1].unstack() if y != 0]
df[0:1].unstack()[df[0:1].unstack()!=0]
for i in sorted(df[:2].unstack()[df[:2].unstack()!=0]):
print(i)
'''
for i in range(len(df)):
length=df[i:i+1].unstack()[df[i:i+1].unstack()!=0].count()
#print(length)
if (length >=3):
cnt=math.trunc(length/3)
tot=length - 2*cnt
seq=0
val=0
#print(cnt,tot)
print(df.index[i])
#print(df[i:i+1].unstack()[df[i:i+1].unstack()!=0].value_counts())
for i in sorted(df[i:i+1].unstack()[df[i:i+1].unstack()!=0]):
seq +=1
if ((seq > cnt) and seq <= (cnt + tot)):
val += i
print(seq,i)
if (val > 0):
print(length,val,math.trunc(val/tot)) #round(val/tot,2)
100
2 55474
3 61401
4 116875 58437
111
3 51781
4 54000
5 54744
7 160525 53508
101
2 54100
3 54100 54100
104
2 49557
3 49574
4 51781
5 150912 50304
109
3 45844
4 48745
5 54015
7 148604 49534
From the above scripts' output, there are few formatting differences.But the
displayed data confirms to the requirements of the problem's definition.
Happy scripting.
*******************Written 03/08/2019****************
--OPTION 04--
connect veeksha/saketh
Elapsed: 00:00:00.13
----------------------------------------------------------------
*******************Written 03/08/2019****************
--OPTION 05--
connect veeksha/saketh
set pagesize 20
with
t1 as (
select code,value from xtern_associate_jp
unpivot(value for value_type in(
COL1,
COL2,
COL3,
COL4,
COL5,
COL6,
COL7,
COL8,
COL9,
COL10,
COL11,
COL12,
COL13,
COL14,
COL15,
COL16,
COL17,
COL18,
COL19,
COL20,
COL21,
COL22,
COL23,
COL24,
COL25,
COL26,
COL27,
COL28,
COL29,
COL30,
COL31,
COL32,
COL33,
COL34,
COL35)) where value >0 order by code,value),
t2 as (select code,count(value) cnt, trunc(count(value)/3) bg,(count(value) -
trunc(count(value)/3)) en from t1 group by code having count(value) >=3),
t3 as (select t.code,value,row_num from (select code,value,rank() over(partition
by code order by value) as row_num from t1) t, t2 where t.code=t2.code and
t.row_num>t2.bg and t.row_num <= t2.en),
t4 as (select code,trunc(avg(value)) avg_value from t3 group by code)
select t3.code,t3.value,t4.avg_value from t3,t4 where t3.code = t4.code order by 1
12 rows selected.
--Happy scripting.