SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


struggling with a query giving duplicate values


struggling with a query giving duplicate values

Author
Message
briancampbellmcad
briancampbellmcad
SSC Veteran
SSC Veteran (256 reputation)SSC Veteran (256 reputation)SSC Veteran (256 reputation)SSC Veteran (256 reputation)SSC Veteran (256 reputation)SSC Veteran (256 reputation)SSC Veteran (256 reputation)SSC Veteran (256 reputation)

Group: General Forum Members
Points: 256 Visits: 428
I'm struggling with what would seem quite easy...
I have two tables tblBank and tblBook... I'm getting duplicates... any clues?
Looks like the LEFT JOIN would give me only 2 rows back:

SELECT DISTINCT Book.Store, Book.Amount
FROM tblBank LEFT JOIN tblBook ON tblBank.Amount = tblBook.Amount
GROUP BY Store, Amount;

tblBank
Store Amount
15 400.00
21 900.00
27 300.00
29 500.00

tblBook
Store Amount
8 300.00
15 400.00
15 400.00
21 600.00
27 300.00
27 300.00
29 700.00
30 100.00

Query results:

Store Amount
15 400.00
15 400.00
27 300.00
27 300.00
Nevyn
Nevyn
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1550 Visits: 3149
this question doesn't make sense on multiple fronts:

First, you provided no DDL, making things harder. We don't know the data type of amount

Second, your query does not even run with your sample. It references Book.store, but you don't select from a table named book, and you don't create an alias, so thats obviously not a real query.

Third, you are doing both a group by and a distinct, which is redundant. Suspenders and a belt ...

Fourth, what real world problem would have you joining on amount like this? Doesn't make much sense.

Fifth, I tried to build sample DDL and copy your query with fixed aliases, and did not get the results (duplicates) that you claimed you are getting.



So my guess is you had a problem on much more complicated tables you did not choose to share, so tried to boil down the problem into an abstract example, but did not put any serious effort or even a test into seeing whether the example demonstrated the problem.



briancampbellmcad (1/17/2014)
I'm struggling with what would seem quite easy...
I have two tables tblBank and tblBook... I'm getting duplicates... any clues?
Looks like the LEFT JOIN would give me only 2 rows back:

SELECT DISTINCT Book.Store, Book.Amount
FROM tblBank LEFT JOIN tblBook ON tblBank.Amount = tblBook.Amount
GROUP BY Store, Amount;

tblBank
Store Amount
15 400.00
21 900.00
27 300.00
29 500.00

tblBook
Store Amount
8 300.00
15 400.00
15 400.00
21 600.00
27 300.00
27 300.00
29 700.00
30 100.00

Query results:

Store Amount
15 400.00
15 400.00
27 300.00
27 300.00

Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26754 Visits: 17557
briancampbellmcad (1/17/2014)
I'm struggling with what would seem quite easy...
I have two tables tblBank and tblBook... I'm getting duplicates... any clues?
Looks like the LEFT JOIN would give me only 2 rows back:

SELECT DISTINCT Book.Store, Book.Amount
FROM tblBank LEFT JOIN tblBook ON tblBank.Amount = tblBook.Amount
GROUP BY Store, Amount;

tblBank
Store Amount
15 400.00
21 900.00
27 300.00
29 500.00

tblBook
Store Amount
8 300.00
15 400.00
15 400.00
21 600.00
27 300.00
27 300.00
29 700.00
30 100.00

Query results:

Store Amount
15 400.00
15 400.00
27 300.00
27 300.00


If you could post some consumable ddl and sample data this would be a lot easier. There is no way based on what you posted that you are getting those results. Not only is it impossible to have duplicates (you used distinct AND a group by) effectively ensuring no duplicates twice, you also would have NULL, NULL and 8, 300.00.

I think you have obfuscated the real data to a point that your example is not an accurate representation of the actual details.

Here is something similar to your posted tables and data.


create table #tblBank
(
Store int,
Amount decimal(9,2)
)

insert #tblBank
select 15, 400.00 union all
select 21, 900.00 union all
select 27, 300.00 union all
select 29, 500.00

create table #tblBook
(
Store int,
Amount decimal(9,2)
)

insert #tblBook
select 8, 300.00 union all
select 15, 400.00 union all
select 15, 400.00 union all
select 21, 600.00 union all
select 27, 300.00 union all
select 27, 300.00 union all
select 29, 700.00 union all
select 30, 100.00



Then I had to modify your query because of the syntax errors so I came up with this. (Your query referenced an alias that isn't in the code you posted)


SELECT DISTINCT
Book.Store, Book.Amount
FROM #tblBank Bank
LEFT JOIN #tblBook Book ON Bank.Amount = Book.Amount
GROUP BY book.Store, book.Amount;




--EDIT--

Seems that Nevyn posted much the same results as me while I was posting.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search