Gmail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Join query returns to many rows
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  14 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Sharon  
View profile  
 More options Nov 3, 11:19 am
Newsgroups: microsoft.public.sqlserver.programming
From: Sharon <Sha...@discussions.microsoft.com>
Date: Mon, 2 Nov 2009 16:19:01 -0800
Local: Tues, Nov 3 2009 11:19 am
Subject: Join query returns to many rows
HI Community

    I am new to the community and I have a question that I hope someone
can answer which is that I have a query that joins 3 tables.  If I only select
the columns in table1  the correct number of records are returned but when
when I select the columns from table2 and table3 as shown below the rows
mulitple exponentially even though I am using the on keyword.  Does anyone
have any idea why that it happening?

select a.fname,  a.lname, a.address, a.phone,
            b.lname,  b.address2,
            c.lname, c.phone2
from table1 a
inner join table2 b  
    on a.lname = b.lname
inner join table3 c
    on a.lname = c.lname
order by a.lname

    Thanks in advance
    Sharon


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
--CELKO--  
View profile  
 More options Nov 3, 11:50 am
Newsgroups: microsoft.public.sqlserver.programming
From: --CELKO-- <jcelko...@earthlink.net>
Date: Mon, 2 Nov 2009 16:50:30 -0800 (PST)
Local: Tues, Nov 3 2009 11:50 am
Subject: Re: Join query returns to many rows
"A problem well stated is a problem half solved." -- Charles F.
Kettering

Please post DDL, so that people do not have to guess what the keys,
constraints, Declarative Referential Integrity, data types, etc. in
your schema are. If you know how, follow ISO-11179 data element naming
conventions and formatting rules.  Temporal data should use ISO-8601
formats.  Code should be in Standard SQL as much as possible and not
local dialect.

Sample data is also a good idea, along with clear specifications.  It
is very hard to debug code when you do not let us see it. If you want
to learn how to ask a question on a Newsgroup, look at:
http://www.catb.org/~esr/faqs/smart-questions.html

If you don't know anything about RDBMS, then get a copy of the
simplest intro book I know --
http://www.amazon.com/Manga-Guide-Databases-Mana-Takahashi/dp/1593271905


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Tony Rogerson  
View profile  
 More options Nov 3, 4:36 pm
Newsgroups: microsoft.public.sqlserver.programming
From: "Tony Rogerson" <tonyroger...@torver.net>
Date: Tue, 3 Nov 2009 05:36:07 -0000
Local: Tues, Nov 3 2009 4:36 pm
Subject: Re: Join query returns to many rows
hi Sharon,

Are you absolutely sure that is the same query you ran and you didn't have a
DISTINCT in there?

Can you post the other query please.

Given the exact same FROM clause you should be getting the same number of
rows because its the joins that bring stuff back and you've no WHERE so
there is no filtering and the SELECT does no filtering.

Tony.

"Sharon" <Sha...@discussions.microsoft.com> wrote in message

news:9E858A72-82F3-4872-A2DE-63F4AA62B493@microsoft.com...


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Plamen Ratchev  
View profile  
 More options Nov 3, 5:21 pm
Newsgroups: microsoft.public.sqlserver.programming
From: Plamen Ratchev <Pla...@SQLStudio.com>
Date: Tue, 03 Nov 2009 01:21:05 -0500
Local: Tues, Nov 3 2009 5:21 pm
Subject: Re: Join query returns to many rows
Since you join on the lname column, that would mean that there are duplicate lname values in one or more of the tables.
You can easily check that in each table with a query like this:

SELECT lname, COUNT(*) AS cnt
FROM Table
GROUP BY lname
HAVING COUNT(*) > 1;

To solve this there are different approaches based on your data. If the rows contain exact duplicate data then you can
use DISTINCT:

SELECT DISTINCT a.fname, ...

Also, maybe the query needs to join the tables on additional columns, fname for example:

SELECT ...
FROM Table1 AS A
JOIN Table2 AS B
   ON A.lname = B.lname
  AND A.fname = B.fname
JOIN Table3 AS C
   ON A.lname = C.lname
  AND A.fname = C.fname

--
Plamen Ratchev
http://www.SQLStudio.com


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Sharon  
View profile  
 More options Nov 5, 7:34 am
