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
 General SQL Server Forums
 New to SQL Server Programming
 Join conumdrun

Author  Topic 

gtFlue
Starting Member

8 Posts

Posted - 2012-01-27 : 11:55:54
Relatively new to SQL (taken a handful of classes) but I'm having a rough time with Joins.

If I'm joining two tables, one has 15 of the same cog (for lack of a better term) and the other has 40 sprockets. how do I prevent the join from returning 600 rows if I'm limited on the number of conditions that are true in both tables?

I understand the more join conditions you are able to set the more unique rows are returned, but what do you do if you have only 1 or 2 available join conditions?

Even using distinct does not reduce the number by much.

Thanks.

RickD
Slow But Sure Yak Herding Master

3608 Posts

Posted - 2012-01-27 : 12:01:03
It would be easier if you posted your table structures. You will have key fields between the tables which must match when you write the join condition, this should limit what is joined to what.

Then you limit further on your where clause to get only the results returned that match a condition.

Anyway, post your structures and a bit of sample data and the expected result set and you will get better answers that might start to make sense.
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2012-01-27 : 12:14:55
Well you normally would join Tables based on their "Keys"

And not the attributes of the columns

We need to know what the tables look like (DDL) and the relationships between the 2

Sample data and expected results would also help us formulate a query for you

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

gtFlue
Starting Member

8 Posts

Posted - 2012-01-27 : 13:08:16
um.... table 1 has 81 columns (2 primary Keys), Table 2 has 29 columns (3 primary Keys) only 1 PK is shared between the two. VisitID.

all columns are Varchar, except for the 2 date columns. Other than the single PK only one other column is shard and that is a name column.

I am also only reporting on 4 fields.

Would maybe a subquery be my best bet, use the smaller table to return only distinct rows based on the Name field, and then pull out the required fields from the other table?

These tables are huge since they're in a Med database. I'm trying to report insurance expiration dates for patients...easier said than done when there are more than 500K patients in the database...uhg.

Go to Top of Page
   

- Advertisement -