?
Solved

MS SQL records exclusion

Posted on 2011-09-14
7
Medium Priority
?
189 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
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 2000 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

Get MySQL database support online, now!

At Percona’s web store you can order your MySQL database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Suggested Courses

752 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