retrieve row from multiple row of table in oracle - sql

I want to retrieve data from three table
for example
Table_1 : NAME_A
PD_ID
A
B
C
Table_2 : NAME_B Primary_key PD_ID,EV_N
PD_ID EV_N EV_DEC
A 1 one
A 2 Two
B 1 one
B 2 Two
B 3 THREE
C 1 one
C 2 Two
Table_3 : NAME_C Primary key PD_ID
PD_ID, FFT_NAME, FFT_DESC
A XY XY_DESC
B ZY ZY_DESC
B XY XY_DESC
C ZY ZY_DESC
C XY XY_DESC
C PY PY_DESC
Required Output
PD_ID EV_N EV_DEC FFT_NAME FFT_DESC
A 1 ONE XY XY_DESC
A 2 TWO
B 1 ONE ZY ZY_DESC
B 2 TWO XY XY_DESC
B 3 THREE
C 1 ONE ZY ZY_DESC
C 2 Two XY XY_DESC
PY PY_DESC

The idea is to range records in both tables and then use this range numbers in a full outer join:
with
t1 as (
select 'A' pd_id from dual union all
select 'B' pd_id from dual union all
select 'C' pd_id from dual
),
t2 as (
select 'A' pd_id, 1 EV_N, 'one' EV_DEC from dual union all
select 'A' pd_id, 2 EV_N, 'two' EV_DEC from dual union all
select 'B' pd_id, 1 EV_N, 'one' EV_DEC from dual union all
select 'B' pd_id, 2 EV_N, 'two' EV_DEC from dual union all
select 'B' pd_id, 3 EV_N, 'three' EV_DEC from dual union all
select 'C' pd_id, 1 EV_N, 'one' EV_DEC from dual union all
select 'C' pd_id, 2 EV_N, 'two' EV_DEC from dual
),
t3 as (
select 'A' pd_id, 'XY' FFT_NAME, 'XY_DESC' FFT_DESC from dual union all
select 'B' pd_id, 'ZY' FFT_NAME, 'ZY_DESC' FFT_DESC from dual union all
select 'B' pd_id, 'XY' FFT_NAME, 'XY_DESC' FFT_DESC from dual union all
select 'C' pd_id, 'ZY' FFT_NAME, 'ZY_DESC' FFT_DESC from dual union all
select 'C' pd_id, 'XY' FFT_NAME, 'XY_DESC' FFT_DESC from dual union all
select 'C' pd_id, 'PY' FFT_NAME, 'PY_DESC' FFT_DESC from dual
)
select coalesce(t22.pd_id,t33.pd_id) pd_id,
t22.ev_dec,
t33.FFT_NAME,
t33.FFT_DESC
from (
select pd_id, ev_n, ev_dec, row_number() over (partition by pd_id order by ev_n, ev_dec) rn
from t2
) t22
full join (
select pd_id, FFT_NAME, FFT_DESC, row_number() over (partition by pd_id order by FFT_NAME, FFT_DESC) rn
from t3
) t33
on t22.pd_id = t33.pd_id
and t22.rn = t33.rn

This won't produce the exact output you specify but it will produce a consistent and predictable output:
select t1.PD_ID
, t2.EV_N
, t2.EV_DEC
, t2.FFT_NAME
, t2.FFT_DESC
from name_a t1
cross join ( select coalesce(b.p_id, c.p_id) as p_id
, b.ev_n
, upper(b.ev_dec) as ev_dec
, c.fft_name
, c.fft_desc
from ( select * from name_b ) b
full outer join
( select c.*
, row_number() over (partition by c.p_id
order by c.fft_name) as rn
from name_c c) c
on c.p_id = b._pid
and c.rn = b.ev_n) t2
where t1.p_id = t2.p_id
order by t1.p_id
, t2.ev_n nulls last

Related

Generate Result based on max count in secondary column after a join

