Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

struggling with a query giving duplicate values Expand / Collapse
Author
Message
Posted Friday, January 17, 2014 9:13 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Thursday, March 13, 2014 2:33 PM
Points: 117, Visits: 315
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
Post #1532121
Posted Friday, January 17, 2014 9:42 AM
SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: Yesterday @ 12:03 PM
Points: 621, Visits: 2,127
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
Post #1532143
Posted Friday, January 17, 2014 9:45 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 7:20 AM
Points: 13,298, Visits: 12,149
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 Moden's 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)
Post #1532145
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse