Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Basic Query Question

Author  Topic 

hyefive
Starting Member

16 Posts

Posted - 2004-04-12 : 13:52:28
Hi, SQL newbie here,

I have two tables (TABLE1, TABLE2) with identical column structures. Each has colA, colB, colC, etc...

All I'm trying to do is query certain fields (colA, colB) from both tables simultaneously and work with the resuting values but I'm having a difficult time of it.

Is this possible?

TIA,


X002548
Not Just a Number

15586 Posts

Posted - 2004-04-12 : 13:56:30
Let's assume Col1 is the Primary Key of Both


SELECT a.Col1, b.Col1, a.Col2, b.Col2, ect
FROM TableA a
INNER JOIN TableB b
ON a.Col1 = b.Col1



Brett

8-)
Go to Top of Page

hyefive
Starting Member

16 Posts

Posted - 2004-04-12 : 14:04:35
In this situation, TABLE1 and TABLE2 are in no way related, so does the Primary Key have any relevance?

They just happen to have the same column structure.

I want to SELECT the same columns from each table, and have all matching records put into one recordset.

Is this any clearer?

Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2004-04-12 : 14:07:30
Post a few rows from each table so that we can better help you. What do you mean by matching records?

Tara
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2004-04-12 : 14:08:20
Do you mean duplicates?

SELECT Col1, Col2, Col3, ect
FROM (
SELECT Col1, Col2, Col3, ect
FROM TableA
UNION ALL
SELECT Col1, Col2, Col3, ect
FROM TableA
) AS XXX
GROUP BY Col1, Col2, Col3, ect
HAVING COUNT(*) > 1



Brett

8-)
Go to Top of Page

hyefive
Starting Member

16 Posts

Posted - 2004-04-12 : 14:11:39
Thanks again for the replies!

I think the UNION operator is what I need here, so I'll start down that path...

Have a nice day,
hyefive
Go to Top of Page
   

- Advertisement -