I have the following table:
maker model type
B 1121 pc
A 1233 pc
E 1260 pc
A 1752 laptop
A 1276 printer
D 1288 printer
I need to receive a result in the form: maker, pc. If a particular maker has models in a given type, I need to concatenate the word ‘yes’ with the number of models in parentheses. Ex. yes(1) for maker ‘A’. So, how can I avoid the following duplication?
CASE
WHEN SELECT COUNT(*) WHERE ... > 0
THEN 'yes(' + CAST((SELECT COUNT(*) WHERE ...) AS varchar) + ')'
This is not a real world problem. I just need to understand how to save a subquery result to use it in a branch statement. The result of this branch statement may contain the subquery result itself.
Creating tables:
Query in easy steps:
Results:
I don’t extend solution concatenating strings because I see that you know how to do it. Be free to change first or second CTE query to match yours requirements.