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

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!

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

De-duplicating rows in a table with respect to certain columns and retaining the corresponding values in the other columns in HIVE

I need to create a temporary table in HIVE using an existing table that has 7 columns. I just want to get rid of duplicates with respect to first three columns and also retain the corresponding values in the other 4 columns. I don't care which row is actually dropped while de-duplicating using first three rows alone.
You could use something as below if you are not considered about ordering
create table table2 as
select col1, col2, col3,
,split(agg_col,"|")[0] as col4
,split(agg_col,"|")[1] as col5
,split(agg_col,"|")[2] as col6
,split(agg_col,"|")[3] as col7
from (Select col1, col2, col3,
max(concat(cast(col4 as string),"|",
cast(col5 as string),"|",
cast(col6 as string),"|",
cast(col7 as string))) as agg_col
from table1
group by col1,col2,col3 ) A;
Below is another approach, which gives much control over ordering but slower than above approach
create table table2 as
select col1, col2, col3,max(col4), max(col5), max(col6), max(col7)
from (Select col1, col2, col3,col4, col5, col6, col7,
rank() over ( partition by col1, col2, col3
order by col4 desc, col5 desc, col6 desc, col7 desc ) as col_rank
from table1 ) A
where A.col_rank = 1
GROUP BY col1, col2, col3;
rank() over(..) function returns more than one column with rank as '1' if order by columns are all equal. In our case if there are 2 columns with exact same values for all seven columns then there will be duplicates when we use filter as col_rank =1. These duplicates can be eleminated using max and group by clauses as written in above query.

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.

Select column in query based on other table

I have a table called A where records contains some column name of table B.
table A
Id, columnName
1 col1
2 col2
3 col3
table B
ID, col1, col2, col3, col4, col5
I want to select columns of B based on the value of table A.
Example
Select col1, col2, col3
from B
If the record number 3 in table A were deleted the sql statement will be.
Select col1, col2
from B
You need a join. A basic SQL construct.
http://www.w3schools.com/sql/sql_join_inner.asp

One row with multiple columns to two columns per row

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.