Solved

MS SQL records exclusion

Posted on 2011-09-14
7
184 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
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Modal Popup Extender control 1 18
Simple SQL query from two tables 13 51
always on switch back after failover 2 31
Expression Evaluater 3 24
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

786 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