Я получил следующую таблицу Patients
.
HospitalId INT,
GenderId BIT,
Age TINYINT,
DiseaseId SMALLINT
GenderId
= 0 - это мужчина
GenderId
= 1 - это женщина
Больница A имеетHospitalId
0
В HospitalB есть HospitalId
1
Вот вывод, который я хочу произвести:
DiseaseId | HospitalA_Male_18-30 | HospitalA_Male_31-40 |
---------------------------------------------------------
0 | (count here) | (count here) |
1 | (count here) | (count here) |
2 | (count here) | (count here) |
3 | (count here) | (count here) |
(продолжение столбцов)
HospitalA_Female_18-30 | HospitalA_Female_31-40 |
-------------------------------------------------
(count here) | (count here) |
(count here) | (count here) |
(count here) | (count here) |
(count here) | (count here) |
(продолжение столбцов)
HospitalB_Male_18-30 | HospitalB_Male_31-40 |
---------------------------------------------
(count here) | (count here) |
(count here) | (count here) |
(count here) | (count here) |
(count here) | (count here) |
(продолжение столбцов)
HospitalB_Female_18-30 | HospitalB_Female_31-40 |
-------------------------------------------------
(count here) | (count here) |
(count here) | (count here) |
(count here) | (count here) |
(count here) | (count here) |
(9 столбцов в наборе результатов)
Итак, как вы можете видеть, я на самом деленеобходимо подсчитать, для каждого заболевания, сколько пациентов имеют заболевание в каждой конкретной группе (по больницам, по полу и по возрастным категориям).
Как такое группирование можно сделать (наиболее эффективно) в T-SQL