0

This is really bugging me.

Is there any way to name both the column (i.e.: [John]) and what you are counting (i.e. 'Bob') based on the results of another query? That way I don't have to define each one manually?

For example, let's say I have the following query:

SELECT
   COUNT(CASE WHEN [Name] = 'John' THEN 1 ELSE NULL END) AS [John]
  ,COUNT(CASE WHEN [Name] = 'Bob' THEN 1 ELSE NULL END) AS [Bob]
FROM [database].[dbo].[people]
GROUP BY
   COUNT(CASE WHEN [Name] = 'John' THEN 1 ELSE NULL END) AS [John]
  ,COUNT(CASE WHEN [Name] = 'Bob' THEN 1 ELSE NULL END) AS [Bob]

I tried this with a variable, but not having much luck without some type of loop function to loop through assigning variables and definitions right.

Fastidious
  • 1,249
  • 4
  • 25
  • 43

0 Answers0