Solved

MS SQL records exclusion

Posted on 2011-09-14
7
185 Views
Last Modified: 2012-05-12
MS SQL Server 2008R2

I have the table 'widgets"

widget         process      id
     1                   a           1
      1                  a           2
     1                    b           3

     2                    a           4
      2                   a           5
      2                   a           6

      3                    a          7
      3                   a           8
      3                    a          9
       3                    a        10

        4                   a         11
        4                    a        12
         4                   b          13
I would like to return the record for a widget that does NOT have a process of "b" in any of it's records, and the record I would like to return is the most recent record (highest ID number of any of its records).
The record needed from the above set would be the ones with the "ID" field containing
 "6" & "10"
0
Comment
Question by:ITMikeK
7 Comments
 
LVL 42

Expert Comment

by:dqmq
ID: 36540689
Select * from widgets w inner join
(Select max(id) as Id from widgets t1
   where not exists
   (select * from widgets t2 where t1.widget = t2.widget and t2.process = 'b')
) m on m.id = w.id
0
 
LVL 42

Expert Comment

by:dqmq
ID: 36540695
Opps, try again...

Select * from widgets w inner join
(Select widget, max(id) as Id from widgets t1
   where not exists
   (select * from widgets t2 where t1.widget = t2.widget and t2.process = 'b')
   group by widget
) m on m.id = w.id and m.widget = w.widget
0
 
LVL 9

Expert Comment

by:sachinpatil10d
ID: 36540928
select widget, process, MAX(id) Id from (
select * from Test
where widget not in(select widget from Test where process = 'b')
) tmp
group by widget,process
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 9

Expert Comment

by:sachinpatil10d
ID: 36540930
select widget, process, MAX(id) Id from (
select * from widgets
where widget not in(select widget from widgets where process = 'b')
) tmp
group by widget,process
0
 
LVL 9

Accepted Solution

by:
sachinpatil10d earned 500 total points
ID: 36540931
select * from widgets
where widget not in(select widget from widgets where process = 'b')
and id in (select MAX(id) from widgets group by widget)
0
 
LVL 16

Expert Comment

by:Easwaran Paramasivam
ID: 36542234
select top 1 from widgets
where process != 'b'
order by id desc
0
 
LVL 18

Expert Comment

by:deighton
ID: 36542341
SELECT * FROM 
(SELECT W1.*, RANK() OVER (partition by W1.widget order by W1.id DESC) TheRank  FROM Widgets W1 WHERE NOT EXISTS(SELECT NULL FROM WIDGETS W2 WHERE W2.process = 'b' AND W2.Widget = W1.Widget)) SUBQ
WHERE SUBQ.TheRank = 1

Open in new window

0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how the fundamental information of how to create a table.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question