Solved

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

Posted on 2014-03-10
2
279 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
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now