Newsgroups: microsoft.public.sqlserver.programming
From: Sharon <Sha...@discussions.microsoft.com>
Date: Wed, 4 Nov 2009 12:34:08 -0800
Local: Thurs, Nov 5 2009 7:34 am
Subject: RE: Join query returns to many rows
Hi Tony

    You are right, the actual query says "Select Distinct(Description), ...
" but how can that Distinct keyword cause so many rows to be returned when I
add the column names from the other tables to the select statement?


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Sharon  
View profile  
 More options Nov 5, 7:39 am
Newsgroups: microsoft.public.sqlserver.programming
From: Sharon <Sha...@discussions.microsoft.com>
Date: Wed, 4 Nov 2009 12:39:02 -0800
Local: Thurs, Nov 5 2009 7:39 am
Subject: Re: Join query returns to many rows
Hi Tony

    You are right, the actual query has says "Select Distinct(Description),
...." but how can the Distinct keyword cause all of those rows to mulitply
when I add the columns from the other tables?

    Sharon


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Hugo Kornelis  
View profile  
 More options Nov 5, 8:22 am
Newsgroups: microsoft.public.sqlserver.programming
From: Hugo Kornelis <h...@perFact.REMOVETHIS.info.INVALID>
Date: Wed, 04 Nov 2009 22:22:28 +0100
Local: Thurs, Nov 5 2009 8:22 am
Subject: Re: Join query returns to many rows

On Wed, 4 Nov 2009 12:34:08 -0800, Sharon wrote:
>Hi Tony

>    You are right, the actual query says "Select Distinct(Description), ...
>" but how can that Distinct keyword cause so many rows to be returned when I
>add the column names from the other tables to the select statement?

Hi Sharon,

The DISTINCT keyword doesn't add extra rows, it removes duplicate rows.
And for determining duplicates, the entire SELECT list is used. So if
you have a query without DISTINCT that would return

FirstName
---------
George
George
John

than adding DISTINCT would remove one of the duplicated rows with first
name equal to George. However, if the query without DISTINCT would have
returned

FirstName LastName
--------- -----------
George    Harrison
George    Michael
John      Lennon

adding DiSTINCT would not remove anything at all, because George Michael
is not a duplicate of George Harrison.

So when you added extra columns to the SELECT, rows that were duplicates
in the old, smaller subset of columns now no longer are duplicates so
they are no longer removed.

--
Hugo Kornelis, SQL Server MVP
My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Tony Rogerson  
View profile  
 More options Nov 5, 6:26 pm
Newsgroups: microsoft.public.sqlserver.programming
From: "Tony Rogerson" <tonyroger...@torver.net>
Date: Thu, 5 Nov 2009 07:26:40 -0000
Local: Thurs, Nov 5 2009 6:26 pm
Subject: Re: Join query returns to many rows
It makes the final result non-distinct.

The DISTINCT keyword works across the entire row rather than just the
column.

Can you post your queries and I can explain further.

Many thanks,
Tony

"Sharon" <Sha...@discussions.microsoft.com> wrote in message

news:126F461F-331E-4D98-BAE8-109F6CEBD2DB@microsoft.com...


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Sharon  
View profile  
 More options Nov 6, 11:20 am
Newsgroups: microsoft.public.sqlserver.programming
From: Sharon <Sha...@discussions.microsoft.com>
Date: Thu, 5 Nov 2009 16:20:01 -0800
Local: Fri, Nov 6 2009 11:20 am
Subject: Re: Join query returns to many rows
Hello Hugo and thank you for the explanation

    Does that mean of the following 2 queries that the first one would only
compare the lastname fields to determine if the row is a  duplicate and the
second query compare  the lastname and firstname and address fields to
determine if the row is a duplicate?

select distinct(lastname), firstname, address
or
select distinct lastname, firstname, address


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Sharon  
View profile  
 More options Nov 6, 11:26 am
Newsgroups: microsoft.public.sqlserver.programming
From: Sharon <Sha...@discussions.microsoft.com>
Date: Thu, 5 Nov 2009 16:26:02 -0800
Local: Fri, Nov 6 2009 11:26 am
Subject: Re: Join query returns to many rows
Hello Tony

    I actually found out that it was the other columns that were added that  
made the rows duplicates.

    Thank you
    Sharon


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Tom Cooper  
View profile  
 More options Nov 6, 1:12 pm
