I'm using SQL Server. Not sure what version, but it's still supported. I have a table that has 3 columns that I'm interested in. I need to change the column names in a 'hardware' column with the counts for each. Right now I have the following:
SELECT
COUNT(a.EAMacBook13) AS 'MacBook13',
COUNT(a.EAMacBook16) AS 'MacBook16',
COUNT(a.EAStandLaptop) AS 'StandLaptop'
FROM
Employee AS e, EmpAttributes AS a
WHERE
a.EAEmpID = e.EmpID AND e.EmpProjID = 1
where instead I need something like:
Is this possible?
You can do it with a simple union all:
Depending on your RDBMS, you might have access to UNPIVOT which would make this less writing.
You could also use a CTE so that (if your RDBMS supports materializing them) you do less work:
You will have to list out the possible Hardware values unless you use some degree of dynamic SQL.