Solved

MS SQL: Do not include result if any order values are set to LIFETIME

Posted on 2014-03-10
2
292 Views
Last Modified: 2014-03-11
sqlOrders = "Select * From Orders Left Join Contacts On Orders.ContactID = Contacts.ContactID Where Not(Status = 'deleted') And Not(Orders.Product = 'LIFETIME') Order By Orders.ContactID Desc, Orders.OrderDate Desc"

Open in new window


I do not want any results to be returned if the value of any orders for the Contacts.ContactID are set to 'LIFETIME'.

The problem with the above query is that if a contact has 3 orders and one of them is set to 'LIFETIME' then it still returns the contact information for that user.

I use MS SQL 2008.
0
Comment
Question by:hankknight
[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
2 Comments
 
LVL 11

Accepted Solution

by:
Simone B earned 500 total points
ID: 39918767
You can use a subquery:

Select * From Orders 
Left Join Contacts On Orders.ContactID = Contacts.ContactID 
Where Not(Status = 'deleted') And Contacts.ContactID NOT IN 
	(SELECT ContactID From Orders 
	WHERE Orders.Product = 'LIFETIME') 
Order By Orders.ContactID Desc, Orders.OrderDate Desc

Open in new window

0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 39919447
if the value of any orders for the Contacts.ContactID are set to 'LIFETIME'.
Don't you mean Orders.Product.are set to 'LIFETIME'?
0

Featured Post

Quiz: What Do These Organizations Have In Common?

Hint: Their teams ended up taking quizzes, too.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
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.

717 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