Solved

Help with unmatched query in SQL Server 2005

Posted on 2006-11-17
7
261 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
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
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
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 125 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sql server insert 12 30
convert null in sql server 12 33
Return 0 on SQL count 24 29
Replace the integer portion in CAST with a column 4 15
I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
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 shrink a transaction log file down to a reasonable size.

770 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