SQL Server - Insert into a combination of two tables - sql

I need to combinate two tables in SQL Server. But I need to have a row with each item of table A with each item of table B, resulting in a table C. I would be like this:
Table A
A
B
C
D
Table B
1
2
3
Table C
column x | cloumn Y
A 1
A 2
A 3
B 1
B 2
B 3
C 1
C 2
C 3
D 1
D 2
D 3
Thanks all!

You can simply do this:
SELECT
a.a + CAST(b AS VARCHAR(2)) AS a
FROM tablea a
CROSS JOIN tableb AS b;
See it in action:
SQL Fiddle Demo
Then you can use the INTO clause to insert them into a table already exists:
INSERT INTO tablec(c)
SELECT
a.a + CAST( b AS VARCHAR(2)) AS a
FROM tablea a
CROSS JOIN tableb AS b;
or create a new table from these values:
SELECT
a.a + CAST( b AS VARCHAR(2)) AS c
INTO Tablec
FROM tablea a
CROSS JOIN tableb AS b;
Updated SQL Fiddle Demo
Note that: I assumed the columns' names, since you didn't specify them in your question.

Looks like you're trying to get the following:
select a.col1, b.col1
into tableC
from tableA a
cross join tableB b

Related

How to combine two tables with different number of columns SQL

I have two tables A and B which look like this:
Table A
col1 col2 ID
--------------
a b 1
c d 2
Table B
col3 col4 ID
--------------
x t 1
y u 1
z o 2
m n 2
I want to create this new table:
Table C
col1 col2 col3 col4 ID
-------------------------
a b x t 1
a b y u 1
c d z o 2
c d m n 2
The ID's in A are not the unique ID's of those elements in that list. I just need to add a few more properties from their "parent" table(B) in this new table. I know there will be a lot of repetitions but I don't mind that for now.
I tried this following statement:
INSERT INTO C(SELECT A.*, B.col1, B.col2 from A LEFT OUTER JOIN B ON (B.ID = A.ID));
But it is resulting in something I did not expect. The row count of C must be the same as the row count of B. However when I used this SQL query, the resulting tables row count was even more than the total row count of A and B. I'm a beginner in using SQL I would appreciate any help.
A basic JOIN will do:
select
a.col1,
a.col2,
b.col3,
b.col4,
a.id
from tablea a
join tableb b on a.id = b.id
If I got you right your tables do not have any relation to each other.
So in this case you should be able to do something like this?
Select * from table1, table2
You can also combine this with an insert so you should get your new table

Left join but maintain values where NULL may result in Microsoft SQL

I have two tables:
Table A:
id names
1 a
2 b
3 c
and Table B:
id names
1 x
2 y
I'd like to perform a left join of Table B on Table A that results in the following table:
id names
1 x
2 y
3 c
How can I do this in Microsoft SQL?
You could use COALESCE:
SELECT a.id, COALESCE(b.name, a.name) AS name
FROM tab1 a
LEFT JOIN tab2 b
ON a.id = b.id
I think you just want coalesce():
select a.id, coalesce(b.name, a.name) as name
from a left join
b
on a.id = b.id;

SQL: pivot multiple tables

I have two tables
TableA
ID Qualification
1 A
2 A
3 B
TableB
ID Qualification
1 C
2 A
3 A
Unfortunately, the names of the columns in table A and B are the same - resulting in an error 8156 - The column 'Qualification' was specified multiple times.
My select looks like follows
SELECT *
FROM (
SELECT A.ID, A.Qualification, B.Qualification
FROM TableA A LEFT OUTER JOIN TableB B
ON A.ID = B.ID
)s
PIVOT
(SUM(ID)
FOR Qualification IN ([A],[B],[C])) pvt
TIA!

select sql query to merge results

