?
Solved

Help with unmatched query in SQL Server 2005

Posted on 2006-11-17
7
Medium Priority
?
265 Views
Last Modified: 2008-03-06
For years I've been using unmatched queries in Access (I have to admit to NEVER writing an unmatched query by hand - always used the Wizard) but now that I've moved my data to SQL Server I'm having a bit of a problem figuring out how to achieve the same results using stored procedures.

In one stored procedure, I return a set of all customers with the count of open orders:

SELECT     fldCustID, COUNT(fldOrderID) AS OpenOrders
FROM         dbo.tblOrders
WHERE     (fldCompleted = 0)
GROUP BY fldCustID

That part's easy.  Now I need to compare this to my customers table:

SELECT     fldCustID
FROM         dbo.tblCustomers
WHERE     (fldInactive = 0)

 and find all customers in the second resultset that don't exist in the results of the first stored procedure.

I'm sure this is easy but it's got me stumped.  Thanks in advance for your help!
0
Comment
Question by:penlandt
[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
  • 3
  • 3
7 Comments
 
LVL 9

Expert Comment

by:dduser
ID: 17966769
Select fldCustId from dbo.tblCustomers Inner Join (SELECT     fldCustID, COUNT(fldOrderID) AS OpenOrders
FROM         dbo.tblOrders
WHERE     (fldCompleted = 0)
GROUP BY fldCustID) as DerivedTable on DerivedTable.fldCustId <> tblCustomers.fldCustId where fldInactive = 0

Regards,

dduser
0
 
LVL 1

Author Comment

by:penlandt
ID: 17966852
Thanks for the quick reply.  This gave me a cartesian product (I stopped at 200,000+ results from a customers table of +/- 1600 rows).  Any idea where the problem is?
0
 
LVL 9

Expert Comment

by:dduser
ID: 17966876
Try this

Select fldCustId from dbo.tblCustomers where fldInactive = 0 and fldCompled = 0

Check this out this should also work...

Regards,

dduser
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 1

Author Comment

by:penlandt
ID: 17966976
Nope - there is no "fldCompleted" in the customers table - it's in the orders table.
0
 
LVL 11

Accepted Solution

by:
rw3admin earned 500 total points
ID: 17966980
This should work on your query,
SELECT     fldCustID
FROM       dbo.tblCustomers
Left Join
(      SELECT     fldCustID, COUNT(fldOrderID) AS OpenOrders
      FROM         dbo.tblOrders
      WHERE     (fldCompleted = 0)
      GROUP BY fldCustID ) as A
ON      dbo.tblCustomers.fldCustID=A.fldCustID
WHERE     (fldInactive = 0) and A.fldCustID is Null


but I think basically this is what you want ... get fldCustID which is not in tblOrders table

SELECT     dbo.tblCustomers.fldCustID
FROM       dbo.tblCustomers
Left Join
      dbo.tblOrders
ON      dbo.tblOrders.fldCustID =dbo.tblCustomers.fldCustID
WHERE     (dbo.tblCustomers.fldInactive = 0) and (dbo.tblOrders.fldCompleted = 0) and
                dbo.tblOrders.fldCustID is Null
0
 
LVL 9

Expert Comment

by:dduser
ID: 17967000
Select fldCustId from dbo.tblCustomers where fldInactive = 0 and fldCustId not in (SELECT     fldCustID FROM      dbo.tblOrders
WHERE     (fldCompleted = 0)
GROUP BY fldCustID)

Regards,

dduser
0
 
LVL 1

Author Comment

by:penlandt
ID: 17967033
Thank You.  The first of the two was what I was looking for.  The second one won't work for me because all of the customers have at least one record in the orders table.  Thanks again for the solution.
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

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…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
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…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Suggested Courses

765 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