x

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, 2012 at 08:30 PM in Default

sfenster gravatar image

sfenster
20 1 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, 2012 at 09:16 PM

SirSQL gravatar image

SirSQL
4.8k 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, 2012 at 04:14 AM

Scot Hauder gravatar image

Scot Hauder
6k 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.

New code box

There's a new way to format code on the site - the red speech bubble logo will automatically format T-SQL for you. The original code box is still there for XML, etc. More details here.

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

SQL Server Central

Need long-form SQL discussion? SQLserverCentral.com is the place.

Topics:

x1833

asked: Aug 13, 2012 at 08:30 PM

Seen: 412 times

Last Updated: Aug 14, 2012 at 04:16 AM