how to Combine two tables column in oracle? - oracle

Table 1:
CURRENCY CODE ER GUARANTOR ID G AMOUNT
USD 1.2986 117 750
AED 4.76976 117 5750
ZAR 11.4717 117 234
INR 70.676 117 1243
AMD 526.5823 117 500000
EUR 1 117 12435
ALL 139.63197 117 2000000
EUR 1 173 200000
EUR 1 217 20000000
INR 70.676 26 100000
AED 4.76976 43 1000000
EUR 1 53 10000
Table 2:
F AMOUNT
USD 1.2986 117 450
AED 4.76976 117 7900
INR 70.676 117 2237.4
ZAR 11.4717 117 140.4
AMD 526.5823 117 500000
EUR 1 117 6961
ALL 139.63197 117 2000000
EUR 1 173 20000
EUR 1 217 14000000
INR 70.676 26 300000
AED 4.76976 43 2000000
EUR 1 53 10000
Result:
CURRENCY CODE ER GUARANTOR ID G AMOUNT F AMOUNT
USD 1.2986 117 750 450
AED 4.76976 117 5750 7900
ZAR 11.4717 117 234 2237.4
INR 70.676 117 1243 140.4
AMD 526.5823 117 500000 500000
EUR 1 117 12435
ALL 139.63197 117 2000000
EUR 1 173 200000
EUR 1 217 20000000
INR 70.676 26 100000
AED 4.76976 43 1000000
EUR 1 53 10000
I want to combine Both table like i need all the column in table 1 and F AMOUNT column from table 2. how to achieve this?
Thanks in Advance.

use the below query
select t1.CURRENCY CODE
, t1.ER
, t1.GUARANTOR
, t1.ID
, t2.FAMOUNT
from table1 t1
, table2 t2
where t1.CURRENCY CODE=t2.CURRENCY CODE

Related

To list out the hierarchy in depth on employees table using CTE

