One row with multiple columns to two columns per row - sql

I am having trouble creating a SQL select in Oracle, which does the following.
I have a
table (col1, col2 ... col15)
with 15 columns per row.
How can I make query as follows:
row1: col1, col2
row2: col3, col4
row3: col5, col6
...
rowN: col14, col15

Assuming all columns are of the same datatype, you could do a union. It will be tedious.
SELECT COL1, COL2 FROM TABLE_1
UNION ALL
SELECT COL3, COL4 FROM TABLE_1
UNION ALL
......
SELECT COL13, COL14 FROM TABLE_1
If you tell me your a little more about the problem maybe it can be solved in some way other than a plain query?
For example - if you are using another programing language perl or php, you can select row wise and output 8 lines per row.
Or, if its pure database, you can consider PL/SQL.

Related

Get one row per unique column value combination (`DISTINCT ON` operation without using it)

I have a table with 5 columns, For each unique combination of the first three columns, I want a single sample row. I don't care which values are considered for columns 4 and 5, as long as they come from the same row (they should be aligned).
I realise I can do a DISTINCT to fetch on the first three columns to fetch unique combinations of the first three columns. But the problems is then I cannot get 4th and 5th column values.
I considered GROUP BY, I can do a group by on the first three columns, then I can do a MIN(col4), MIN(col5). But there is no guarantee that values of col4 and col5 are aligned (from the same row).
The DB I am using does not support DISTINCT ON operation, which I realise is what I really need.
How do I perform what DISTINCT ON does without actually using that operation ?
I am guessing this is how I would write the SQL if DISTINCT ON was supported:
SELECT
DISTINCT ON (col1, col2, col3)
col1, col2, col3, col4, col5
FROM TABLE_NAME
select
col1, col2, col3, col4, col5
from (
select col1, col2, col3, col4, col5,
row_number() over (partition by col1, col2, col3) as n
from table_name
)
where n = 1

Get everything as seperate column after and before special character in SQL

I have the values in one the column
"A","1","Created","DO",....
"B","2","Update","Cancel",....
And want the results like each in seperate column
A as col1, 1 as col2, Created as col3
And B as col1, 2 as col2, Update as col3 like this
select substr(data, instr(substr(data, 6),'","')+8,1)
Can someone help me
If youre database is mysql you can use this:
SELECT
REPLACE(SUBSTRING_INDEX((SUBSTRING_INDEX(yourColumn,',',1)),',',-1),'"','') AS col1,
REPLACE(SUBSTRING_INDEX((SUBSTRING_INDEX(yourColumn,',',2)),',',-1),'"','') AS col2,
REPLACE(SUBSTRING_INDEX((SUBSTRING_INDEX(yourColumn,',',3)),',',-1),'"','') AS col3
FROM youreTable

Oracle SQL: How to convert one column of Select to rows

I am new to Oracle and am looking for a way to convert 1 column in a Select to rows.
My first approach was using Listagg which does exactly what I want but the character limit for this is not enough for my case.
As an alternative I would like to do the following.
SELECT
t.col1
, t.col2
, t.col3
, t.col4
, t.col5
FROM
my table t
Instead of the standard output of t.col1 t.col2 t.col3 t.col4 t.col5 I would like t.col2 to appear in rows (i.e. below each other) instead of in columns (next to each other). Col2 always contains a value and each of them should appear in a separate row.
When searching for a solution to this I came across Unpivot and Decode but am not sure if and how this could be applied here.
Can someone tell me how this can be achieved ?
Many thanks in advance for any help,
Mike
A simple method -- if your data is not too large -- is just to use union all. Your description makes it sound like you want this:
select col1, col2, col5
from t
where col2 is not null
union all
select col1, col3, col5
from t
where col2 is not null
union all
select col1, col4, col5
from t
where col2 is not null;
Hmmm, or if you just want the distinct values in col2:
select distinct col2
from t;
You are looking for the UNPIVOT function
SELECT col
FROM my table t
UNPIVOT INCLUDE NULLS (col FOR source_column_name IN (col1, col2, col3, col4, col5)
COL
----
Value 1
Value 2
Value 3
Value 4
Value 5
The result contains five rows with one column COL each that contains the value from the columns COL1 to COL5. The (unused) column SOURCE_COLUMN_NAME contains the name of the column where the data is coming from. You can remove the INCLUDING NULLS if you are only interested in rows the COL IS NOT NULL.
See the ORACLE-BASE article on PIVOT and UNPIVOT operators for more details.

Oracle SQL - Join 2 table columns in 1 row

I have 2 SQL's and the result come fine. They are no relation between those 2 queries but I want to see all the rows in single column.
e.g.
Select col1,col2,sum(col3) as col3 from table a
select col4,col5 from table b
I would like the result to be
col1 col2 col3 col4 col5
If there is no equivalent row for either table a or table b replace with zeroes.
Could some one help me with this. thanks.
Since, you didn't provided any information like table structure or data inside each tables. You can cross join both tables.
select t.col1,t.col2,t.col3,t1.col1,t1.col2 from tab1 t,tab2 t1;
SQLFiddle
In both select statements add column based on rownum or row_number() and then full join results using this column:
select nvl(col1, 0) col1, nvl(col2, 0) col2, nvl(col3, 0) col3,
nvl(col4, 0) col4, nvl(col5, 0) col5
from
(select rownum rn, col1, col2, col3 from (
select col1, col2, sum(col3) col3 from tableA group by col1, col2)) a
full join (select rownum rn, col4, col5 from tableB) b using (rn)
SQLFiddle demo
I guess a UNION could be a pragmatic solution since the 2 queries are not related. They are just 2 data sets that should be retrieved in one statement:
Select col1,col2,sum(col3) as col3 from table a
UNION
select col4,col5, to_number(null) col6 from table b
Be aware of col6 in the example. SQL insists on retrieving an equal set of columns in a UNION statement. It is a good practice to retrieve columns with exactly the same datatype. Since the sum(col3) will yield a number datatype column, col6 should too.
The outcome of col4 and col5 will be shown in col1 and col2.

how to get data from two table having different no. of column

i have two tables table1 and table2. Table1 has only 1 row of data and 7 column.
for ex.
col1 col2 col3 col4 col5 col6 col7 having data such as
(123 , abc , dfg, ed , " " , sc , fgh)
table2 has 6 column .
i want to join these two table table on such a way that finla output should have 1st row having data of
table1 and from 2nd row data of 2nd table.
NO commom column is there between this two table
plz tell me how to write query for this.
Do you really need them on 2 separate rows?
In that case, you can use the UNION as follows,
Select col1, col2, col3, col4, col5, col6, col7
from table1
UNION
Select col1, col2, col3, col4, col5, col6, ''
from table2
That should return the two records in two different rows, with a null column for the table2 since it consists of one column less that table1.
However, you have to ensure that the column types are similar in both tables for it to match!