Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to write joint query?

Posted on 1998-10-30
1
Medium Priority
?
952 Views
Last Modified: 2006-11-17
let's say i have a one table called order which contains productid, now how do i write the query which counts the number of orders which contains only two product (Ex Product A and Product B).
i try by writing one query which display product A and then second query which display product b and then i join two gather it works fine but how do i write everything in one query instead of three query.
0
Comment
Question by:bcp5190
[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
1 Comment
 

Accepted Solution

by:
rscott_young earned 40 total points
ID: 1967279
Because you can't use "SUM" in your WHERE clause you'll have to use two queries.  Below is an example of both.  Use the first query to create a temporary table and the second to select your orders:

SELECT max([Order ID]),
sum(iif([Product ID] = 'A',1,0)) as PRODUCTA,
sum(iif([Product ID] = 'B',1,0)) as PRODUCTB,
sum(iif([Product ID] <> 'A' and [Product ID] <> 'B',1,0)) AS NONAB
FROM [Orders Detail]
GROUP BY [Order ID];

SELECT Order ID
FROM Temptable
WHERE PRODUCTA > 0 and PRODUCTB > 0 and NONAB = 0
0

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

722 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