I am trying to find the hierarchy in the employees table using a CTE Query, using the below query.
WITH MANAGERS
(EMPID,
ENAME,
MID,
MNAME,
DEPTH)
AS
(
SELECT EMPLOYEE_ID AS EMPID,
FIRST_NAME||' '||LAST_NAME AS ENAME,
MANAGER_ID AS MID,
FIRST_NAME||' '||LAST_NAME AS MNAME,
0
FROM EMPLOYEES
WHERE MANAGER_ID IS NULL
UNION ALL
SELECT EMPLOYEES.EMPLOYEE_ID AS EMPID,
EMPLOYEES.FIRST_NAME||' '||EMPLOYEES.LAST_NAME AS ENAME,
EMPLOYEES.MANAGER_ID AS MID,
MANAGERS.MNAME,
MANAGERS.DEPTH+1
FROM EMPLOYEES JOIN MANAGERS
ON EMPLOYEES.MANAGER_ID=MANAGERS.EMPID
)
SELECT * FROM MANAGERS
ORDER BY DEPTH;
But in the result "Steven King" repeats as manager_name for all the records, instead of the respective manager name. Can someone help me identify the error in my SQL?
Thanks in advance
Helina
You are joining "back" to the employees table to get the manager name. But when you join back, you are now looking at that manager as an employee, so it is the employee name not the manager name column you want
SQL> WITH MANAGERS
2 (EMPID,
3 ENAME,
4 MID,
5 MNAME,
6 DEPTH)
7 AS
8 (
9 SELECT EMPLOYEE_ID AS EMPID,
10 FIRST_NAME||' '||LAST_NAME AS ENAME,
11 MANAGER_ID AS MID,
12 FIRST_NAME||' '||LAST_NAME AS MNAME,
13 0
14 FROM EMPLOYEES
15 WHERE MANAGER_ID IS NULL
16 UNION ALL
17 SELECT EMPLOYEES.EMPLOYEE_ID AS EMPID,
18 EMPLOYEES.FIRST_NAME||' '||EMPLOYEES.LAST_NAME AS ENAME,
19 EMPLOYEES.MANAGER_ID AS MID,
20 MANAGERS.ENAME,
21 MANAGERS.DEPTH+1
22 FROM EMPLOYEES JOIN MANAGERS
23 ON EMPLOYEES.MANAGER_ID=MANAGERS.EMPID
24 )
25 SELECT * FROM MANAGERS
26 ORDER BY DEPTH;
EMPID ENAME MID MNAME DEPTH
---------- ---------------------------------------------- ---------- ---------------------------------------------- ----------
100 Steven King Steven King 0
101 Neena Kochhar 100 Steven King 1
102 Lex De Haan 100 Steven King 1
114 Den Raphaely 100 Steven King 1
120 Matthew Weiss 100 Steven King 1
121 Adam Fripp 100 Steven King 1
122 Payam Kaufling 100 Steven King 1
123 Shanta Vollman 100 Steven King 1
124 Kevin Mourgos 100 Steven King 1
145 John Russell 100 Steven King 1
146 Karen Partners 100 Steven King 1
147 Alberto Errazuriz 100 Steven King 1
148 Gerald Cambrault 100 Steven King 1
149 Eleni Zlotkey 100 Steven King 1
201 Michael Hartstein 100 Steven King 1
202 Pat Fay 201 Michael Hartstein 2
200 Jennifer Whalen 101 Neena Kochhar 2
203 Susan Mavris 101 Neena Kochhar 2
204 Hermann Baer 101 Neena Kochhar 2
205 Shelley Higgins 101 Neena Kochhar 2
108 Nancy Greenberg 101 Neena Kochhar 2
103 Alexander Hunold 102 Lex De Haan 2
115 Alexander Khoo 114 Den Raphaely 2
116 Shelli Baida 114 Den Raphaely 2
117 Sigal Tobias 114 Den Raphaely 2
118 Guy Himuro 114 Den Raphaely 2
119 Karen Colmenares 114 Den Raphaely 2
125 Julia Nayer 120 Matthew Weiss 2
126 Irene Mikkilineni 120 Matthew Weiss 2
127 James Landry 120 Matthew Weiss 2
128 Steven Markle 120 Matthew Weiss 2
180 Winston Taylor 120 Matthew Weiss 2
181 Jean Fleaur 120 Matthew Weiss 2
182 Martha Sullivan 120 Matthew Weiss 2
183 Girard Geoni 120 Matthew Weiss 2
129 Laura Bissot 121 Adam Fripp 2
130 Mozhe Atkinson 121 Adam Fripp 2
131 James Marlow 121 Adam Fripp 2
132 TJ Olson 121 Adam Fripp 2
184 Nandita Sarchand 121 Adam Fripp 2
185 Alexis Bull 121 Adam Fripp 2
186 Julia Dellinger 121 Adam Fripp 2
187 Anthony Cabrio 121 Adam Fripp 2
133 Jason Mallin 122 Payam Kaufling 2
134 Michael Rogers 122 Payam Kaufling 2
135 Ki Gee 122 Payam Kaufling 2
136 Hazel Philtanker 122 Payam Kaufling 2
188 Kelly Chung 122 Payam Kaufling 2
189 Jennifer Dilly 122 Payam Kaufling 2
190 Timothy Gates 122 Payam Kaufling 2
191 Randall Perkins 122 Payam Kaufling 2
137 Renske Ladwig 123 Shanta Vollman 2
138 Stephen Stiles 123 Shanta Vollman 2
139 John Seo 123 Shanta Vollman 2
140 Joshua Patel 123 Shanta Vollman 2
192 Sarah Bell 123 Shanta Vollman 2
193 Britney Everett 123 Shanta Vollman 2
194 Samuel McCain 123 Shanta Vollman 2
195 Vance Jones 123 Shanta Vollman 2
198 Donald OConnell 124 Kevin Mourgos 2
199 Douglas Grant 124 Kevin Mourgos 2
141 Trenna Rajs 124 Kevin Mourgos 2
142 Curtis Davies 124 Kevin Mourgos 2
143 Randall Matos 124 Kevin Mourgos 2
144 Peter Vargas 124 Kevin Mourgos 2
196 Alana Walsh 124 Kevin Mourgos 2
197 Kevin Feeney 124 Kevin Mourgos 2
150 Peter Tucker 145 John Russell 2
151 David Bernstein 145 John Russell 2
152 Peter Hall 145 John Russell 2
153 Christopher Olsen 145 John Russell 2
154 Nanette Cambrault 145 John Russell 2
155 Oliver Tuvault 145 John Russell 2
156 Janette King 146 Karen Partners 2
157 Patrick Sully 146 Karen Partners 2
158 Allan McEwen 146 Karen Partners 2
159 Lindsey Smith 146 Karen Partners 2
160 Louise Doran 146 Karen Partners 2
161 Sarath Sewall 146 Karen Partners 2
162 Clara Vishney 147 Alberto Errazuriz 2
163 Danielle Greene 147 Alberto Errazuriz 2
164 Mattea Marvins 147 Alberto Errazuriz 2
165 David Lee 147 Alberto Errazuriz 2
166 Sundar Ande 147 Alberto Errazuriz 2
167 Amit Banda 147 Alberto Errazuriz 2
168 Lisa Ozer 148 Gerald Cambrault 2
169 Harrison Bloom 148 Gerald Cambrault 2
170 Tayler Fox 148 Gerald Cambrault 2
171 William Smith 148 Gerald Cambrault 2
172 Elizabeth Bates 148 Gerald Cambrault 2
173 Sundita Kumar 148 Gerald Cambrault 2
174 Ellen Abel 149 Eleni Zlotkey 2
175 Alyssa Hutton 149 Eleni Zlotkey 2
176 Jonathon Taylor 149 Eleni Zlotkey 2
177 Jack Livingston 149 Eleni Zlotkey 2
178 Kimberely Grant 149 Eleni Zlotkey 2
179 Charles Johnson 149 Eleni Zlotkey 2
206 William Gietz 205 Shelley Higgins 3
109 Daniel Faviet 108 Nancy Greenberg 3
110 John Chen 108 Nancy Greenberg 3
111 Ismael Sciarra 108 Nancy Greenberg 3
107 Diana Lorentz 103 Alexander Hunold 3
113 Luis Popp 108 Nancy Greenberg 3
104 Bruce Ernst 103 Alexander Hunold 3
105 David Austin 103 Alexander Hunold 3
106 Valli Pataballa 103 Alexander Hunold 3
112 Jose Manuel Urman 108 Nancy Greenberg 3
107 rows selected.

