SQL, exclude condition

Hi Experts,

I have a column "Status" which has value : archived, accepted, sold, sent.

I want to get the dealid which its status is either not accepted or not archived. But dealid A  has multiple status by different time, i.e. sent, sold, archived. So, how can I get the dealid without dealid A.

This is the query I wrote, but it still return dealid A.

select dealid from dealview
where status <>'archived' or status<>'accepted'

Thank you!
tanj1035Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Vitor MontalvãoMSSQL Senior EngineerCommented:
You should change the OR to AND. Also you can use the NOT IN operator:
select dealid 
from dealview
where status not in ('archived','accepted')

Open in new window

0
Brian CroweDatabase AdministratorCommented:
Vitor's solution is the best one but your query would have worked if you replaced the OR with AND.
0
tanj1035Author Commented:
I tried "And" and also "not in". It still returns dealid A. The tricky part is that if dealid A only has one status: archived, your solutions work. But, dealid A has multiple status: sent, sold, and archived  by different period. see the table below

DealID    Date   Status
A              5-1      Sent
A              5-2      Sold
A              5-3      Archived

So, the query will still return the rows which have status=sent, sold.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

tanj1035Author Commented:
Thanks for your help, guys, I worked it out. Maybe my query is not compact enough, but the result is correct.





declare @StartDate date
declare @EndDate date
declare @Dealerid int
set @StartDate ='2015-05-01'
set @EndDate='2015-05-27'
set @Dealerid= 169
;

with cte as (select DV.dealid, d.lastmessage as dealerlastmessage, s.lastmessage as shopperlastmessage, dr.shopperresponsestatus
from  MMD_Feed.[dbo].[DealResponseMinAndMaxdealer] d
inner join MMD_Feed.[dbo].[DealResponseMinAndMaxShopper] s on d.dealid=s.dealid
inner join MMD_Feed.dbo.dealresponse dr on dr.dealid=s.dealid
inner join MMD_Feed.dbo.dealview dv on dv.dealid=dr.dealid
where dv.dealerid=@Dealerid
and cast(dr.timestamp as date)between @StartDate and @EndDate
and d.lastmessage<s.lastmessage
and dv.dealerid =@Dealerid
and dv.dealid not in (select dealid from MMD_Feed.dbo.dealresponse where shopperresponsestatus  in ('accepted','archived')))

select distinct dealid from cte
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Brian CroweDatabase AdministratorCommented:
Another way to do it...

select D.dealid
from dealview AS D
LEFT OUTER JOIN dealview AS DArchived
   ON D.dealid = DArchived.dealid
   AND DArchived.status = 'archived'
LEFT OUTER JOIN dealview AS DAccepted
   ON D.dealid = DAccepted.dealid
   AND DAccepted.status = 'accepted'
WHERE DArchived.dealid IS NULL
   AND DAccepted.dealid IS NULL
0
tanj1035Author Commented:
Hi Brian, thanks for your reply.

This is how it will show based on my understanding of the virtual tables from your query.  I tried your query, but it did not return a correct result. Do you have any thoughts? Thanks.

         Table D              Table Darchive               Table Daccepted
DealID      Status              DealID      Status                  DealID      Status
   1             sent                  1             archive                  1            accepted
   1             accepted                    
   1             archive      
   2             sent              
   3             saved
0
tanj1035Author Commented:
Vitor's solution gave me some ideas, and I have modified
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.