x
login about faq Site discussion (meta-askssc)

How would you write this query...

I need help writing a query. Here's the data I have:

ticketnumber   status
------------   ------
123            0
123            16
456            16
789            0
234            0
345            0
678            16

I want to find all of the records for each ticketnumber that do not have at least a status of 0. So, 123 is ok because there's a record for 123 with a status of 0. 456 is not ok because there is only a record with a status of 16. 678 is not ok either.

The output based on the sample input should be:

ticketnumber
------------
456
678

I'm thinking I will need to do a self join. I'm not quite sure of how to proceed.

Any help would be appreciated.

Thanks,

Seth

more ▼

asked Aug 13 '12 at 08:30 PM in Default

sfenster gravatar image

sfenster
20 1

(comments are locked)
10|1200 characters needed characters left

2 answers: sort voted first

How about

SELECT y.ticketnumber 
FROM YourTable y
WHERE ticketnumber NOT IN (SELECT ticketnumber
FROM YourTable
WHERE status = 0)
GROUP BY y.ticketnumber
more ▼

answered Aug 13 '12 at 09:16 PM

SirSQL gravatar image

SirSQL
4.6k 1 3

(comments are locked)
10|1200 characters needed characters left

If Status cannot be negative...

SELECT TicketNumber
FROM Tickets
GROUP BY TicketNumber
HAVING MIN(Status) <> 0
more ▼

answered Aug 14 '12 at 04:14 AM

Scot Hauder gravatar image

Scot Hauder
5.7k 13 15 18

(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments



Facebook logo Follow Ask SSC on Facebook
Find Ask SSC on Google+
linkedin logo Find us on LinkedIn

Topics:

x1601

asked: Aug 13 '12 at 08:30 PM

Seen: 241 times

Last Updated: Aug 14 '12 at 04:16 AM

Copyright © 2002-2012 Simple Talk Publishing. All Rights Reserved. If you have any queries, please contact the site administrators.
Ask SQL Server Central is a community service provided by Red Gate.