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.
Related
I need to separate two columns into two rows in sql
I have this:
Column1 Column2 Column3
Car 2 5
Boat 4
Truck 6
And I want this:
Column1 Column2
Car 2
Car 5
Boat 4
Truck 6
How can I do this in SQL?
This operation is unpivoting. I would recommend apply:
select t.column1, v.col
from t cross apply
(values (col1), (col2)) v(col)
where v.col is not null;
This should do it
select Column1, Column2
from tbl where Column2 is not null and Column2 <> ''
union
select Column1, Column3
from tbl where Column3 is not null and Column3 <> ''
I have a table like:
column1 column2 column3 column4
A B C D
wants to convert it as:
Column
A
B
C
D
Without using this union all in oracle:
select column1 as Column from dual
union all
select column2 as Column from dual
union all
select column3 as Column from dual
union all
select column4 as Column from dual
You can try to use unpivot
select v
from (
SELECT 'A' column1,'B' column2,'C' column3,'D' column4 from dual
) t
unpivot
(
v for val in (column1,column2,column3,column4)
) u;
sqlfiddle:https://dbfiddle.uk/?rdbms=oracle_11.2&fiddle=b2105e1010b332ce5b702eba7bfa7f2d
Mine SQL Query returns a few columns but only one row. I need to transpose this one row and column's names into two columns - first with column's names and second with row values. For some reasons I can't use crosstab.
Operation that I want to perform is shown below.
Before:
Column1 | Column2 | Column3 | Column4
---------+---------+---------+--------
Value1 | Value2 | Value3 | Value4
And I want to get this:
Cols | Vals
--------+---------
Column1 | Value1
Column2 | Value2
Column3 | Value3
Column4 | Value4
Does this do what you want?
SELECT
'Column1' AS Cols
, Column1 AS Vals
FROM Table
UNION ALL
SELECT
'Column2' AS Cols
, Column2 AS Vals
FROM Table
UNION ALL
SELECT
'Column3' AS Cols
, Column3 AS Vals
FROM Table
UNION ALL
SELECT
'Column4' AS Cols
, Column4 AS Vals
FROM Table
select 'Column1' as Cols, Column1 as Vals from YourTable
union all
select 'Column2', Column2 from YourTable
union all
select 'Column3', Column3 from YourTable
union all
select 'Column4', Column4 from YourTable
Try crosstab from the tablefunc extension. http://www.postgresql.org/docs/9.0/static/tablefunc.html
It should do exactly what you want.
You can install the extension like this:
CREATE EXTENSION tablefunc;
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) :)
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?