• 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/