Conditionally select from one table or another table - sql

I have two tables
TableA Column1, Column2,Column3, Column4
TableB Column1, Column2,Column3, Column4
Query to modify:
Select Column2,Column3, Column4
From TableA
How would I modify the above query to select Column3 from TableB if the value of TableA.Column2 exists in TableB? It would also have to be the max value of TableB.Column1.
TableA
Column1 Column2 Column3 Column4
1 A zebra apple
2 C lion orange
3 R giraffe banana
TableB
Column1 Column2 Column3 Column4
1 Q snail rock
2 C frog stone
3 Z giraffe tree
4 C walrus limb
Result
Column2 Column3 Column4
A zebra apple
C walrus orange
R giraffe banana
It might be something like:
Select TableA.Column2,Case When Exists(Select TableB.Column3 From Table3
Where Table2.Column = TableB.Column2 ) Then Select Table2.Column3 Where [Table2.Column1 is max value], Column4
From TableA

Something like this below:
select
a.column2
, case when b.column2 is not null then max(b.column3) else a.column3 end
, a.column4
from
tableA a
left join tableB b
on a.column2 = b.column2
group by
a.column2
, a.column3
, a.column4
, b.column2
If this is a homework assignment, as the question seems, please study the code to understand why and how and it works.

Related

select * using Group By Oracle SQL

I have this tables
column1 column2 colum3 column4 colum5 column6
a b 1 2 0 x
a b 1 2 0 y
a b 3 0 0 z
I want to group the same record
my sql code looks like this but it gives the same result (select * from employee where column1 = 'a' and column2= 'b')
SELECT a.column1,a.column2,a.column3,a.column4,a.column5
FROM employee a,
(SELECT column3, column4,column5
FROM employee
WHERE column1 = 'a' and column2= 'b'
GROUP BY column3 colum4 column5) b
WHERE a.column3 = b.column3,
and a.column4 = b.column4,
and a.column5 = b.column5,
the result should be like this:
column1 column2 colum3 column4 colum5
a b 1 2 0
a b 3 0 0
Try the following with window function row_number(), here is the demo.
select
column1,
column2,
colum3,
column4,
colum5
from
(
select
column1,
column2,
colum3,
column4,
colum5,
row_number() over (partition by column1, column2, colum3, column4, colum5 order by column1) as rnk
from myTable
) val
where rnk = 1
Output:
*---------------------------------------*
| COLUMN1 COLUMN2 COLUM3 COLUMN4 COLUM5|
*---------------------------------------*
| a b 1 2 0 |
| a b 3 0 0 |
*---------------------------------------*
For this dataset, a simple distinct on the first five columns would return the result that you expect:
select distinct column1, column2, column3, colum4, column5
from employee

Can't figure out how to create a select statement from this scenario?

Can you please help formulate a select statement from the scenario below?
column1 column2 column3 column4 column5 column6
a b c alt1 alt2 alt3
aa bb cc alt1 alt2
aaa bbb ccc alt1
if column6 !=Null then results should give me 3 rows of data
abcalt3
abcalt2
abcalt1
if column6 = Null and column5 !=Null then results should give me 2 rows of data
aabbccalt2
aabbccalt1
if column6 = Null and column5 = Null and column4 != Null then results should give me 1 row of data
aaabbbcccalt1
You can try with UNION ALL with separate select statement for different conditions as below-
DEMO HERE
SELECT column1+column2+column3+column4
FROM your_table where column6 is not null
UNION ALL
SELECT column1+column2+column3+column5
FROM your_table where column6 is not null
UNION ALL
SELECT column1+column2+column3+column6
FROM your_table where column6 is not null
UNION ALL
SELECT column1+column2+column3+column4
FROM your_table where column6 is null AND column5 IS NOT NULL
UNION ALL
SELECT column1+column2+column3+column5
FROM your_table where column6 is null AND column5 IS NOT NULL
UNION ALL
SELECT column1+column2+column3+column4
FROM your_table where column6 is null AND column5 IS NULL AND column4 IS NOT NULL

How to select value A or both A and B but not B in a column?

I have a tableA:
column1 column2
name1 A
name1 B
In column1, name1 can have 2 values in column2: A or B.
I want to select like this:
select *
from tableA
where ??
The condition is when column2 is equal to A or both A and B, but not B alone. Is this possible?
What I expect in the condition of the query:
column1 column2
name1 A
Or
column1 column2
name1 A
name1 B
What I do not want:
column1 column2
name1 B
Your specification is:
select t.*
from t
where t.column2 = 'A' or
exists (select 1 from t t2 where t2.column1 = t.column1 and t2.column2 = 'A');
This can actually be simplified to:
select t.*
from t
where exists (select 1 from t t2 where t2.column1 = t.column1 and t2.column2 = 'A');
If you just want the names with this property, then:
select distinct t.column
from t
where t.column1 = 'A';
You can use exists
select *
from tableA a
where exists ( select 1 from tableA where column1 = a.column1 and column2 = 'A' );
For current case, even without adding column1 = a.column1 and part, you can get the desired results.
Demo

Result of one query into another query

I have two tables
TABLE_A with columnname
COLUMN1 COLUMN2 COLUMN3 COLUMN4 COLUMN5 and which has data
abc def ghi jkl mno
123 456 789 001 121
TABLE_B with columnname
COLUMN6 COLUMN7 which has data as
COLUMN5 124
COLUMN4 bca
COLUMN3 aaa
COLUMN5 bbb
So I have the columnname of Table_A as a data in the table_B
so I want to do something like this in a single query
$query1= select COLUMN6 from TABLE_B where COLUMN7='aaa';
$query2= select $query1 from TABLE_A where COLUMN1='123';
Thanks
You need to have a value to match from table a -> to table B
If it looks like this:
TableA -> id, name
TableB -> id, table_a_id, name
This query will work:
SELECT a.name, b.name
FROM tableA as a
JOIN tableB as b ON a.id=b.table_a_id AND b.name='123'
WHERE a.name='aaaa'
To get the names of table A and B. I am using an alias for the table names here to make it easier to read. I hope, with this example, this will answer your question.
If you don't have any matching values, but you want to have all columns crossed, you can do this:
SELECT a.name, b.name
FROM tableA a, tableB b
WHERE a.name='aaa' AND b.name='123'
You may use CASE or DECODE to do that:
select a.* from tableA a, tableB b
WHERE
b.column7 = 'aaa'
and case
when b.column6 = 'COLUMN1' then a.column1
when b.column6 = 'COLUMN2' then a.column2
when b.column6 = 'COLUMN3' then a.column3
when b.column6 = 'COLUMN4' then a.column4
when b.column6 = 'COLUMN5' then a.column5
else null end = '123' -- condition for tableA
You can make this statement up to 1000 columns (ORACLE hard limit) :)

Select query with names columns

I want have a query on the table values
Column1 Column2 Column3
-----------------------
a b c
d e f
Result should be
Column1 a
Column2 b
Column3 c
Column1 d
Column2 e
Column3 f
Basically a key value pairs. Is it possible in Oracle?
Thanks for the help in advance
You can do it with UNION ALL, like this:
SELECT 'Column1' as Name, Column1 as Value FROM my_table
UNION ALL
SELECT 'Column2' as Name, Column2 as Value FROM my_table
UNION ALL
SELECT 'Column3' as Name, Column3 as Value FROM my_table
Here is a demo on sqlfiddle.