SQL return both table rows separately - sql-server

I have two tables and want to return both table's rows. Both tables do not have any relation.
Table 1 has columns userid, name, and other columns... and Table 2 has only two columns id, name.
I want both table results in one query result set.
Table results:
userid name and other columns from Table 1.
id name and NULL, NULL should show as Table 2 do not have extra columns.

Use a union
select userid, name, col1, col2, col3 from table1
union all
select id, name, null, null, null from table2

select userid, name, col1, col2, col3 from table1
union all
select id, name, null, null, null from table2

This should work:
(SELECT userid, name, column3, column4, column5 FROM table1)
UNION ALL
(SELECT id, name, NULL, NULL, NULL FROM table2)

Related

Union all when one query is missing a column

This is similar to the code I use:
SELECT id, col1 FROM table1
UNION ALL
SELECT id, col2 FROM table2
UNION ALL
SELECT id, col3 FROM table3
...
For example col2 in table2 does not exist, how to make this query work and not to query for columns that do not exist?
Using NULL or any other value and alias it to match column list defintion:
SELECT id, col1 FROM table1
UNION ALL
SELECT id, NULL AS col2 FROM table2
UNION ALL
SELECT id, col3 FROM table3

SQL Server append table to an existing table

I need to append rows from one table to an existing table. I am thinking something like following but this gives an error
select *
into table1
from (select *
from table1
union
select *
from table2) as tmp
Is there a way to use ALTER TABLE or UPDATE with UNION?
i will assume the below scenario ,
1- you need to insert in table1 all data from table 2
use this one
INSERT INTO TABLE1 (Col1, Col2)
SELECT Col1, COl2 FROM Table2
2- you have 2 tables , table 1 and need to insert in table 3
INSERT INTO TABLE3 (Col1, Col2)
SELECT Col1, COl2 FROM Table1
Union all --to remove duplication in data
SELECT Col1, COl2 FROM Table2
Just do a direct insert to Table1 from Table2
INSERT INTO TABLE1 (Col1, Col2)
SELECT
Col1, COl2
FROM
Table2
Use this query:
-- merge table1 and table2 data in temp table
SELECT * INTO #Temp
FROM
(
SELECT *
FROM table1
UNION
SELECT *
FROM table2
)
-- empty table1 and delete existing data
TRUNCATE TABLE table1
-- insert all merged data in table 1
INSERT INTO table1
SELECT *
FROM #Temp
You can remove duplicate rows with using UNION ALL instead of UNION

Ignore duplicates and not deleting them

I already know how to delete duplicate rows on an Id column. But I am not allowed to delete anything on the server. What I need is a WITH statement that ignores duplicate rows (takes only one of them). Is there a way to do this without modifying data in a table?
P.S.1 All the duplicates rows are identical. So there is no need to decide which one to keep.
P.S.2 I'd rather not create an extra temp table (SELECT * INTO ...)
You can use a CTE which does a select DISTINCT on all columns:
WITH cte AS (
SELECT DISTINCT Id, col1, col2, ..., colN
FROM yourTable
)
You could also achieve this using GROUP BY on all columns:
WITH cte AS (
SELECT Id, col1, col2, ..., colN
FROM yourTable
GROUP BY Id, col1, col2, ..., colN
)
If the Id values are not duplicated, but all other columns are, then you can try:
WITH cte AS (
SELECT MIN(Id) AS Id, col1, col2, ..., colN
FROM yourTable
GROUP BY col1, col2, ..., colN
)

Convert column to rows in SQL Server

I have a scenario where I need to convert column to rows. I have two tables table1 and table2 with the following structure
Table1:
Col11 Col12 Col13
-------------------------
200 text 55
Table2:
Col1 Col2
--------------------
Col11
Col12
Col13
In need the following result from the above two tables
Col1 Col2
--------------------
Col11 200
Col12 text
Col13 55
Is it possible to do this by using temp tables ?
You can do it using UNION ALL:
SELECT 'Col1' AS Col1, Col1 AS Col2
FROM mytable
UNION ALL
SELECT 'Col2', Col2
FROM mytable
UNION ALL
SELECT 'Col3', Col3
FROM mytable
Table2 doesn't seem to play any role in producing the required result set. So it is left out in the above query.
you can use cross apply or UNPIVOT to get
select
coll,colvalue
from PivotColToRow
cross apply
(
select 'col1', col1 union all
select 'col2', col2 union all
select 'col3', col3
) c (Coll, colvalue);
Using UNPIVOT
select col,colvalue
from PivotColToRow
unpivot
(
colvalue
for col in (col1, col2, col3)
) unpiv;
try this

Sql server union but keep order

Is there a way to union two tables, but keep the rows from the first table appearing first in the result set?
For example:
Table1
name surname
-------------------
John Doe
Bob Marley
Ras Tafari
Table2
name surname
------------------
Lucky Dube
Abby Arnold
I want the result set to look like:
name surname
-------------------
John Doe
Bob Marley
Ras Tafari
Lucky Dube
Abby Arnold
Unfortunately, union somehow reorders the table. Is there a way around this?
Try this :-
Select *
from
(
Select name,surname, 1 as filter
from Table1
Union all
Select name,surname , 2 as filter
from Table2
)
order by filter
The only way to guarantee output order is to use ORDER BY:
SELECT name,surname,1 as rs
FROM table1
UNION ALL
SELECT name,surname,2
FROM table2
ORDER BY rs
If you don't want rs to appear in the final result set, do the UNION as a subquery:
SELECT name,surname
FROM (
SELECT name,surname,1 as rs
FROM table1
UNION ALL
SELECT name,surname,2
FROM table2
) t
ORDER BY rs
;WITH cte as (
SELECT name, surname, 1 as n FROM table1
UNION ALL
SELECT name, surname, 2 as n FROM table2
UNION ALL
SELECT name, surname, 3 as n FROM table3
)
SELECT name, surname
FROM cte
ORDER BY n;
.Like this?
CREATE TABLE #Table1 (Names VARCHAR(50))
CREATE TABLE #Table2 (Names VARCHAR(50))
INSERT INTO #Table1
(
Names
)
VALUES
('John Doe'), ('Bob Marley'), ('Ras Tafari')
INSERT INTO #Table2
(
Names
)
VALUES
('Lucky Dube'), ('Abby Arnold')
SELECT ArbSeq = 1, *
FROM #Table1
UNION ALL
SELECT ArbSeq = 2, *
FROM #Table2
ORDER BY ArbSeq
It should be noted that ordering is not guaranteed when not explicitly defined.
If the table features a clustered index, the rows will typically be returned in the index's order - but this is not guaranteed.

Resources