I have two tables which have a common key between them, and quite a lot of other important infos ; for the sake of simplicity i will be using Combination A and Combination B. When a combination is met, whichever table has the maximum number of records should be the source where i collect the information ; in this case say IDs. The priority when counts are same is Table1.
COMMONKEY column is the combination/join condition in my tables.
(Table 1)
SELECT '123' table1_id,'Comb A' commonkey from dual UNION
SELECT '124' table1_id,'Comb A' commonkey from dual UNION
SELECT '125' table1_id,'Comb A' commonkey from dual UNION
SELECT '126' table1_id,'Comb A' commonkey from dual UNION
SELECT '215' table1_id,'Comb B' commonkey from dual UNION
SELECT '216' table1_id,'Comb B' commonkey from dual UNION
SELECT '559' table1_id,'Random Combination 1' commonkey from dual UNION
SELECT '560' table1_id,'Random Combination 2' commonkey from dual ;
( Table 2 )
SELECT 'abc1' table2_id,'Comb A' commonkey from dual UNION
SELECT 'abc2' table2_id,'Comb A' commonkey from dual UNION
SELECT 'abc3' table2_id,'Comb A' commonkey from dual UNION
SELECT 'abc4' table2_id,'Comb A' commonkey from dual UNION
SELECT 'xyz1' table2_id,'Comb B' commonkey from dual UNION
SELECT 'xyz2' table2_id,'Comb B' commonkey from dual UNION
SELECT 'xyz3' table2_id,'Comb B' commonkey from dual UNION
SELECT 'xyz2' table2_id,'Comb B' commonkey from dual UNION
SELECT '416abc1' table2_id,'Random Combination 91' commonkey from dual UNION
SELECT '416abc2' table2_id,'Random Combination 92' commonkey from dual;
Result Set Expected :
ID COMMONKEY
123 Comb A
124 Comb A
125 Comb A
126 Comb A
xyz1 Comb B
xyz2 Comb B
xyz3 Comb B
559 Random Combination 1
560 Random Combination 1
416abc1 Random Combination 91
416abc2 Random Combination 92
Updated Image :
( the image shows a screenshot of the trail data in an excel; The Requirement and Strategy are color mapped to make it quickly understandable )
I need to generate the result set using SQL as follows :
When table1.commonkey = table2.commonkey hits, I need to-
If table1 has 10 IDs, table2 has 5 IDs -> Pick 10 IDs from table1.
If table1 has 15 IDs, table2 has 30 IDs -> Pick 30 IDs from table2.
If table1 has 4 IDs, table2 has 4 IDs -> Pick 4 IDs from table1.
( when equal, choose table1 IDs )
When no matches occur with the common key, prevent a cross join and add in the rowsets linearly to the result table.
Edit : I've initially gone on routes with
a left join b where b.key IS null ;
a full outer join b where b.key IS NULL or a.key is NULL ;
to achieve workarounds with A-B or B-A result sets but both these approaches were quite wrong. Gathering Delta sets or Exclusion sets didnt go well.
Here's one option; see comments within code
SQL> with
2 -- sample data
3 a (id, ckey) as
4 (select '123', 'ca' from dual union all
5 select '124', 'ca' from dual union all
6 select '125', 'ca' from dual union all
7 select '126', 'ca' from dual union all
8 select '215', 'cb' from dual union all
9 select '216', 'cb' from dual union all
10 select '551', 'r1' from dual union all
11 select '552', 'r2' from dual
12 ),
13 b (id, ckey) as
14 (select 'abc1', 'ca' from dual union all
15 select 'abc2', 'ca' from dual union all
16 select 'abc3', 'ca' from dual union all
17 select 'abc4', 'ca' from dual union all
18 select 'xyz1', 'cb' from dual union all
19 select 'xyz2', 'cb' from dual union all
20 select 'xyz3', 'cb' from dual union all
21 select '9991', 'r3' from dual union all
22 select '9992', 'r4' from dual
23 ),
24 -- count rows per each CKEY (common key)
25 tempa as
26 (select id, ckey, count(*) over (partition by ckey) cnt
27 from a
28 ),
29 tempb as
30 (select id, ckey, count(*) over (partition by ckey) cnt
31 from b
32 )
33 -- final query
34 select distinct
35 case when a.cnt >= b.cnt then a.id
36 else b.id
37 end id,
38 a.ckey
39 from tempa a join tempb b on b.ckey = a.ckey
40 union all
41 select ckey, id from a
42 where not exists (select null from b where a.ckey = b.ckey)
43 union all
44 select ckey, id from b
45 where not exists (select null from a where a.ckey = b.ckey)
46 order by 1, 2;
which results in
ID CKEY
---- -----
r1 551
r2 552
r3 9991
r4 9992
xyz1 cb
xyz2 cb
xyz3 cb
123 ca
124 ca
125 ca
126 ca
11 rows selected.
SQL>

Find rows with consecutive ones

