ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Sql Table Join? (https://www.excelbanter.com/excel-programming/312639-sql-table-join.html)

soniya

Sql Table Join?
 
I have two table from which i am importing Data using SQl
Query. Both table got several identical filelds while
some fields do not appear in both (some fields are in
table A but not in table B and vice versa)

Is it possible that I can get data from both tables to a
single sheet either with identical fields only or even
including un identical fields?

Identical fields only will also help me.

any code example will be much helpful

Thanks

Soniya


Jamie Collins

Sql Table Join?
 
"Soniya" wrote ...

I have two table from which i am importing Data using SQl
Query. Both table got several identical filelds while
some fields do not appear in both (some fields are in
table A but not in table B and vice versa)

Is it possible that I can get data from both tables to a
single sheet either with identical fields only or even
including un identical fields?

Identical fields only will also help me.


Rows common to both tables:

SELECT T1.MyKeyCol, T1.MyDataCol
FROM Table1 T1
INNER JOIN Table2 T2
ON T1.MyKeyCol = T2.MyKeyCol;

Rows in table1 that are not in table2:

SELECT T1.MyKeyCol, T1.MyDataCol
FROM Table1 T1
LEFT JOIN Table2 T2
ON T1.MyKeyCol = T2.MyKeyCol
WHERE T2.MyKeyCol IS NULL;

Rows in table2 that are not in table1:

SELECT T2.MyKeyCol, T2.MyDataCol
FROM Table1 T1
RIGHT JOIN Table2 T2
ON T1.MyKeyCol = T2.MyKeyCol
WHERE T1.MyKeyCol IS NULL;

Rows from both tables, no duplicates:

SELECT T1.MyKeyCol, T1.MyDataCol
FROM Table1 T1
UNION
SELECT T2.MyKeyCol, T2.MyDataCol
FROM Table2 T2;

Rows from both tables, including duplicates:

SELECT T1.MyKeyCol, T1.MyDataCol
FROM Table1 T1
UNION ALL
SELECT T2.MyKeyCol, T2.MyDataCol
FROM Table2 T2
ORDER BY 1;

Jamie.

--


All times are GMT +1. The time now is 05:36 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com