I have a table old_data and a table new_data. I want to write a select statement that gives me
Rows in old_data stay there
New rows in new_data get added to old_data
unique key is id so rows with id in new_data should update existing ones in old_data
I need to write a select statement that would give me old_data updated with new data and new data added to it.
Example:
Table a:
id count
1 2
2 19
3 4
Table b:
id count
2 22
5 7
I need a SELECT statement that gives me
id count
1 2
2 22
3 4
5 7
Based on your desired results:
SELECT
*
FROM
[TableB] AS B
UNION ALL
SELECT
*
FROM
[TableA] AS A
WHERE
A.id NOT IN (SELECT id FROM [TableB])
I think this would work pretty neatly with COALESCE:
SELECT a.id, COALESCE(b.count, a.count)
FROM a
FULL OUTER JOIN b
ON a.id = b.id
Note - if your RDBMS does not contain COALESCE, you can write out the function using CASE as follows:
SELECT a.id,
CASE WHEN b.count IS NULL THEN a.count
ELSE b.count END AS count
FROM ...
You can write a FULL OUTER JOIN as follows:
SELECT *
FROM a
LEFT JOIN b
ON a.id = b.id
UNION ALL
SELECT *
FROM b
LEFT a
ON b.id = a.id
You have to use UPSERT to update old data and add new data in Old_data table and select all rows from Old_data. Check following and let me know what you think about this query
UPDATE [old_data]
SET [count] = B.[count]
FROM [old_data] AS A
INNER JOIN [new_Data] AS B
ON A.[id] = B.[id]
INSERT INTO [old_data]
([id]
,[count])
SELECT A.[id]
,A.[count]
FROM [new_Data] AS A
LEFT JOIN [old_data] AS B
ON A.[id] = B.[id]
WHERE B.[id] IS NULL
SELECT *
FROM [old_data]

Problem combining result of two different queries into one

I have two tables (TableA and TableB).
create table TableA
(A int null)
create table TableB
(B int null)
insert into TableA
(A) values (1)
insert into TableB
(B) values (2)
I cant join them together but still I would like to show the result from them as one row.
Now I can make select like this:
select
(select A from tableA) as A
, B from TableB
Result:
A B
1 2
But if I now delete from tableB:
delete tableB
Now when I run the same query as before:
select
(select A from tableA) as A
, B from TableB
I see this:
A B
But I was expecting seeing value from tableA
like this:
Expected Result:
A B
1
Why is this happening and how can I still see the value from TableA although selectB is returning 0 rows?
I am using MS SQL Server 2005.
Use a LEFT JOIN (although it's more of a cross join in your case).
If your db supports it:
SELECT a.a, b.b
FROM a
CROSS JOIN b
If not, do something like:
SELECT a.a, b.b
FROM a
LEFT JOIN b ON ( 1=1 )
However, once you have more rows in a or b, this will return the cartesian product:
1 1
1 2
2 1
2 2
This will actually give you what you're looking for, but if you only have one row per table:
select
(select A from tableA) as A
, (select B from TableB) as B
give this a try:
DECLARE #TableA table (A int null)
DECLARE #TableB table (B int null)
insert into #TableA (A) values (1)
insert into #TableB (B) values (2)
--this assumes that you don't have a Numbers table, and generates one on the fly with up to 500 rows, you can increase or decrease as necessary, or just join in your Numbers table instead
;WITH Digits AS
(
SELECT 0 AS nbr
UNION SELECT 1 UNION SELECT 2 UNION SELECT 3
UNION SELECT 4 UNION SELECT 5 UNION SELECT 6
UNION SELECT 7 UNION SELECT 8 UNION SELECT 9
)
, AllNumbers AS
(
SELECT u3.nbr * 100 + u2.nbr * 10 + u1.nbr + 1 AS Number
FROM Digits u1, Digits u2, Digits u3
WHERE u3.nbr * 100 + u2.nbr * 10 + u1.nbr + 1 <= 500
)
, AllRowsA AS
(
SELECT
A, ROW_NUMBER() OVER (ORDER BY A) AS RowNumber
FROM #TableA
)
, AllRowsB AS
(
SELECT
B, ROW_NUMBER() OVER (ORDER BY B) AS RowNumber
FROM #TableB
)
SELECT
a.A,b.B
FROM AllNumbers n
LEFT OUTER JOIN AllRowsA a on n.Number=a.RowNumber
LEFT OUTER JOIN AllRowsB b on n.Number=b.RowNumber
WHERE a.A IS NOT NULL OR b.B IS NOT NULL
OUTPUT:
A B
----------- -----------
1 2
(1 row(s) affected)
if you DELETE #TableB, the output is:
A B
----------- -----------
1 NULL
(1 row(s) affected)
try this:
select a, (select b from b) from a
union
select b, (select a from a) from b
should retrieve you all the existing data.
you can filter it more by surrounding it with another select