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
Microsoft SQL Server
Last Comment
petekipe
8/22/2022 - Mon
Pawan Kumar
Please try this -
SELECT InspectionType.inspection_type_name, --[last_sticker_no] - [next_sticker_no] + 1 AS remaining_stickers, --InspectionStickers.status SUM ( CASE WHEN InspectionStickers.status='C' THEN 1 ELSE 0 END ) UnusedStickersC ,SUM ( CASE WHEN InspectionStickers.status='N' THEN 1 ELSE 0 END ) UnusedStickersNFROM (InspectionStickers INNER JOIN CampaignYear ON InspectionStickers.fk_campaign_year = CampaignYear.pk_campaign_year) INNER JOIN InspectionType ON CampaignYear.fk_inspection_type = InspectionType.pk_inspection_typeWHERE AND InspectionType.pk_inspection_type = 1 AND CampaignYear.pk_campaign_year = 4GROUP BY InspectionType.inspection_type_name
If you face any issues then pls provide some sample data and expected output.
petekipe
ASKER
Thanks for the quick response, but I don't think I made my request clear. Attached are the three tables with sample data:
I'm looking for a result set that would look like this:
For this query, rows in status 'U' are ignored. All but the Annual Inside type involve only a single InspectionStickers status 'C' row, but there are two Annual Inside rows - one in status 'C' (pk 5) and the other status 'N' (pk 7). I need to be able to sum the total of all unused stickers (to_sticker_no - next_sticker_no + 1) in both rows. And I want the result set to look like the four rows shown above. There will only be one row in status 'C' for any inspection type and campaign year, but there can be more than one row is status 'N' for a given inspection type and campaign year.
and here is the query: Images of data are painful, just like images of solutions would be.
PortletPaul
sorry (silly mood); but is there any chance you can just copy/paste some data into code boxes? or, for access, you can upload a db file of just the stuff you care to share.
petekipe
ASKER
Sorry - attached is a stripped-down Access 2000 database containing the three tables and the beginnings of a query.
InspectionStickers primary key 5 shows next_sticker_no of 1253804 and to_sticker_no of 1253950, so 1253950 - 1253804 + 1 = 147.
InspectionStickers primary key 7 shows next_sticker_no of 2590481 and to_sticker_no of 2590580, so 2590580 - 2590481 + 1 = 100.
The total of the two = 247.
If 2 stickers of 10 have been used, the next sticker would be 3 - and to calculate the number remaining (which should be 8), we subtract 3 from 10 and add 1.
Pete
Pawan Kumar
Thank you. Will provide you a query in some time. :)
Just to clarify, InspectionStickers primary keys 5 and 7 have fk_inspection_type and fk_campaign_year in common (1 and 4), and neither is in status 'U', so the remaining counts need to be combined.
Pete
Pawan Kumar
Okies. Got it.
Pawan Kumar
Please try this solution -
SELECT Inspection_type_name , SUM( to_sticker_no - next_sticker_no + 1 ) as SummationStickers FROM inspectionStickers as issINNER JOIN CampaignYear as cy ON iss.fk_CampaignYear = cy.pk_CampaignYear AND iss.fk_Inspection_Type = cy.fk_Inspection_TypeINNER JOIN InspectionType it ON it.pk_Inspection_Type = iss.fk_Inspection_TypeWHERE [Status] <> 'U'GROUP BY Inspection_type_name
I copied your solution into the sample database I attached earlier and ran it - got the error 'Syntax error (missing operator) in query expression ''. I'm running Office 365 installed locally, version 1709 (build 8528.2147).
petekipe
ASKER
I don't think it likes the AND in the first join
Pawan Kumar
Updated..Pls try(as was missing)
SELECT Inspection_type_name , SUM ( to_sticker_no - next_sticker_no + 1 ) as SummationStickers FROM
inspectionStickers as iss
INNER JOIN CampaignYear as cy ON ( iss.fk_CampaignYear = cy.pk_CampaignYear ) AND ( iss.fk_Inspection_Type = cy.fk_Inspection_Type )
INNER JOIN InspectionType as it ON ( it.pk_Inspection_Type = iss.fk_Inspection_Type )
WHERE Status <> 'U'
GROUP BY Inspection_type_name
You had a couple of field names incorrectly spelled, so I fixed them. But, I'm still getting 'Syntax error (missing operator) in query expression '( iss.fk_Campaign...etc.
I tried building the query using Access' Query Design, but Access wants me to group on status, and that doesn't allow 'C' and 'N' rows to be combined. Here's Access' code:
SELECT InspectionType.inspection_type_name, Sum([to_sticker_no]-[next_sticker_no]+1) AS SummationStickersFROM (InspectionStickers INNER JOIN CampaignYear ON InspectionStickers.fk_campaign_year = CampaignYear.pk_campaign_year) INNER JOIN InspectionType ON CampaignYear.fk_inspection_type = InspectionType.pk_inspection_typeGROUP BY InspectionType.inspection_type_name, InspectionStickers.statusHAVING (((InspectionStickers.status)<>'U'));
Open in new window