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:

Professional Opinions
Ask a Question
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

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, 
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