struggling with a query giving duplicate values

  • 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

  • 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

  • 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 http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply