基于 SQL 条件的数据集
我有一个不是我设计的 SQL Server 数据库。 员工的学位、执照和证书存储在几个不同的表中。 我已经编写了将所有这些信息连接在一起的查询,以便我可以看到数据的总体结果。我被要求为这些数据创建一个视图,该视图仅返回他们获得的最高学位和两个最高认证。 问题是,由于它是预先存在的数据,因此数据中没有内置层次结构。所有学位和证书都简单地存储为与其员工编号关联的字符串。 第一个逻辑步骤是创建一个邻接表(我相信这是正确的术语)。 例如,“MD”是我们列表中您可以获得的最高学位。因此,我给出的“排名”为 1。下一个较低的级别“排名”为 2。等等。 我可以加入包含这些内容的文本字段并返回其关联的排名。 我遇到的问题是根据此排名仅返回两个最高的。 如果员工拥有多个学位或证书,则它们会列在第二行或第三行。从逻辑的角度来看,我需要对员工 ID、名字和姓氏进行分组。然后是如何根据我为他们创建的“排名”来连接学位、证书和执照。从我的角度来看,这并不是一个真正的层次结构,因为我只需要知道最高的两个,而不一定知道结果之间的关系。
另一个潜在的警告是数据库必须保持在 SQL Server 2000 兼容模式。
任何可以提供的帮助将不胜感激。谢谢。
select a.EduRank as 'Licensure Rank',
b.EduRank as 'Degree Rank',
EmpComp.EecEmpNo,
EmpPers.EepNameFirst,
EmpPers.EepNameLast,
RTRIM(EmpEduc.EfeLevel),
RTRIM(EmpLicns.ElcLicenseID),
a.EduType,
b.EduType
from empcomp
join EmpPers on empcomp.eeceeid = EmpPers.eepEEID
join EmpEduc on empcomp.Eeceeid = EmpEduc.EfeEEID
join EmpLicns on empcomp.eeceeid = EmpLicns.ElcEEID
join yvDegreeRanks a on a.EduCode = EmpLicns.ElcLicenseID
join yvDegreeRanks b on b.EduCode = EmpEduc.EfeLevel
I have a SQL Server database that I did not design.
The employees have degrees, licensures and credentials stored in a few different tables.
I have written the query to join all of this information together so I can see an over all result of what the data looks like. I have been asked to create a view for this data that returns only the highest degree they have obtained and the two highest certifications.
The problem is, as it is pre existing data, there is no hierarchy built into the data. All of the degrees and certifications are simply stored as a string associated with their employee number.
The first logical step was to create an adjacency list(I believe this is the correct term).
For example 'MD' is the highest degree you can obtain in our list. So I have given that the "ranking" of 1. The next lower degree is "ranked" as 2. and so forth.
I can join on the text field that contains these and return their associated rank.
The problem I am having is returning only the two highest based on this ranking.
If the employee has multiple degrees or certifications they are listed on a second or third row. From a logical standpoint, I need to group the employee ID, First name and Last name. Then some how concatenate the degrees, certifications and licensures based on the "ranking" I created for them. It is not a true hierarchy in the way that I am thinking about it because I only need to know the highest two and not necessarily the relationship between the results.
Another potential caveat is that the database must remain in SQL Server 2000 compatibility mode.
Any help that can be given would be much appreciated. Thank you.
select a.EduRank as 'Licensure Rank',
b.EduRank as 'Degree Rank',
EmpComp.EecEmpNo,
EmpPers.EepNameFirst,
EmpPers.EepNameLast,
RTRIM(EmpEduc.EfeLevel),
RTRIM(EmpLicns.ElcLicenseID),
a.EduType,
b.EduType
from empcomp
join EmpPers on empcomp.eeceeid = EmpPers.eepEEID
join EmpEduc on empcomp.Eeceeid = EmpEduc.EfeEEID
join EmpLicns on empcomp.eeceeid = EmpLicns.ElcEEID
join yvDegreeRanks a on a.EduCode = EmpLicns.ElcLicenseID
join yvDegreeRanks b on b.EduCode = EmpEduc.EfeLevel
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我想我能明白你的问题是什么 - 但我不确定。将表连接在一起就得到了“双行”。解决此查询的“快速而肮脏”的方法是使用子查询而不是连接。这样做时,您只能选择
TOP 1
学位和TOP 2
认证。编辑:你能尝试这个查询吗?
I think I can see what your problem is - however I'm not sure. Joining the tables together has given you "double rows". The "quick-and-dirty" way to solve this query, would be to use Subqueries other than Joins. Doing so, you can select only the
TOP 1
Degree, andTOP 2
certifications.EDIT : Can you try this query ?
这不是最优雅的解决方案,但希望它至少能以某种方式帮助您。
This is not the most elegant solution, but hopefully it will at least help you out in some way.
拥有员工表、学位类型(包括等级)、证书类型(包括等级)以及连接表员工_学位和员工_证书。 [如果所有其他字段都相同,最好将学位和证书放在一个带有 is_ Degree 标志的表中。] 您可以提取现有的字符串值并将其替换为 FK id 到学位和证书表。
查询本身比较困难,因为 PARTITION BY 在 SQL Server 2000 中不可用(根据 Google 的说法)。 UW 的答案至少有两个问题:您需要 LEFT JOIN ,因为并非所有员工都有学位和证书,并且没有 ORDER BY 来显示您想要获取的内容最好的。
TOP 2
子查询在这种情况下特别难以使用。所以对于这一点,我还不能给出答案。Have tables for employees, types of degrees (including a rank), types of certs (including a rank), and join tables employees_degrees and employees_certs. [It might be better to put degrees and certs in one table with a flag is_degree, if all their other fields are the same.] You can extract the existing string values and replace them with FK ids into the degree and cert tables.
The query itself is harder, because
PARTITION BY
is not available in SQL Server 2000 (according to Google). UW's answer has at least two problems: you needLEFT JOIN
s because not all employees have degrees and certs, and there is noORDER BY
to show what you want to take the best of.TOP 2
subqueries are particularly difficult to use in this context. So for that, I can't yet give an answer.