Consider this:
WITH tots AS(
SELECT
itineraryID,
SUM(CASE WHEN Type = 'Entry Fee' THEN NumberOfPeople END) as E,
SUM(CASE WHEN Type = 'Camping Fee' THEN NumberOfPeople END) as C
FROM
t
GROUP BY itineraryID
)
If we join this back to our table (SELECT * FROM t JOIN tots ON t.itineraryID = tots.itineraryID
) then we can use the E and C values per row to work some things out:
- If E or C is 0 then mark T ("If an itinerary only have entry or camping, then mark T")
- If E = C and it's a Camping row then mark 'T'
- If E = C and it's an Entry row mark 'F'
After this logic is done in a SELECT CASE WHEN, you just need to convert it to an UPDATE JOIN where you modify t
(UPDATE t SET flag = CASE WHEN ... FROM t JOIN tots ...
)
Or you can make a new table with the result of the select (or you can make a view this it and just query it and it will work out the T/F dynamically each time)
NB: Your example data didn't seem to consider what happens if 2 entry and 4 camping are bought.. But it's easy to extend the logic
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…