Newsgroups: microsoft.public.sqlserver.programming
From: "Tom Cooper" <tomcoo...@comcast.net>
Date: Thu, 5 Nov 2009 21:12:23 -0500
Local: Fri, Nov 6 2009 1:12 pm
Subject: Re: Join query returns to many rows
No, both of those queries would do the same thing.  Distinct would apply to
all columns in both cases and compare the lastname and firstname and address
fields to determine if the row is a duplicate.

Tom

"Sharon" <Sha...@discussions.microsoft.com> wrote in message

news:26FC3762-EE5A-4113-8C13-E95DD1C2D998@microsoft.com...


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Hugo Kornelis  
View profile  
 More options Nov 7, 10:32 am
Newsgroups: microsoft.public.sqlserver.programming
From: Hugo Kornelis <h...@perFact.REMOVETHIS.info.INVALID>
Date: Sat, 07 Nov 2009 00:32:36 +0100
Local: Sat, Nov 7 2009 10:32 am
Subject: Re: Join query returns to many rows

On Thu, 5 Nov 2009 16:20:01 -0800, Sharon wrote:
>Hello Hugo and thank you for the explanation

>    Does that mean of the following 2 queries that the first one would only
>compare the lastname fields to determine if the row is a  duplicate and the
>second query compare  the lastname and firstname and address fields to
>determine if the row is a duplicate?

>select distinct(lastname), firstname, address
>or
>select distinct lastname, firstname, address

Hi Sharon,

As Tom already said, they are the same. The parentheses are not
associated with the DISTINCT keyword - the first query is considered as
starting with SELECT (lastname), firstname, address [where the
parentheses are superfluous but syntactically correct], and then with
the DISTINCT keyword added.

And that is a good thing. After all, imagine we have these two people in
the database: White, George, 1334 Main St.; and White, Burt, 6339 Small
Ave. If the former query would look only at last names for determining
duplicates, it should return one row only - but what values should it
return for the firstname and address columns?

--
Hugo Kornelis, SQL Server MVP
My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
--CELKO--  
View profile  
 More options Nov 8, 11:10 am
Newsgroups: microsoft.public.sqlserver.programming
From: --CELKO-- <jcelko...@earthlink.net>
Date: Sat, 7 Nov 2009 16:10:56 -0800 (PST)
Local: Sun, Nov 8 2009 11:10 am
Subject: Re: Join query returns to many rows
There are two forms of the SELECT [ALL | DISTINCT]; the SELECT ALL
keeps duplicate rows and the SELECT DISTINCT removes redundant
duplicate rows, leaving one copy.  SELECT by itself is a shorthand for
SELECT ALL.  You will find these keywords used in other parts of SQL
for this sort of thing.

As a generalization, a SELECT DISTINCT is a symptom of a bad query
with an unexpected CROSS JOIN hidden in it or a non-normalized schema
that has redundant duplicates.  I would guess the later, since you
have three tables with the same data.  It looks like attribute
splitting but you did not post any DDL, all we do is guess.


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Tony Rogerson  
View profile  
 More options Nov 8, 7:55 pm
Newsgroups: microsoft.public.sqlserver.programming
From: "Tony Rogerson" <tonyroger...@torver.net>
Date: Sun, 8 Nov 2009 08:55:30 -0000
Local: Sun, Nov 8 2009 7:55 pm
Subject: Re: Join query returns to many rows

> As a generalization, a SELECT DISTINCT is a symptom of a bad query
> with an unexpected CROSS JOIN hidden in it or a non-normalized schema

Precisely why you should never code using the original ansi 89 style and use
infix instead...

Never code like this...

FROM {tbl} as a, {tbl} as b
WHERE a.tblkey = b.tblkey
      AND a.postertype = 'opinionated'
      AND a.postername = '--celko--'

Instead you should write like the majority of other folk do....

FROM {tbl} as a
    INNER JOIN {tbl} as b ON a.tblkey = b.tblkey
WHERE  a.postertype = 'opinionated'
      AND a.postername = '--celko--'

Mixing the join and the filter in the WHERE clause is extremely dangerous
and like you state almost always results in a cartesian product.

--ROGGIE--

"--CELKO--" <jcelko...@earthlink.net> wrote in message

news:1309e209-ddb4-4b61-ae83-9618dcfc0b6c@z4g2000prh.googlegroups.com...


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google