0
異なる値に応じて数を取る必要があります。私はこれを使用しています。それを簡単にすることはできますか?SQLクエリを簡略化することは可能ですか
SELECT
[FC].[Name] AS [JobCategory],
(SELECT COUNT(*)
FROM [dbo].[JobCandidateHire] AS [JCH]
LEFT JOIN [dbo].[Job] AS [J] ON [J].[ID]=[JCH].[JobID]
LEFT JOIN [dbo].[Candidate] AS [C] ON [C].[ID]=[JCH].[CandidateID]
LEFT JOIN [dbo].[JobCandidateHireLevel] AS [JCHL] ON [JCHL].[ID]=[JCH].[JobCandidateHireLevelID]
WHERE [JCHL].[LevelType]=5 AND [J].[FunctionalCategoryID] =[FC].[ID] AND [C].[EthnicityID]=5 AND [C].[GenderID]=2
) AS [HMale],
(SELECT COUNT(*)
FROM [dbo].[JobCandidateHire] AS [JCH]
LEFT JOIN [dbo].[Job] AS [J] ON [J].[ID]=[JCH].[JobID]
LEFT JOIN [dbo].[Candidate] AS [C] ON [C].[ID]=[JCH].[CandidateID]
LEFT JOIN [dbo].[JobCandidateHireLevel] AS [JCHL] ON [JCHL].[ID]=[JCH].[JobCandidateHireLevelID]
WHERE [JCHL].[LevelType]=5 AND [J].[FunctionalCategoryID] =[FC].[ID] AND [C].[EthnicityID]=2 AND [C].[GenderID]=3
) AS [AmericanIndianFemale],
(SELECT COUNT(*)
FROM [dbo].[JobCandidateHire] AS [JCH]
LEFT JOIN [dbo].[Job] AS [J] ON [J].[ID]=[JCH].[JobID]
LEFT JOIN [dbo].[Candidate] AS [C] ON [C].[ID]=[JCH].[CandidateID]
LEFT JOIN [dbo].[JobCandidateHireLevel] AS [JCHL] ON [JCHL].[ID]=[JCH].[JobCandidateHireLevelID]
WHERE [JCHL].[LevelType]=5 AND [J].[FunctionalCategoryID] =[FC].[ID] AND [C].[EthnicityID]=7 AND [C].[GenderID]=3
) AS [TwoMoreRaceFemale],
(SELECT COUNT(*)
FROM [dbo].[JobCandidateHire] AS [JCH]
LEFT JOIN [dbo].[Job] AS [J] ON [J].[ID]=[JCH].[JobID]
LEFT JOIN [dbo].[Candidate] AS [C] ON [C].[ID]=[JCH].[CandidateID]
LEFT JOIN [dbo].[JobCandidateHireLevel] AS [JCHL] ON [JCHL].[ID]=[JCH].[JobCandidateHireLevelID]
WHERE [JCHL].[LevelType]=5 AND [J].[FunctionalCategoryID] = [FC].[ID]
) AS [CategoryTotal],
[FC].[IsArchived]
FROM [dbo].[FunctionalCategory] AS [FC]
ここでは、条件が異なるだけでサブクエリと同じです。
すべてのサブクエリを削除して、EthnicityIDとGenderID – scsimon