问题描述
我有以下查询,其结果是员工所说的语言及其相应级别:
I have the following query which gets as result the language spoken by an employee and his corresponding level :
SELECT E.EmployeeId
,ISNULL(L.ID ,0) AS LanguageId
,L.Label AS Language,
ll.Label AS LanguageLevel
FROM Employee e
LEFT JOIN AF_AdminFile aaf ON e.AdminFileId = aaf.AdminFileId
LEFT JOIN AF_Language al ON aaf.AdminFileId = al.AdminFileId
LEFT JOIN Language l ON al.LanguageId = l.ID
LEFT JOIN LanguageLevel ll ON al.LanguageLevelId = ll.LanguageLevelId
ORDER BY e.EmployeeId
结果如下:
对于 EmployeeId=6 的员工,他说英语/流利,西班牙语/好,法语/母语.
知道我的表 Language 中有 187 种不同的语言,我的表 LanguageLevel 中有 4 个语言级别(公平,流利,好,母语)
For the Employee with EmployeeId=6,he speaks English/Fluent, Spanish/Good,French/Mother Tongue.
Knowing that I have 187 different languages in my table Language and 4 language levels in my table LanguageLevel (Fair,Fluent,Good,Mother Tongue)
我只想获得母语和流利的语言,如下所示:
I want to get only the MotherTongue and the Fluent language like below :
EmployeeId MotherTongue Fluent
6 French English
推荐答案
当前查询的输出遵循非规范化键值存储的模式.在这种情况下,键是语言级别,值是语言.处理此问题的一种方法是按员工汇总,然后使用透视逻辑来获取您想要的语言.
The output from your current query follows the pattern of an unnormalized key value store. In this case, the keys are the language levels, and the values the languages. One way to handle this is to aggregate by employee and then use pivoting logic to obtains the languages you want.
SELECT
e.EmployeeId,
MAX(CASE WHEN ll.label = 'Mother Tongue' THEN l.label END) AS MotherTongue,
MAX(CASE WHEN ll.label = 'Fluent' THEN l.label END) AS Fluent
FROM Employee e
LEFT JOIN AF_AdminFile aaf
ON e.AdminFileId = aaf.AdminFileId
LEFT JOIN AF_Language al
ON aaf.AdminFileId = al.AdminFileId
LEFT JOIN Language l
ON al.LanguageId = l.ID
LEFT JOIN LanguageLevel ll
ON al.LanguageLevelId = ll.LanguageLevelId
GROUP BY
e.EmployeeId
ORDER BY
e.EmployeeId;
这篇关于获得员工的母语和流利的语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!