We help IT Professionals succeed at work.
Get Started

SQL Statement Containing Multiple Subqueries for a Single Result Not Working

113 Views
Last Modified: 2017-11-05
I'm back looking for help with another query.  I've attached a sample MS Access database containing Query1.  I'm trying to do something I've never tried before, and I don't know how to make it work.  Here's the query:
SELECT IS1.receipt_no,
        IS1.transmittal_no,
        CY.campaign_year,
        EXISTS(SELECT IS2.receipt_no FROM InspectionStickers AS IS2 WHERE IS2.receipt_no = IS1.receipt_no AND IS2.fk_inspection_type = 1) AS AI,
        EXISTS(SELECT IS2.receipt_no FROM InspectionStickers AS IS2 WHERE IS2.receipt_no = IS1.receipt_no AND IS2.fk_inspection_type = 3) AS AO,
        EXISTS(SELECT IS2.receipt_no FROM InspectionStickers AS IS2 WHERE IS2.receipt_no = IS1.receipt_no AND IS2.fk_inspection_type = 2) AS SI,
        EXISTS(SELECT IS2.receipt_no FROM InspectionStickers AS IS2 WHERE IS2.receipt_no = IS1.receipt_no AND IS2.fk_inspection_type = 4) AS IM,
        IIF((SELECT IS2.receipt_no FROM InspectionStickers AS IS2 WHERE IS2.receipt_no = IS1.receipt_no AND IS2.status <> 'U') = Null, 'Used', IIF((SELECT IS2.receipt_no FROM InspectionStickers AS IS2 WHERE IS2.receipt_no = IS1.receipt_no AND IS2.status <> 'N') = Null, 'New', 'Current')) AS status 
FROM   InspectionStickers AS IS1 
INNER JOIN CampaignYear AS CY ON IS1.fk_campaign_year = CY.pk_campaign_year 
GROUP BY
        IS1.receipt_no,
        IS1.transmittal_no,
        CY.campaign_year 
ORDER BY 1 DESC

Open in new window


My problem is with the last column I'm trying to return, which I can only conceptualize as requiring nested IIF statements containing subqueries.  The error I'm getting is 'At most one record can be returned by this subquery.  My objective is to return 'New' if all rows for a given receipt_no are in status 'N', 'Used' if all rows for a given receipt_no are in status 'U', otherwise 'Current'.  Any help would be greatly appreciated.
Comment
Watch Question
EE Topic Advisor
CERTIFIED EXPERT
Most Valuable Expert 2014
Awarded 2013
Commented:
This problem has been solved!
Unlock 1 Answer and 9 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE