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

avatar 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

avatar image

SirSQL
4.9k 4 5

(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

avatar image

Scot Hauder
6.4k 13 16 22

(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

SQL Server Central

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

Topics:

x2091

asked: Aug 13, 2012 at 08:30 PM

Seen: 496 times

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

Copyright 2016 Redgate Software. Privacy Policy