Solved

Help with unmatched query in SQL Server 2005

Posted on 2006-11-17
7
259 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
  • 3
  • 3
7 Comments
 
LVL 9

Expert Comment

by:dduser
Comment Utility
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
Comment Utility
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
Comment Utility
Try this

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

Check this out this should also work...

Regards,

dduser
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 1

Author Comment

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

Accepted Solution

by:
rw3admin earned 125 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Performance is the key factor for any successful data integration project, knowing the type of transformation that you’re using is the first step on optimizing the SSIS flow performance, by utilizing the correct transformation or the design alternat…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how the fundamental information of how to create a table.

772 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