Для этого можно использовать PIVOT
:
Настройка
DECLARE @data as Table
(
Name varchar(50) not null,
Gender varchar(10) not null,
Job varchar(10) not null
)
INSERT INTO @data (Name, Gender, Job)
VALUES ('John','Male','Accountant'),
('Jane','Female','Accountant'),
('Stephen','Male','Sales'),
('James','Male','Accountant'),
('Hanna','Female','Sales'),
('Joe','Male','Sales'),
('Diana','Female','Sales')
Запрос:
заменить @data ссылкой на таблицу
SELECT Job, Male = SUM(Male), Female = SUM(Female)
FROM @data
PIVOT
(
SUM ( Gender ) FOR
Gender IN ( Male, Female )
) as pvtData
GROUP BY Job