SQL - Transpose one row into two columns - sql

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;

Related

Show 2 columns in 2 different rows - SQL

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 <> ''

update same rows

I have a table like below:
Current
Column1 Column2 column3
-------------------------
abc cat1 1
efg cat1 3
hij cat1 2
klm cat2 1
nop car2 2
qrs cat2 3
I want to update column1 where all rows matching cat1 from rows matching cat2 , considering rows will be updated for id which matches column 3 ids
Expected
Column1 Column2 column3
-------------------------
klm cat1 1
qrs cat1 3
nop cat1 2
klm cat2 1
nop car2 2
qrs cat2 3
Use below query :
CREATE TABLE #Table (Column1 VARCHAR(100),Column2 VARCHAR(100), Column3
VARCHAR(100))
INSERT INTO #Table ( Column1 , Column2 , Column3 )
SELECT 'abc','cat1',1 UNION ALL
SELECT 'efg','cat1',3 UNION ALL
SELECT 'hij','cat1',2 UNION ALL
SELECT 'klm','cat2',1 UNION ALL
SELECT 'nop','cat2',2 UNION ALL
SELECT 'qrs','cat2',3
UPDATE #Table SET Column1 = A.Column1
FROM
(
SELECT Column1 , Column3
FROM #Table
WHERE Column2 = 'cat2'
)A WHERE #Table.Column2 = 'cat1' AND A.Column3 = #Table.Column3
SELECT * FROM #Table
http://rextester.com/QYHLTE44670
Please try with below code:
DECLARE #Table TABLE
(Column1 VARCHAR(100),Column2 VARCHAR(100), Column3 INT)
INSERT INTO #Table VALUES
('abc','cat1',1),
('efg','cat1',3),
('hij','cat1',2),
('klm','cat2',1),
('nop','cat2',2),
('qrs','cat2',3)
UPDATE
t
SET t.Column1 = d.Column1
FROM #Table t INNER JOIN #Table d
ON t.Column3 = d.Column3
WHERE d.Column2 ='cat2'

Inserting multiple rows with column into single row

I am trying to write a query to copy set of rows from one table to another table in this format
Table 1
ColumnName ColumnValue RowId
Column1 Value1 1
Column2 Value2 1
Column3 Value3 1
Column1 Value4 2
Column2 Value5 2
Column3 Value6 2
Column1 Value7 3
Column2 Value8 3
Column3 Value9 3
Table2
Column1 Column2 Column3
Value1 Value2 Value3
Value4 Value5 Value6
Value7 Value8 Value9
Here basically table 1 is input and table 2 is the output which I am trying. I used the pivot and row number but none worked.
What will be the insert query to achieve this. Here all rows of rowId 1 from table 1 will form form one row of table2.
select rowID,
max(case when columnName = 'Column1' then value else null end) as column1,
max(case when columnName = 'Column2' then value else null end) as column2,
max(case when columnName = 'Column3' then value else null end) as column3
from table1
group by rowID

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.

Iterating result of Select Query

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?