I've two integer columns and need to display the rows with consecutive one's in the NUM column.
Sample data:
CREATE TABLE table_name ( ID, NUM ) AS
SELECT 1, 1 FROM DUAL UNION ALL
SELECT 2, 1 FROM DUAL UNION ALL
SELECT 3, 1 FROM DUAL UNION ALL
SELECT 4, 2 FROM DUAL UNION ALL
SELECT 5, 1 FROM DUAL UNION ALL
SELECT 6, 2 FROM DUAL UNION ALL
SELECT 7, 2 FROM DUAL;
Expected Output:
ID NUM
-- ---
1 1
2 1
3 1
I have tried using self-joins and achieved the result:
WITH TAB (ID, NUM) AS
(
SELECT 1, 1 FROM DUAL UNION ALL
SELECT 2, 1 FROM DUAL UNION ALL
SELECT 3, 1 FROM DUAL UNION ALL
SELECT 4, 2 FROM DUAL UNION ALL
SELECT 5, 1 FROM DUAL UNION ALL
SELECT 6, 2 FROM DUAL UNION ALL
SELECT 7, 2 FROM DUAL
)
SELECT DISTINCT
T.ID,
T.NUM
FROM
TAB T
JOIN (
SELECT
T1.ID ID1,
T2.ID ID2,
T1.NUM,
COUNT(1) OVER(
PARTITION BY T1.NUM
) RN
FROM
TAB T1
JOIN TAB T2 ON ( T1.NUM = T2.NUM
AND T1.ID = T2.ID + 1 )
) T_IN ON ( ( T.ID = T_IN.ID1
OR T.ID = T_IN.ID2 )
AND T.NUM = T_IN.NUM
AND RN >= 2 ) -- THIS CONDITION IS TO RESTRICT CONSECUTIVES LESS THAN 3
ORDER BY
1
output:
db<>fiddle demo
Use analytic functions LAG or LEAD:
Oracle Setup:
CREATE TABLE table_name ( ID, NUM ) AS
SELECT 1, 1 FROM DUAL UNION ALL
SELECT 2, 1 FROM DUAL UNION ALL
SELECT 3, 1 FROM DUAL UNION ALL
SELECT 4, 2 FROM DUAL UNION ALL
SELECT 5, 1 FROM DUAL UNION ALL
SELECT 6, 2 FROM DUAL UNION ALL
SELECT 7, 2 FROM DUAL;
Query:
SELECT id,num
FROM (
SELECT id,
num,
LAG( num ) OVER ( ORDER BY id ) AS prev_num,
LEAD( num ) OVER ( ORDER BY id ) AS next_num
FROM table_name
)
WHERE num = 1
AND ( num = prev_num
OR num = next_num )
Output:
ID | NUM
-: | --:
1 | 1
2 | 1
3 | 1
db<>fiddle here

Pivot from row to column - Oracle SQL

I have a table:
Table1
row_id var var_val
1 Test 1 123
1 Test 2 456
1 Test 3 789
1 Test 4 1234
2 Test 1 665t
2 Test 2 dsfs
2 Test 3 df
2 Test 4 sfd
3 Test 1 sfs
3 Test 2 sf
3 Test 3 sdfs
3 Test 4 sfsd
Here is the output:
Table2
row_id var1 var2
1 123 456
2 665t dsfs
3 sfs sf
For var1 - get value where var = "Test 1"
For var2 - get value where var = "Test 2"
Is there a way to use pivot or some way of extracting the variable for each row_id from the table1 as per above?
You can use conditional aggregation or a join:
select t11.row_id, t11.var, t12.var
from table1 t11 join
table1 t12
on t11.row_id = t12.row_id and
t11.var = 'Test 1' and
t12.var = 'Test 2'
Is there a way to use pivot...?
Sure:
select *
from table1
pivot (max(var_val) for var in ('Test 1' var1, 'Test 2' var2))
demo
You can use correlated subqueries and row_number() window analytic function together
with table1(row_id, var, var_val) as
(
select 1,'Test 1','123' from dual union all
select 1,'Test 2','456' from dual union all
select 1,'Test 3','789' from dual union all
select 1,'Test 4','1234' from dual union all
select 2,'Test 1','665t' from dual union all
select 2,'Test 2','dsfs' from dual union all
select 2,'Test 3','df' from dual union all
select 2,'Test 4','sfd' from dual union all
select 3,'Test 1','sfs' from dual union all
select 3,'Test 2','sf' from dual union all
select 3,'Test 3','sdfs' from dual union all
select 3,'Test 4','sfsd' from dual
), t2 as
(
select t.*, row_number() over (partition by var order by row_id) as rn
from table1 t
)
select distinct row_id,
(select var_val
from table1 t2
where t2.var = 'Test 1'
and t2.row_id = rn) as var1,
(select var_val
from table1 t2
where t2.var = 'Test 2'
and t2.row_id = rn) as var2
from t2
order by row_id
ROW_ID VAR1 VAR2
------ ---- ----
1 123 456
2 665t dsfs
3 sfs sf
Demo

I need the count of values