Its possible to Group by data instead of column?

I was thinking, if it was possible to use GROUP BY based on the data of a certaint column in a expecific way, instead of the column. So my question is can i create groups based on the 0 occurence of a certant field.
DIA MES YEAR TODAY TOMORROW ANALYSIS LIMIT
---------- ---------- ---------- ---------- ---------- ---------- ----------
19 9 2016 111 988 0 150
20 9 2016 988 853 853 150
21 9 2016 853 895 895 150
22 9 2016 895 776 776 150
23 9 2016 776 954 0 150
26 9 2016 954 968 968 150
27 9 2016 968 810 810 150
28 9 2016 810 937 937 150
29 9 2016 937 769 769 150
30 9 2016 769 1020 0 150
3 10 2016 1020 923 923 150
4 10 2016 923 32 32 150
Like, in this case, i would want to create groups, like this:
Group 1 (Analysis): 0
Group 2(Analysis): 853, 895,776,0
Group 3(Analysis): 968,810,937,169,0
...
Assuming your table name is tbl, something like this should work (it's called "start-of-group" method if you want to Google it):
select
from ( select tbl.*,
count(case when analysis = 0 then 1 end)
over (order by year, mes, dia) as cnt
from tbl
)
where ...
GROUP BY cnt
;

Nurse scheduling model formulation in AMPL

I have been working on a Nurse scheduling problem in AMPL for the following conditions:
Total no. of Nurses=20
Total no. of shits= 3 #morning,day,night
Planning Horizon 7 days: let's say M T W R F Sa Su
Along with following constraints:
Max no. of working days in a week: 5
A rest days after 4 continuous
night shifts.
Consecutive night and morning shifts are not allowed.
Demand per shift is 7 nurses.
A nurse can only work in one shift per day, i.e. morning, night, day
Cost scenarios:
Morning shift: $12
Day shift: $13
Night shift : $15
Objective function is to minimize the cost of operation as per Nurse preferences.
Can anyone give me an idea of how this problem can be formulated ?
So at first some things unusual in your problem definition:
This is not a real optimization problem, since your objective function is fixed per definition (every shift has 7 nurses, and every nurse has an equal price per shift)
In your Problem you defined 7 nurses per shift with a maimum of 5 working days. So you need 7 nurses on three shifts on seven days. This equals 147 nurse/shifts. But with the cap of five working days and only one shift per day, you just have 20 Nurses on 5 shifts, which equals to 100 nurse/shifts.
I've built the problem in Mathprog but the code should be more or less equal to AMPL. I've started with three sets for the nurses, days and shifts.
set shifts := {1,2,3};
set days := {1,2,3,4,5,6,7};
set nurses := {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
The shedule is defined as a set of binary variables:
var schedule{nurses, days, shifts}, binary;
The simple objective contains the sum of all nurse/shifts in this week with the related prices:
minimize cost: sum{i in nurses, j in days}(schedule[i,j,1]*c_morning+schedule[i,j,2]*c_day+schedule[i,j,3]*c_night);
To your first constraint one can limit the sum of all shifts per nurse to five, since there is only one shift per day possible:
s.t. working_days{n in nurses}:
sum{i in days, j in shifts}(schedule[n,i,j]) <= 5;
The restday is the hardest part of the problem. For simplicity I've created another set which just contains the days, where a nurse could have achived four night-shifts in a row. You can also formulate the constraint with the original set of days and exclude the first four days.
set nigth_days := {5,6,7};
s.t. rest{n in nurses,i in nigth_days}:
(schedule[n,i-4,3]+schedule[n,i-3,3]+schedule[n,i-2,3]+schedule[n,i-1,3]+sum{j in shifts}(schedule[n,i,j])) <= 4;
For not having a morning-shift after a night-shift I used the same attempt like for the rest days. The seventh day is excluded, since there is no eigth day where we can look for a morning-shift.
set yester_days := {1,2,3,4,5,6};
s.t. night_morning{i in yester_days, n in nurses}:
(schedule[n,i,3]+schedule[n,i+1,1]) <= 1;
The demand of four nurses per shift should be met (I've reduced the number since more then 4 nurses are infeasible, due to the 5 shift limit)
s.t. demand_shift{i in days, j in shifts}:
sum{n in nurses}(schedule[n,i,j]) = 4;
The fifth constraint is to limit the shifts per day to a max of one.
s.t. one_shift{n in nurses, i in days}:
sum{ j in shifts}(schedule[n,i,j]) <= 1;
set nurse; #no. of full time employees working in the facility
set days; #planning horizon
set shift; #no. of shift in a day
set S; #shift correseponding to the outsourced nurses
set D;#day corresponding to the outsourced nurses
set N;#
# ith nurse working on day j
# j starts from Monday (j=1), Tuesday( j=2), Wednesday (j=3), Thursday(j=4), Friday(j=5), Saturday(j=6), Sunday(j=7)
#s be the shift as morning, day and night
param availability{i in nurse, j in days};
param costpershift{i in nurse, j in days, s in shift};
param outcost{n in N, l in D, m in S};
var nurseavailability{i in nurse,j in days,s in shift} binary; # = 1 if nurse i is available on jth day working on sth shift, 0 otherwise
var outsourced{n in N, l in D, m in S} integer;
#Objective function
minimize Cost: sum{i in nurse, j in days, s in shift} costpershift[i,j,s]*nurseavailability[i,j,s]+ sum{ n in N, l in D, m in S}outcost[n,l,m]*outsourced[n,l,m];
#constraints
#maximum no. of shifts per day
subject to maximum_shifts_perday {i in nurse,j in days}:
sum{s in shift} nurseavailability[i,j,s]*availability[i,j] <= 1;
#maximum no. of working says a week
subject to maximum_days_of_work {i in nurse}:
sum{j in days,s in shift} availability[i,j]*nurseavailability[i,j,s]<=5; #maximum working days irrespective of shifts
# rest days after night shifts
subject to rest_days_after_night_shift{i in nurse}:
sum{j in days} availability[i,j]*nurseavailability[i,j,3]<=4;
#demand per shift
subject to supply{j in days, s in shift, l in D, m in S}:
sum{i in nurse} availability[i,j]*nurseavailability[i,j,s] + sum{n in N} outsourced[n,l,m]=7;
#outsourcing only works well when there is more variability in supply.
#increasing the staff no. would be effective for reducing the cost variability in demand.
#considering a budget of $16,000 per week
#outsourcing constraints: a maximum of 20 nurses can be outsourced per shift
# no. of fulltime employees=30
#demand is 7 nurses per shift
#the average variability
#all nurses are paid equally # $12 per hour.
#cost of an outsourced shift is $144.
#cost of morning shift is $96.
#cost of day shift is $104.
#cost of night shift is $120.
data;
#set nurse ordered:= nurse1 nurse2 nurse3 nurse4 nurse5 nurse6 nurse7 nurse8
#nurse9 nurse10 nurse11 nurse12 nurse13 nurse14 nurse15 nurse16 nurse17
#nurse18 nurse19 nurse20;
set nurse:= 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30;
#set days ordered:= Monday Tuesday Wednesday Thursday Friday Saturday Sunday;
set days:= 1 2 3 4 5 6 7;
#set shift ordered:= Morning Day Night;
set shift:= 1 2 3;
set D:= 1 2 3 4 5 6 7; #outsourced days
set S:=1 2 3; #outshit
set N := 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20;
param outcost
[*,*,1]:
1 2 3 4 5 6 7:=
1 144 144 144 144 144 144 144
2 144 144 144 144 144 144 144
3 144 144 144 144 144 144 144
4 144 144 144 144 144 144 144
5 144 144 144 144 144 144 144
6 144 144 144 144 144 144 144
7 144 144 144 144 144 144 144
8 144 144 144 144 144 144 144
9 144 144 144 144 144 144 144
10 144 144 144 144 144 144 144
11 144 144 144 144 144 144 144
12 144 144 144 144 144 144 144
13 144 144 144 144 144 144 144
14 144 144 144 144 144 144 144
15 144 144 144 144 144 144 144
16 144 144 144 144 144 144 144
17 144 144 144 144 144 144 144
18 144 144 144 144 144 144 144
19 144 144 144 144 144 144 144
20 144 144 144 144 144 144 144
[*,*,2]:
1 2 3 4 5 6 7:=
1 144 144 144 144 144 144 144
2 144 144 144 144 144 144 144
3 144 144 144 144 144 144 144
4 144 144 144 144 144 144 144
5 144 144 144 144 144 144 144
6 144 144 144 144 144 144 144
7 144 144 144 144 144 144 144
8 144 144 144 144 144 144 144
9 144 144 144 144 144 144 144
10 144 144 144 144 144 144 144
11 144 144 144 144 144 144 144
12 144 144 144 144 144 144 144
13 144 144 144 144 144 144 144
14 144 144 144 144 144 144 144
15 144 144 144 144 144 144 144
16 144 144 144 144 144 144 144
17 144 144 144 144 144 144 144
18 144 144 144 144 144 144 144
19 144 144 144 144 144 144 144
20 144 144 144 144 144 144 144
[*,*,3]:
1 2 3 4 5 6 7:=
1 144 144 144 144 144 144 144
2 144 144 144 144 144 144 144
3 144 144 144 144 144 144 144
4 144 144 144 144 144 144 144
5 144 144 144 144 144 144 144
6 144 144 144 144 144 144 144
7 144 144 144 144 144 144 144
8 144 144 144 144 144 144 144
9 144 144 144 144 144 144 144
10 144 144 144 144 144 144 144
11 144 144 144 144 144 144 144
12 144 144 144 144 144 144 144
13 144 144 144 144 144 144 144
14 144 144 144 144 144 144 144
15 144 144 144 144 144 144 144
16 144 144 144 144 144 144 144
17 144 144 144 144 144 144 144
18 144 144 144 144 144 144 144
19 144 144 144 144 144 144 144
20 144 144 144 144 144 144 144;
param availability:
1 2 3 4 5 6 7 :=
1 0 0 0 0 0 0 0
2 1 1 1 1 1 1 1
3 1 1 1 1 1 1 1
4 1 1 1 1 1 1 1
5 1 1 1 1 1 1 1
6 1 1 1 1 1 1 1
7 1 0 1 1 1 1 1
8 1 1 1 1 1 1 1
9 1 1 1 1 1 1 1
10 1 1 1 1 1 1 1
11 1 1 1 1 1 1 1
12 1 1 1 1 1 1 1
13 1 1 1 1 1 1 1
14 1 1 1 1 1 1 1
15 1 1 1 1 1 1 1
16 1 1 1 1 1 1 1
17 0 1 1 1 1 1 1
18 1 1 1 1 1 1 1
19 1 1 1 1 1 1 1
20 1 1 1 1 1 1 1
21 1 1 1 1 1 1 1
22 1 1 1 1 1 1 1
23 1 1 1 1 1 1 1
24 1 1 1 1 1 1 1
25 1 1 1 1 1 1 1
26 1 1 1 1 1 1 1
27 1 1 1 1 1 1 1
28 1 1 1 1 1 1 1
29 1 1 1 1 1 1 1
30 1 1 1 1 1 1 1;
param costpershift:=
[*,*,1]: 1 2 3 4 5 6 7 :=
1 96 96 96 96 96 96 96
2 96 96 96 96 96 96 96
3 96 96 96 96 96 96 96
4 96 96 96 96 96 96 96
5 96 96 96 96 96 96 96
6 96 96 96 96 96 96 96
7 96 96 96 96 96 96 96
8 96 96 96 96 96 96 96
9 96 96 96 96 96 96 96
10 96 96 96 96 96 96 96
11 96 96 96 96 96 96 96
12 96 96 96 96 96 96 96
13 96 96 96 96 96 96 96
14 96 96 96 96 96 96 96
15 96 96 96 96 96 96 96
16 96 96 96 96 96 96 96
17 96 96 96 96 96 96 96
18 96 96 96 96 96 96 96
19 96 96 96 96 96 96 96
20 96 96 96 96 96 96 96
21 96 96 96 96 96 96 96
22 96 96 96 96 96 96 96
23 96 96 96 96 96 96 96
24 96 96 96 96 96 96 96
25 96 96 96 96 96 96 96
26 96 96 96 96 96 96 96
27 96 96 96 96 96 96 96
28 96 96 96 96 96 96 96
29 96 96 96 96 96 96 96
30 96 96 96 96 96 96 96
[*,*,2] : 1 2 3 4 5 6 7 :=
1 104 104 104 104 104 104 104
2 104 104 104 104 104 104 104
3 104 104 104 104 104 104 104
4 104 104 104 104 104 104 104
5 104 104 104 104 104 104 104
6 104 104 104 104 104 104 104
7 104 104 104 104 104 104 104
8 104 104 104 104 104 104 104
9 104 104 104 104 104 104 104
10 104 104 104 104 104 104 104
11 104 104 104 104 104 104 104
12 104 104 104 104 104 104 104
13 104 104 104 104 104 104 104
14 104 104 104 104 104 104 104
15 104 104 104 104 104 104 104
16 104 104 104 104 104 104 104
17 104 104 104 104 104 104 104
18 104 104 104 104 104 104 104
19 104 104 104 104 104 104 104
20 104 104 104 104 104 104 104
21 104 104 104 104 104 104 104
22 104 104 104 104 104 104 104
23 104 104 104 104 104 104 104
24 104 104 104 104 104 104 104
25 104 104 104 104 104 104 104
26 104 104 104 104 104 104 104
27 104 104 104 104 104 104 104
28 104 104 104 104 104 104 104
29 104 104 104 104 104 104 104
30 104 104 104 104 104 104 104
[*,*,3] : 1 2 3 4 5 6 7 :=
1 120 120 120 120 120 120 120
2 120 120 120 120 120 120 120
3 120 120 120 120 120 120 120
4 120 120 120 120 120 120 120
5 120 120 120 120 120 120 120
6 120 120 120 120 120 120 120
7 120 120 120 120 120 120 120
8 120 120 120 120 120 120 120
9 120 120 120 120 120 120 120
10 120 120 120 120 120 120 120
11 120 120 120 120 120 120 120
12 120 120 120 120 120 120 120
13 120 120 120 120 120 120 120
14 120 120 120 120 120 120 120
15 120 120 120 120 120 120 120
16 120 120 120 120 120 120 120
17 120 120 120 120 120 120 120
18 120 120 120 120 120 120 120
19 120 120 120 120 120 120 120
20 120 120 120 120 120 120 120
21 120 120 120 120 120 120 120
22 120 120 120 120 120 120 120
23 120 120 120 120 120 120 120
24 120 120 120 120 120 120 120
25 120 120 120 120 120 120 120
26 120 120 120 120 120 120 120
27 120 120 120 120 120 120 120
28 120 120 120 120 120 120 120
29 120 120 120 120 120 120 120
30 120 120 120 120 120 120 120;

Creating frequency interval in Crystal Report

I am trying to create a dataset of frequency interval in crystal report something like below. First column is rowid, second is start interval , third column is end interval and fourth column is interval name.
1 0 29 0 - 29
2 30 59 30 - 59
3 60 89 60 - 89
4 90 119 90 - 119
5 120 149 120 - 149
6 150 179 150 - 179
7 180 209 180 - 209
8 210 239 210 - 239
9 240 269 240 - 269
10 270 299 270 - 299
11 300 329 300 - 329
12 330 359 330 - 359
13 360 389 360 - 389
14 390 419 390 - 419
15 420 449 420 - 449
16 450 479 450 - 479
17 480 509 480 - 509
18 510 539 510 - 539
19 540 569 540 - 569
20 570 599 570 - 599
21 600 629 600 - 629
22 630 659 630 - 659
23 660 689 660 - 689
24 690 719 690 - 719
25 720 749 720 - 749
26 750 779 750 - 779
27 780 809 780 - 809
28 810 839 810 - 839
29 840 869 840 - 869
30 870 899 870 - 899
Can I write a CTE to generate this interval so that I can use it directly in crystal report without writing function on database side? Below is the code which I wrote:
declare intervalStart integer := 0;
intervalEnd integer := 900;
intervalMins varchar(10) := 30;
totalIntervals number := 0;
begin
begin
execute immediate 'create global temporary table intervalTable (row_Id int not null, intStart integer, intEnd integer, intervalName varchar2(25))ON COMMIT DELETE ROWS';
exception when others then dbms_output.put_line(sqlerrm);
end;
totalIntervals := intervalEnd/intervalMins;
--dbms_output.put_line(totalIntervals);
for i in 1 ..totalIntervals loop
intervalStart := 0;
intervalEnd := 0;
intervalStart := intervalStart + (i-1)*intervalMins;
intervalEnd := intervalEnd + (i*intervalMins)-1;
--dbms_output.put_line(intervalStart || ' - ' || intervalEnd);
insert into intervalTable
(
row_id,
intStart,
intEnd,
intervalName
)
values(i, intervalStart, intervalEnd, (intervalStart || ' - ' || intervalEnd));
end loop;
end;
I think you want something like this:
with freq_data as (
select level as id, (level-1)*30 as start_interval, ((level-1)*30) + 29 as end_interval, (level-1)*30 || ' - ' || to_char(((level-1)*30) + 29) as label
from dual
connect by level <= 30
order by level
)
select * from freq_data;
Output
ID START_INTERVAL END_INTERVAL LABEL
1 0 29 0 - 29
2 30 59 30 - 59
3 60 89 60 - 89
4 90 119 90 - 119
5 120 149 120 - 149
6 150 179 150 - 179
7 180 209 180 - 209
8 210 239 210 - 239
9 240 269 240 - 269
10 270 299 270 - 299
11 300 329 300 - 329
12 330 359 330 - 359
13 360 389 360 - 389
14 390 419 390 - 419
15 420 449 420 - 449
16 450 479 450 - 479
17 480 509 480 - 509
18 510 539 510 - 539
19 540 569 540 - 569
20 570 599 570 - 599
21 600 629 600 - 629
22 630 659 630 - 659
23 660 689 660 - 689
24 690 719 690 - 719
25 720 749 720 - 749
26 750 779 750 - 779
27 780 809 780 - 809
28 810 839 810 - 839
29 840 869 840 - 869
30 870 899 870 - 899
An example using the above in a join query:
create table my_test
(
num number
-- other important data ...
);
-- insert some random numbers
insert into my_test
select trunc(DBMS_RANDOM.VALUE(0,900))
from dual
connect by level <= 10;
commit;
Now joining to get the label for each num field:
with freq_data as (
select level as id, (level-1)*30 as start_interval, ((level-1)*30) + 29 as end_interval, (level-1)*30 || ' - ' || to_char(((level-1)*30) + 29) as label
from dual
connect by level <= 30
order by level
)
select t.num, d.label
from my_test t
left join freq_data d ON (t.num between d.start_interval and d.end_interval);
Output:
NUM LABEL
64 60 - 89
73 60 - 89
128 120 - 149
154 150 - 179
267 240 - 269
328 300 - 329
550 540 - 569
586 570 - 599
745 720 - 749
795 780 - 809

awk find the closest match of a list in a matrix

I look for common elements in two files or which row of a matrix has the most elements from a given row. what I understood until now is how to compare fields. I receive the lines which hold the same value in the same fieldnumber.
But how can I open the search to the other field numbers?
awk 'NR==FNR{a[$1];next}$1 in a{print $1" "FNR}' file1 file2
104 3
Expected output:
104 3 111 4 117 2 134 2 148 - 156 4 166 4 176 3 186 - 198 1 221 6 236 -
best match row 4 with 3 elements common.
file 1
104 111 117 134 148 156 166 176 186 198 221 236
file 2
102 108 116 124 132 141 151 162 173 185 198 211
103 109 117 125 134 143 153 163 175 187 200 213
104 110 118 126 135 144 154 165 176 188 201 215
105 111 119 127 136 145 156 166 178 190 203 217
106 112 120 128 137 147 157 168 179 192 205 219
107 113 121 130 139 148 158 169 181 193 207 221
108 114 122 131 140 150 160 171 183 195 208 200
This solution assumes 1) that file1 contains unique values as shown in the provided example and 2) there is only one top ranked line in file2.
awk -v string=$(cat file1 | tr " " ",") \
'{split(string,array,","); cnt=0;
for(i in array) {for(j=1;j<=NF;j++) if(array[i]==$j) cnt++};
if(cnt>cntmax) {cntmax=cnt; NRmax=NR}} END{print NRmax}' file2
4

Resources