Exclude rows distinct except for null values - sql

I'm trying to write a query that will return distinct rows while excluding rows that don't have maximum data.
table1
col1 col2 col3 col4 col5
one a b c d
two a b d
three a b c
four a c d
five a b
six a c
seven a e
Basically, I want a query that will return the following from the table above
col1 col2 col3 col4 col5
one a b c d
six a c
seven a e

Related

SQL - Add ID column for rows if at least one column have the same value

I have a table with three columna: col1, col2, col3. I want to create a unique ID if at least ONE of the columns have the same value. For example, if col1 equals A in two instances, regardless of the values of col2 and col3, the id should be the same. Same goes for the other cols, so if col2 equals B, the identifier should be the same regardless of the values of col1 or col2.
This is the expected result.
ID
col1
col2
col3
1
A
F
G
1
A
T
Y
2
B
E
U
2
T
E
O
3
H
Y
U
3
H
B
L
3
P
B
P
I've tried using the Dense Rank function but it considers the repeated values in all columns.

Pandas is condition on multiple columns

I have a dataframe
col1 col2 col3 col4
A F F F
B F A B
C B A C
D S A F
I want to say if A and F in any of these columns then make a new column and enter "Found"
col1 col2 col3 col4 output
A F F F Found
B F A B Found
C B A C 0
D S A F Found
Use :
df['output']=np.where(df.eq('A').any(1) & df.eq('F').any(1),'Found',0)
Another approach:
df['output']=(df.eq('A').any(1) & df.eq('F').any(1)).map({True:'Found',False:0})
Output:
col1 col2 col3 col4 output
0 A F F F Found
1 B F A B Found
2 C B A C 0
3 D S A F Found
Try this:
df.loc[df.apply(lambda x: ((x=='F').any() & (x=='A').any()).any(),axis=1), 'output'] = 'Found'
df.fillna(0)
You can use pd.DataFrame.where():
df.where(lambda x: (x=='A') | (x=='F')).dropna(thresh=1)

Conditional Join in Oracle SQL

Consider below 3 tables.
Table a
Col a Col b Col c
1 000 Actual data
1 001 Actual data
2 000 Actual data
3 000 Actual data
3 001 Actual data
3 002 Actual data
Table b
Col a Col b Col d
1 000 Actual data
1 001 Actual data
2 000 Actual data
Table c
Col a Col b Col d
3 000 Actual data
3 001 Actual data
3 002 Actual data
Table a is parent table and table b and c are child table having col a & b common among 3 and needs to be joined.
Now Join should be such if data is not found in table b then only it should be searched in table c
Desired:
cola col b col c col d
1 000 somedata moredata
1 001 somedata moredata
2 000 somedata moredata
3 000 somedata moredata
3 001 somedata moredata
3 002 somedata moredata
Well, currently what i am doing is, left join b to a and c to a, but i think every time for record in a will be searched in b and c both making it Less cost effective. hence want to make it cost effective/fine-tune such that if records NOT exist in b then only search c.
What you really need is a way to "collect" all the rows from table B, and if there are none, then all the rows from table C. Doing the join to A is then standard.
Something like this should work. Make it a subquery and join to your first table.
select col_a, col_b, col_c
from table_b
union all
select col_a, col_b, col_c
from table_c
where (select count(*) from table_b) = 0
If table_b has at least one row, then nothing will be selected from table_c (because the where condition will be false for all rows in table_c). However, if table_b is empty, all the rows from table_c will be selected.
What you need to do is first create a union of two tables B and C with only those records where are in B and C but if they are in B then we should ignore the C ones then do a join with Table A. Thus:
SELECT B.cola, B.colb from B
UNION ALL
SELECT C.cola, C.colb from C
Now using this table, you can join with Table A like:
SELECT A.cola, A.colb, tmp.colc
FROM A
JOIN
( SELECT B.cola, B.colb, B.colc from B
UNION ALL
SELECT C.cola, C.colb from C) AS tmp
ON A.cola = tmp.cola
AND A.colb = tmp.colb
Two left joins:
select a.*, b.*, c.*
from a
left join b
on a.cola=b.cola
and a.colb = b.colb
left join c
on a.cola=c.cola
and a.colb=c.colb

Merge data from two tables into single column of another table

How to merge data from multiple tables into single column of another table.
Example:
Table A
Col1 | Col2 | Col3
10
20
Table B
Col1 | Col2 | Col3
13
99
I want my o/p in Table C in Col1 as
Col1
10
20
13
99
I did (part of query)
Select Col1 from A
Union
Select Col1 from B
but it is not giving me this desired result
The SELECT appears correct (you may want to use UNION ALL instead of UNION to avoid elimination of duplicates).
If you want the results to be in the third table C, you need to make an INSERT from your SELECT, like this:
INSERT INTO C (Col1)
(
SELECT Col1 from A
UNION ALL
SELECT Col1 from B
)

write a sub select query

I have two tables with one to many relationship. I want to write a query which outputs all records from table with one record and only one record from the table having many records.
So the table having many records with first show the most occurring record. If there are equal occurrences then it will Order by ascending and show the first record.
Table1
Col1 Col2 Col3
a1 1 4
a2 2 5
a3 3 6
Table2
Col1 Col4
a1 10
a1 11
a1 22
a1 11
a2 10
a2 11
a3 19
a3 22
a3 22
a3 23
Query output:
Col1 Col2 Col3 Col4
a1 1 4 11
a2 2 5 10
a3 3 6 22
Hope I made it clear.
First you need to use a group by along with a min() to get the smallest number from table2, then you join to table1 to get the columns you need. I've used a left join as I'm assuming there may not be a match in table2 but you can change it to an INNER JOIN if there are always 1 or more corresponding records in table2.
SELECT a.col1, a.col2, a.col3, b.col4
FROM table1 a
LEFT JOIN (
SELECT col1, col4 = MIN(col4)
FROM table2
GROUP BY col1
) b
ON a.col1 = b.col1