Obtaining unique data

  • Here's my simplified table...

    Invoice_id

    897456-0001

    897456-0002

    897456-0003

    898657-0001

    898657-0004

    889977-0003

    889978-0002

    889978-0004

    889979-0001

    889979-0010

    889995-0002

    889995-0003

    889995-0004

    I want to write a script so that it returns the following results...

    889977-0003

    889978-0002

    889978-0004

    889995-0002

    889995-0003

    889995-0004

    Basically the first 6 digits are the account number and the next 4 are the invoice numbers (after the hyphen). I don't want any results on an account number if the account number has an invoice of 0001. If the account has a 0002 or higher only, I want all invoice id's returned.

    How would I approach this?

    TIA,

    John

  • Use MAX

    Group by the stuff before the dash.

    CAST the stuff after the dash as a number, get MAX of it.

  • SELECT invoice_id

    FROM <YOURTABLE>

    WHERE LEFT(invoice_id, 6) NOT IN

    (

    SELECT DISTINCT LEFT(invoice_id, 6)

    FROM <YOURTABLE>

    WHERE (RIGHT(invoice_id, 4) = '0001')

    )

    ________________________________________________________________
    you can lead a user to data....but you cannot make them think
    and remember....every day is a school day

  • Because I pulled the data into a temporary table from the main db, this script wasn't working. I figured out that because I took the data as is, the field was of type CHAR(20)...and it's not something mentioned in my original post so you wouldn't have known. When I changed your script to...

    SELECT invoice_id

    FROM <YOURTABLE>

    WHERE LEFT(RTRIM(invoice_id), 6) NOT IN

    (

    SELECT DISTINCT LEFT(RTRIM(invoice_id), 6)

    FROM <YOURTABLE>

    WHERE (RIGHT(RTRIM(invoice_id), 4) = '0001')

    )

    ...it worked beautifully.

    Thanks for putting me on track with this.

  • Illustrating what PietLinden suggested, which I like because it avoids the sort inherent in DISTINCT:

    WITH SampleData AS

    (

    SELECT invoice_no

    FROM

    (

    VALUES ('897456-0001')

    ,('897456-0002')

    ,('897456-0003')

    ,('898657-0001')

    ,('898657-0004')

    ,('889977-0003')

    ,('889978-0002')

    ,('889978-0004')

    ,('889979-0001')

    ,('889979-0010')

    ,('889995-0002')

    ,('889995-0003')

    ,('889995-0004')

    ) a (invoice_no)

    )

    SELECT invoice_no=MAX(invoice_no)

    FROM SampleData

    GROUP BY LEFT(invoice_no, 6);


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

  • dwain.c (10/21/2013)


    Illustrating what PietLinden suggested, which I like because it avoids the sort inherent in DISTINCT:

    WITH SampleData AS

    (

    SELECT invoice_no

    FROM

    (

    VALUES ('897456-0001')

    ,('897456-0002')

    ,('897456-0003')

    ,('898657-0001')

    ,('898657-0004')

    ,('889977-0003')

    ,('889978-0002')

    ,('889978-0004')

    ,('889979-0001')

    ,('889979-0010')

    ,('889995-0002')

    ,('889995-0003')

    ,('889995-0004')

    ) a (invoice_no)

    )

    SELECT invoice_no=MAX(invoice_no)

    FROM SampleData

    GROUP BY LEFT(invoice_no, 6);

    I was actually going to come back and try PietLinden's suggestion as well. I always like learning multiple ways to complete a task. Thanks for your input.

Viewing 6 posts - 1 through 5 (of 5 total)

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