troubleshooting Question

Need help with tricky SELECT

Avatar of petekipe
petekipeFlag for United States of America asked on
Microsoft SQL Server
20 Comments1 Solution96 ViewsLast Modified:
I need to return the count of remaining items among rows of the same type.  Here's a starting SELECT statement:

SELECT InspectionType.inspection_type_name, 
              [last_sticker_no] - [next_sticker_no] + 1 AS remaining_stickers, 
              InspectionStickers.status
FROM  (InspectionStickers 
INNER JOIN CampaignYear ON InspectionStickers.fk_campaign_year = CampaignYear.pk_campaign_year) 
INNER JOIN InspectionType ON CampaignYear.fk_inspection_type = InspectionType.pk_inspection_type
WHERE InspectionStickers.status='C'
    AND  InspectionType.pk_inspection_type = 1
    AND  CampaignYear.pk_campaign_year = 4

The statement returns the number of unused sticker numbers in each row having a status of 'C' for inspection type 1 by campaign year (4 in this example).

I need to add to that count the number of unused sticker numbers in other rows having a status of 'N' with the same inspection type and campaign year.  I'm sure I need a sub SELECT, but I don't know how to structure it.  Any help would be greatly appreciated!  Pete
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 20 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 20 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros