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) :)
Related
I have two tables where I need to do a FULL JOIN. Table2 has many missing values that can be found in table1.
I need to combine (if column1, 2 and 3 are the same) and append the information from table2 filling the null values from table1.
TABLE1
Column1
Column2
Column3
measure1
measure2
A
B
DAY1
50
null
A
B
DAY2
10
null
TABLE 2
Column1
Column2
Column3
measure1
measure2
A
B
DAY1
null
100
A
null
DAY3
null
300
DESIRED RESULT
Column1
Column2
Column3
measure1
measure2
A
B
DAY1
50
100
A
B
DAY2
10
null
A
B
DAY3
null
300
In this case, I combined first rows from table1 and table2. For second row in table2 we lookup the value of column2 from table1.1.
You can use FULL OUTER JOIN here:
select
coalesce(t1.id, t2.id) id,
coalesce(t1.c1, t2.c1) c1,
coalesce(t1.c2, t2.c2) c2
from t1
full outer join t2 on t1.id = t2.id;
SQLize fiddle
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
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.
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.
I have a question related to select query. here I am explaining down below.
I have a table with the following data
**Column1(Primary Key) Column2 Column3**
------ --------- --------------
1 C
2 C
3 Null
4 H
5 L
6 H
my problem is I have to replace the value of Column3 with the corresponding value of Column1 for every occurrence of data "C", "H" and "L". Please provide me query related to this problem. How can I solve this using query or stored procedure. Please elaborate the same.
I need final select query result as follows
**Column1(Primary Key) Column2 Column3**
------ --------- --------------
1 C 1
2 C 2
3 Null
4 H 4
5 L 5
6 H 6
Select Column1, Column2,
CASE
WHEN Column2 is not null THEN Column1
ELSE null --or whatever value you want for blank
END as Column3
From TableName t1
Alternatively you could it it like this:
Select Column1, Column2,
CASE
WHEN Column2 = 'L' or Column2 = 'H' or Column2 = 'C' THEN Column1
ELSE null --or whatever value you want for blank
END as Column3
From TableName t1
Do you mean UPDATE?
UPDATE tbl SET Column3 = Column1 WHERE Column2 IN ('C', 'H', 'L', 'F')
Or for all;
UPDATE tbl SET Column3 = Column1 WHERE Column2 IS NOT NULL
UPDATE mytable
SET Column3 = Column1
WHERE Column2 in ('C', 'H', 'L')
like this?
select Column1, Column2, Column1 as Column3 from table
Or I'm not sure what you are actually asking for ..?
Ah, sorry. Dind't read all of the question there ...
Most SQL dialects have a way to do conditional subselects, but they are different in differend dialects. So which SQL are you using?