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
Solved

Help with unmatched query in SQL Server 2005

Posted on 2006-11-17
7
262 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
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

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL STANDARD CORE 6 28
data stored like ????? in sql server column 19 53
Row insertion failed. Array 5 45
SQL Syntax Grouping Sum question 7 24
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.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
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…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

792 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