Combine two tables in SQL server 2008 - sql

I need to ask something is there any way combine two tables different count of columns like:
Select a,b,c, from x
union
Select d, e from y

you need to do something like this
Select a,b,c from x
union all -- ALL doesn't filter dups and is faster
Select d, e, '' as f from y
I used '' but you might want to use NULL or 0, NULL will be compatible will all data types, '' will not be
I also used UNION ALL not UNION since it will perform better because it doesn't have to do a sort operation to get rid of dups
the alias f is not needed here either because the top query determines the name of the columns in the resultset

Note that
select a, b, c from x
union
select d, e, '' as f from y;
and
select d, e, '' as f from y
union
select a, b, c from x;
will yield different results i.e. the attribute names from the first-appearing table will be used.
Perhaps better to be unequivocal by explicitly renaming the columns in the second table e.g.
select a, b, c from x
union
select d AS a, e AS b, '' as c from y;

select col1, col2, col3, col4
from mytable1
union all
select col5, col6, null as col7, '' as col8
from mytable2

first table
id
name
second table
name
seatno
if you want to join on name & there are some duplicate names in both table the use ROW_NUMBER in join query!

Related

Postgres: pull two non-overlapping sets of data

I would like to pull 30K rows at random from our data store to create one data set, then 30K more rows for a second data set that doesn't overlap any ids with the first.
My idea for how to make it would would be to somehow reference the id columns pulled in the first subquery when drawing a second subquery, then return their union:
SELECT * FROM (
SELECT id_col, A, B, C, 'group1' as label
FROM my_db
LIMIT 30000
) as t1
UNION ALL
(
SELECT id_col, A, B, C, 'group2' as label
FROM my_db
WHERE id_col NOT IN t1.id_col
LIMIT 30000
) as t2
But this does not work as I get "syntax error at or near t1" .
Updated: add label to column to show how a union would have created a tall format for the two groups.
As klin pointed out in the comments, you would need to use a Common Table Expression (CTE) in order to achieve your desired result:
WITH t1 AS (
SELECT id_col, A, B, C, 'group1' AS label
FROM my_db
LIMIT 30000
), t2 AS (
SELECT id_col, A, B, C, 'group2' AS label
FROM my_db
WHERE id_col NOT IN (SELECT id_col FROM t1)
LIMIT 30000
)
SELECT id_col, A, B, C, label
FROM t1
UNION
SELECT id_col, A, B, C, label
FROM t2
You also would not need to do a UNION ALL, a UNION should suffice.

Get all the possible combinations of one column

Let's say I have a column like this:
col
---
A
B
C
And I need all possible row combinations like the ones below:
result
---
A
B
C
A,B
A,C
B,C
A,B,C
What's the best solution to get the result?
Most of the answers that I find are suggesting joins for each row but I need something to cover variant number of rows
You can use recursive cte to get the desired results and as #Serg suggested, I think 'B' and 'C' should also be the part of the final result.
create table test(col varchar(10))
insert into test
select 'A'
union select 'B'
union select 'C'
;WITH cte (grp, col)
AS
(
SELECT CAST(t.COL AS VARCHAR(100)), t.COL
FROM TEST t
UNION ALL
SELECT CAST(c.grp + ',' + t.col AS VARCHAR(100)), t.COL
FROM TEST t
INNER JOIN cte c
ON c.col < t.COL
)
SELECT grp FROM cte;
Please see the db<>fiddle here.

Insert multiple rows into a table from two different tables in oracle?

I'm trying to insert multiple rows into my table using select but I'm getting not enough values error.
My query:
Insert into c(x, y) select * from a union all select * from d;
table a and b contains 2 records each and table c has one record.
try like below by specifying both column names
Insert into c(x, y)
select col1,col2 from a
union all
select col1,col2 from d
for union all both tables have the same number of colums and their data type also need to be same
List the columns explicitly:
Insert into c (x, y)
select col1, col2
from a
union all
select col1, col2
from d;
If one of tables has only one column, then use a placeholder for the value:
Insert into c (x, y)
select col1, col2
from a
union all
select col1, NULL
from d;

Combining two columns in SQL

I want to combine two columns in such a way that the second column gets added below the first column.
For eg:
Col 1: A B C
Col 2: D E F
Result :
Col : A B C D E F
Do a UNION ALL to get the two columns as one single column:
select col1 from tablename
UNION ALL
select col2 from tablename
If you absolutely want col1 values before col2 values, wrap it up in a derived table and add an ORDER BY:
select col from
(
select col1 as col, 1 as ob from tablename
UNION ALL
select col2, 2 as ob from tablename
) dt
order by ob
SELECT CONCAT(col1, col2) AS col FROM table

Which select is faster in Sqlite?

a is a table index, b is a normal column.
select a,b from ( select a,b from table where a in (*listA*) ) where b in (*listB*)
or
select a,b from table where (a=listA[0] and b=listB[0]) or (a=listA[1] and b=listB[1])...
I am using pseudocode to represent a list declaration.
The first query is wrong because it never looks at the combinations of a and b.
To use a temp table, you have to join with it:
SELECT a, b
FROM MyTable
JOIN (SELECT 1 AS a, 2 AS b UNION ALL
SELECT 3, 4 UNION ALL
SELECT 5, 6 ...)
USING (a, b)
Which version is better optimized depends on too many factors; the only way to find out is to measure with representative data.