Solved

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

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
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…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
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

785 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