October 15, 2012 at 11:14 am
I am attempting to pull only open tickets from my table. Thing is we have duplicate records with different status code in our data
TICKET STATUS
9543 1
9543 5
9543 5
9544 1
9546 1
9547 1
9547 5
I only want the records with a 1 and not a 5 status, so in the above sample, I should only return 9544 and 9546.
Any suggestions are much appreciated.
October 15, 2012 at 11:28 am
What exactly are you trying to do here?
Can you please post better data, DDL and DML statements. You'll get more help if you follow the suggestions in this Post http://www.sqlservercentral.com/articles/Best+Practices.
Also Post what you have tried, and we can go from there
October 15, 2012 at 11:47 am
i think a simple test with an EXISTS and correlated subquery will get you want you want:
With YOURTABLE (TICKET,STATUS)
AS
(
SELECT 9543,1 UNION ALL
SELECT 9543,5 UNION ALL
SELECT 9543,5 UNION ALL
SELECT 9544,1 UNION ALL
SELECT 9546,1 UNION ALL
SELECT 9547,1 UNION ALL
SELECT 9547,5
)
--the above is just setup data to represent your data , wherever it is.
--data like that makes it easy for anyone to offer helpful posts.
--proposed example is just what is below.
SELECT
*
FROM YOURTABLE OPENTIX
WHERE NOT EXISTS(SELECT
1
FROM YOURTABLE CLOSEDTIX
WHERE CLOSEDTIX.TICKET = OPENTIX.TICKET
AND CLOSEDTIX.STATUS = 5)
AND OPENTIX.STATUS = 1
Lowell
October 15, 2012 at 12:13 pm
Lowell, that was exactly what I was looking for. Thank you so much for the assistance.:-)
October 15, 2012 at 2:11 pm
I think that a GROUP BY is much simpler and probably faster, too.
With YOURTABLE (TICKET,STATUS)
AS
(
SELECT 9543,1 UNION ALL
SELECT 9543,5 UNION ALL
SELECT 9543,5 UNION ALL
SELECT 9544,1 UNION ALL
SELECT 9546,1 UNION ALL
SELECT 9547,1 UNION ALL
SELECT 9547,5
)
SELECT TICKET, MAX(STATUS) AS STATUS
FROM YOURTABLE
GROUP BY TICKET
HAVING MAX(STATUS) = 1
GO
Drew
J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
October 15, 2012 at 3:27 pm
Thank you for the reply. I will keep this handy.
October 18, 2012 at 1:27 am
This is another way of doing it...and I guess the simplest way....:-)
With YOURTABLE (TICKET,STATUS)
AS
(
SELECT 9543,1 UNION ALL
SELECT 9543,5 UNION ALL
SELECT 9543,5 UNION ALL
SELECT 9544,1 UNION ALL
SELECT 9546,1 UNION ALL
SELECT 9547,1 UNION ALL
SELECT 9547,5
)
Select * From YOURTABLE
Where TICKET NOT IN (Select TICKET From YOURTABLE Where STATUS = 5)
GO
October 27, 2012 at 11:18 pm
drew.allen (10/15/2012)
I think that a GROUP BY is much simpler and probably faster, too.
With YOURTABLE (TICKET,STATUS)
AS
(
SELECT 9543,1 UNION ALL
SELECT 9543,5 UNION ALL
SELECT 9543,5 UNION ALL
SELECT 9544,1 UNION ALL
SELECT 9546,1 UNION ALL
SELECT 9547,1 UNION ALL
SELECT 9547,5
)
SELECT TICKET, MAX(STATUS) AS STATUS
FROM YOURTABLE
GROUP BY TICKET
HAVING MAX(STATUS) = 1
GO
Drew
Oh, be careful. For this particular data, that will absolutely work. If they introduce status less than 5 that aren't cause for rejection, it won't.
--Jeff Moden
Change is inevitable... Change for the better is not.
October 27, 2012 at 11:22 pm
vinu512 (10/18/2012)
This is another way of doing it...and I guess the simplest way....:-)
With YOURTABLE (TICKET,STATUS)
AS
(
SELECT 9543,1 UNION ALL
SELECT 9543,5 UNION ALL
SELECT 9543,5 UNION ALL
SELECT 9544,1 UNION ALL
SELECT 9546,1 UNION ALL
SELECT 9547,1 UNION ALL
SELECT 9547,5
)
Select * From YOURTABLE
Where TICKET NOT IN (Select TICKET From YOURTABLE Where STATUS = 5)
GO
It's a good idea for the data given. I have a hard time believing that they have no statuses between 1 and 5, though. If they do, the code you posted would produce duplicate ticket numbers. Of course, that may be just what the doctor ordered if there are status other than 1 and 5.
--Jeff Moden
Change is inevitable... Change for the better is not.
October 27, 2012 at 11:25 pm
dthmtlgod (10/15/2012)
I am attempting to pull only open tickets from my table. Thing is we have duplicate records with different status code in our dataTICKET STATUS
9543 1
9543 5
9543 5
9544 1
9546 1
9547 1
9547 5
I only want the records with a 1 and not a 5 status, so in the above sample, I should only return 9544 and 9546.
Any suggestions are much appreciated.
The question that begs to be answered here is.... in real life, will there be any tickets with a status other than 1 and 5 and what do you want to do if that's true?
--Jeff Moden
Change is inevitable... Change for the better is not.
October 27, 2012 at 11:29 pm
Lowell (10/15/2012)
i think a simple test with an EXISTS and correlated subquery will get you want you want:
With YOURTABLE (TICKET,STATUS)
AS
(
SELECT 9543,1 UNION ALL
SELECT 9543,5 UNION ALL
SELECT 9543,5 UNION ALL
SELECT 9544,1 UNION ALL
SELECT 9546,1 UNION ALL
SELECT 9547,1 UNION ALL
SELECT 9547,5
)
--the above is just setup data to represent your data , wherever it is.
--data like that makes it easy for anyone to offer helpful posts.
--proposed example is just what is below.
SELECT
*
FROM YOURTABLE OPENTIX
WHERE NOT EXISTS(SELECT
1
FROM YOURTABLE CLOSEDTIX
WHERE CLOSEDTIX.TICKET = OPENTIX.TICKET
AND CLOSEDTIX.STATUS = 5)
AND OPENTIX.STATUS = 1
Same thing here, ol' friend. This works perfectly if there are interim statuses and they don't mind seeing those interim status or the dupe ticket listings. If they want just a single ticket number for each ticket that's still open, this will probably need to change. Of course, we need to hear from the OP to know for sure.
Scratch that with my apologies. I made a mistake. Your's works fine.
--Jeff Moden
Change is inevitable... Change for the better is not.
October 28, 2012 at 4:24 am
dthmtlgod (10/15/2012)
I only want the records with a 1 and not a 5 status
;WITH SampleData (TICKET, [STATUS]) AS (
SELECT 9543, 1 UNION ALL
SELECT 9543, 5 UNION ALL
SELECT 9543, 5 UNION ALL
SELECT 9544, 1 UNION ALL
SELECT 9546, 1 UNION ALL
SELECT 9547, 1 UNION ALL
SELECT 9547, 5)
SELECT TICKET
FROM SampleData
WHERE [STATUS] IN (1,5)
GROUP BY TICKET
HAVING COUNT(*) = 1
AND MIN([STATUS]) = 1
For better assistance in answering your questions, please read this[/url].
Hidden RBAR: Triangular Joins[/url] / The "Numbers" or "Tally" Table: What it is and how it replaces a loop[/url] Jeff Moden[/url]
Viewing 12 posts - 1 through 11 (of 11 total)
You must be logged in to reply to this topic. Login to reply