Getting exception while executing the query to compare two values - sql

While executing a query to compare the rates of two columns and display if one is grater than other I am getting ORA-01427 exception
Here is the data set. This is mocked up data . Data size is huge in databse
Table1
col1 col2 col3 col4 col5 col6
c1 c1test 85 85 I 5
c2 c2test 85 85 I 3
c3 c3test 85 85 E 6
c4 c4test G1 G1 E 7
c5 c5test G1 G1 E 5
c6 c6test G1 G1 E 8
c7 c7test G1 G1 I 3
c8 c8test G1 G1 G 7
Table2
col1 col2 col3 col4 col5
85 85 D I 3
85 85 D E 5
G1 G1 D E 5
G1 G1 D I 3
G1 G1 D G 5
G1 G1 E I 2
G1 G1 E E 2
85 85 E I 3
Expected result
We need to compare the value of col5 of table2 with the col6 of table1 to find out the values greater in col6 of table1 and display the records. Comparison needs to be done only for col3 equals D values.
col1 col2 col3 col4 col5 col6
c1 c1test 81 81 I 5
c3 c3test 81 81 E 6
c4 c4test G1 G1 E 7
c6 c6test G1 G1 E 8
c8 c8test G1 G1 G 7
I am using the below query
Select * from table1 where
col6 > (select col5 from
table2 where col3='D'
and col1=table1.col3
and col2=table1.col4
and col4=table1.col5
This throws an ora-01427 exception. Can you pls hel to get the expected output.

If you are looking for value grater than any value of table2.col5, try this
SELECT t1.*
FROM table1 t1
WHERE t1.col6 > ANY (SELECT t2.col5
FROM table2 t2
WHERE t2.col3 = 'D'
AND t2.col1 = t1.col3
AND t2.col2 = t1.col4
AND t2.col4 = t1.col5
);
If you need table1.col6 should be greater than all values found in table2.col5 use ALL instead of ANY

Your subquery is returning multiple rows. You can use min() or max() to get around this. I'm not sure which logic you really want:
Select t1.*
from table1 t1
where t1.col6 > (select max(t2.col5)
from table2 t2
where t2.col3 = 'D' and
t2.col1 = t1..col3 and
t2.col2 = t1.col4 and
t2.col4 = t1.col5
);

Related

select a value from different table based on conditions in sql

I'm trying to select a value from a different table based on current table values and condition
Table 1:
C1
C2
C3
1
2
3
1
4
5
1
6
6
2
3
3
Table 2:
D1
D2
D3
D4
1
2
3
Value1
1
4
5
Value2
1
6
8
Value3
2
3
4
Value4
2
Value5
And Im trying to get the below expected result table a single line sql
Results:
C1
C2
C3
D4
1
2
3
Value1
1
4
5
Value2
1
6
6
2
3
3
Value5
The condition is to pick D4 value only
(C1=D1 and C2=D2 and C3=D3) matches then D4 or when C1=D1 matches then D4 else null for all
I tried inner join and also case statement but no success
Here is the fiddle i had created
This looks like a left join:
select t1.*, t2.d4
from table1 t1 left join
table2 t2
on t1.C1 = t2.D1 and
(t1.C2 = t2.D2 or t2.D2 is null) and
(t1.C3 = t2.D3 or t2.D3 is null);
Note: It is a little hard to tell from the explanation if you need for both D2 and D3 to be NULL. If so:
select t1.*, t2.d4
from table1 t1 left join
table2 t2
on t1.C1 = t2.D1 and
(t1.C2 = t2.D2 and t1.C3 = t2.D3 or
t2.D2 is null and t2.D3 is null
);
Here is the fiddle.

expanding a row by splitting it into existing columns

I have read tables from pdf using tabula-py command with the following code:
table = tabula.read_pdf(files[0],pages = 'all',multiple_tables = True, stream = True)
Sometimes values from two columns are joined into a single column(separated by single space). For example:
col0
col1
col2
col3
col4
col5
col6
col7
a1
b1 c1
d1
e1 f1
g1
h1
NA
NA
a2
b2
c2
d2
e2
f2
g2
h2
How can i readjust the values into the correct columns, to get:
col0
col1
col2
col3
col4
col5
col6
col7
a1
b1
c1
d1
e1
f1
g1
h1
a2
b2
c2
d2
e2
f2
g2
h2
output as space delimited
replace quoted strings from step 1
read back as space delimited
import io
df = pd.read_csv(io.StringIO("""col0 col1 col2 col3 col4 col5 col6 col7
a1 b1 c1 d1 e1 f1 g1 h1 NA NA
a2 b2 c2 d2 e2 f2 g2 h2"""), sep="\t")
df = pd.read_csv(io.StringIO(df.to_csv(sep=" ").replace("\"", "")), sep="\s+")
output
col0 col1 col2 col3 col4 col5 col6 col7
a1 b1 c1 d1 e1 f1 g1 h1
a2 b2 c2 d2 e2 f2 g2 h2
Could you try
table = tabula.read_pdf(files[0],pages = 'all',multiple_tables = True,guess = False, stream = True)

All rows of First N items of a group of data in dataset based on another column in pandas

Let's consider I have this dataset:
name comp item type
A c1 item21 t1
A c1 item231 t1
A c1 item3 t1
B c3 item23 t1
B c3 item1 t1
B c3 p3251 t1
C c4 item1 t1
C c4 p32sd t1
C c4 item512 t1
D c5 item242 t2
D c5 item1 t2
F c6 item4 t2
F c6 item24 t2
H c7 item4125 t2
H c7 item3 t2
H c7 item14 t2
K c8 item1 t2
K c8 p3223 t2
I want to select all items of first n [names,comp] of each type:
For example all items of first 2 names-comp of each type the expected df would be:
name comp item type
A c1 item21 t1
A c1 item231 t1
A c1 item3 t1
B c3 item23 t1
B c3 item1 t1
B c3 p3251 t1
D c5 item242 t2
D c5 item1 t2
F c6 item4 t2
F c6 item24 t2
Does anybody have any idea how to do this?
Try this:
cols = ['type', 'name', 'comp']
# The first 2 name-comp of each type
tmp = df[cols].drop_duplicates().groupby('type').head(2)
# All rows that match the criteria
result = tmp.merge(df, left_on=cols, right_on=cols)
If you want no intermediary data frame:
df[cols].drop_duplicates().groupby('type').head(2).merge(df, left_on=cols, right_on=cols)

Update one table from another table with duplicate keys

I am trying to merge data from one table into another.
Table 1 (Tab1)
ID col2 col3 col_to_update
1 s1 a1 null
2 s1 a2 null
3 s1 a2 null
4 s2 a1 null
5 s3 a1 null
6 s4 a1 null
Table 2 (Tab2)
ID col2 col3 col4
10 s1 a1 v1
11 s1 a1 v2
12 s1 a2 v3
13 s2 a1 v4
14 s3 a1 v5
15 s4 a1 v6
16 s4 a1 v7
I am trying to map column col4 from table Tab2 into column col_to_update in table Tab1 based on matching Tab1.col2 = Tab2.col2 and Tabl.col3 = Tab2.col3 to get below expected output:
Expected Output
ID col2 col3 col4
1 s1 a1 v1
2 s1 a2 v3
3 s1 a2 v3
4 s2 a1 v4
5 s3 a1 v5
6 s4 a1 v6
I tried unsuccessfully with below query:
MERGE INTO Tab1 x1
USING
(
SELECT t1.id as t1id, t2.id as t2id, t2.col2 t2col2, t2.col3 t2col3, t2.col4 from Tab2 t2
JOIN Tab1 t1 ON t2.col2 = t1.col2 AND t2.col3 = t1.col3
) x2
ON (x1.id = x2.t1id)
WHEN MATCHED THEN UPDATE SET
x1.col_to_update = x2.col4;
Is there a way to get the expected output.
You simply want to update tab1:
update tab1
set col_to_update =
(
select min(tab2.col4) -- or whichever value you want to use
from tab2
where tab2.col2 = tab1.col2
and tab2.col3 = tab1.col3
);

Oracle SQL - retrive data by GROUP BY and then derive the data based on the group by result

I have a table with three columns, Col1, Col2 and Col3 where Col2 is primary key.
Below is the SOURCE table data as
Col1 Col2 Col3
G1 S1 C
G1 S2 Y
G1 S3 U
G2 S4 C
G2 S5 Y
G3 S6 C
G3 S7 C
G4 S8 Y
G4 S9 Y
G5 S10 U
G5 S11 U
G6 S12 C
G6 S13 U
G7 S14 Y
G7 S15 U
Expected TARGET table will ahve only Col1 and Col3 and data should be
Col1 Col3
G1 U
G2 B
G3 C
G4 Y
G5 U
G6 U
G7 U
Logic used is:
1) For a given Col1 record, if all the col2 records have same value in Col3 then return one record for all the col2 records ( Examples G3, G4, G5)
2) For a given Col1 record, if all the col2 records having either 'C' or 'Y then return 'B' (Example G2)
3) For all the remaining combinations return 'U' for Col3
Any one on the group please advice me on how to write query to get the data in target table
Thank you
Use a CASE construct for this. For the 'C' and 'Y' thing you'd have to count conditionally (i.e. also with CASE).
select
col1,
case
when min(col3) = max(col3) then min(col3)
when count(case when col3 not in ('C','Y') then 1 end) = 0 then 'B'
else 'U'
end as col3
from mytable
group by col1;