Solved

How to write joint query?

Posted on 1998-10-30
1
951 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 20 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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
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…

617 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