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

Obtaining unique data Expand / Collapse
Author
Message
Posted Monday, October 21, 2013 2:52 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Thursday, July 17, 2014 1:53 PM
Points: 96, Visits: 296
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
Post #1506879
Posted Monday, October 21, 2013 2:58 PM
Say Hey Kid

Say Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey Kid

Group: General Forum Members
Last Login: Yesterday @ 10:48 PM
Points: 710, Visits: 4,530
Use MAX
Group by the stuff before the dash.
CAST the stuff after the dash as a number, get MAX of it.
Post #1506881
Posted Monday, October 21, 2013 3:13 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Yesterday @ 11:46 PM
Points: 1,886, Visits: 18,551

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 !
__________________________________________________________________
Post #1506887
Posted Monday, October 21, 2013 4:12 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Thursday, July 17, 2014 1:53 PM
Points: 96, Visits: 296
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.
Post #1506902
Posted Monday, October 21, 2013 6:27 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Yesterday @ 10:07 PM
Points: 3,617, Visits: 5,236
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!

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?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1506936
Posted Monday, October 21, 2013 6:35 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Thursday, July 17, 2014 1:53 PM
Points: 96, Visits: 296
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.
Post #1506939
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse