Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2014-03-10
2
Medium Priority
?
300 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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
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.

610 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