I have data in my table in oracle like below
A_CODE B_M P_id
------ ---- ------
123 A 1
123 A 2
123 B 5
678 B 3
678 C 3
678 B 4
123 BC 2
The value "BC" is B and C. The data is not normalized so we need to count it as B and C. I need the counts to be displayed as below per A_CODE
A_CODE B_M COUNT
------- ---- -------
123 A 2
123 B 2
123 C 1
678 B 2
678 C 1
How can i do this in Oracle?
You should use CONNECT BY and CONNECT_BY_ROOT.
I hope this helps:
SELECT A_CODE, B_M, COUNT (*)
FROM ( SELECT A_CODE, SUBSTR (CONNECT_BY_ROOT (B_M), LEVEL, 1) B_M
FROM your_table
CONNECT BY LEVEL <= LENGTH (B_M))
WHERE B_M IS NOT NULL
GROUP BY A_CODE, B_M
ORDER BY A_CODE;
Please try below:
SELECT A_CODE, B_M, COUNT(*) "COUNT" FROM
(SELECT A_CODE, B_M
FROM
(SELECT A_CODE,
SUBSTR(B_M,x.LVL,1) B_M
FROM my_table t,
(SELECT LEVEL LVL FROM dual
CONNECT BY LEVEL <=
(SELECT MAX(LENGTH(B_M)) FROM my_table)
) x
WHERE t.B_M is not null
)
WHERE B_M IS NOT NULL
UNION ALL
SELECT A_CODE, B_M FROM my_table WHERE B_M IS NULL
)
GROUP BY A_CODE,
B_M
ORDER BY A_CODE, B_M;
One option is to join this table to a data set that provides you with the normalised structure you need.
with cte_normaliser as (
select 'A' B_M, 'A' 'B_M_norm from dual union all
select 'B' B_M, 'B' 'B_M_norm from dual union all
select 'C' B_M, 'C' 'B_M_norm from dual union all
select 'BC' B_M, 'B' 'B_M_norm from dual union all
select 'BC' B_M, 'C' 'B_M_norm from dual)
select my_table.A_CODE,
n.B_M_norm,
count(*)
from my_table join
cte_normaliser n on n.B_M = my_table.B_M
group by my_table.A_CODE,
n.B_M_norm;
Using a fixed data set like that might not be feasible if you have a large number of variable code combination, though, and that data set might need to be built dynamically.
Besides having my_table, you create a table with all possible values. That is:
P_VAL
-----
A
B
C
Then, you should be able to obtain the count of occurrences with a join. Something like:
with my_table
as ( select '123' a_code, 'A' b_m, '1' p_id from dual
union select '123' a_code, 'A' b_m, '2' p_id from dual
union select '123' a_code, 'B' b_m, '5' p_id from dual
union select '678' a_code, 'B' b_m, '3' p_id from dual
union select '678' a_code, 'C' b_m, '3' p_id from dual
union select '678' a_code, 'B' b_m, '4' p_id from dual
union select '123' a_code, 'BC' b_m, '2' p_id from dual)
,possible_values
as ( select 'A' p_val from dual
union select 'B' p_val from dual
union select 'C' p_val from dual)
select a_code
,p_val b_m
,count('X') count
from my_table
join possible_values
on instr(b_m,p_val) > 0
group by a_code,p_val
order by a_code,p_val;

Find count value for a different group with reference with single column (Oracle)

I have the below scenario data. I need a count for column 'c1' with different set of data. total count should be based on unique no of data from column c1 and e1.
with t as
(
select 'cab1' as c1, 'ae1' as e1 from dual
union all
select 'cab1' , 'ae2' from dual
union all
select 'cab1' , 'ae3' from dual
union all
select 'cab1' , 'ae4' from dual
union all
select 'cab3' , 'ae1' from dual
union all
select 'cab3' , 'ae1' from dual
union all
select 'cab2' , 'ae' from dual
)
select
c1,e1, COUNT(*) OVER (partition by c1 order by c1,e1 ) as p1
from t;
my result should be
c1 e1 count
-----------------------
cab1 ae3 4
cab1 ae2 4
cab1 ae1 4
cab1 ae4 4
cab2 ae 1
cab3 ae1 1
Can anyone help on this.
SqlFiddleDemo
with t as
(
select 'cab1' as c1, 'ae1' as e1 from dual
union all
select 'cab1' , 'ae2' from dual
union all
select 'cab1' , 'ae3' from dual
union all
select 'cab1' , 'ae4' from dual
union all
select 'cab3' , 'ae1' from dual
union all
select 'cab3' , 'ae1' from dual
union all
select 'cab2' , 'ae' from dual
)
SELECT
c1,
e1,
COUNT(*) OVER (partition by c1) as p1
FROM t
GROUP BY c1, e1