基于 SQL 条件的数据集

发布于 2024-11-08 12:43:08 字数 1116 浏览 0 评论 0原文

我有一个不是我设计的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

柏拉图鍀咏恒 2024-11-15 12:43:08

我想我能明白你的问题是什么 - 但我不确定。将表连接在一起就得到了“双行”。解决此查询的“快速而肮脏”的方法是使用子查询而不是连接。这样做时,您只能选择 TOP 1 学位和 TOP 2 认证。

编辑:你能尝试这个查询吗?

SELECT *
FROM employSELECT tblLicensures.EduRank as 'Licensure Rank',
    tblDegrees.EduRank as 'Degree Rank',
    EmpComp.EecEmpNo, 
    EmpPers.EepNameFirst, 
    EmpPers.EepNameLast, 
    RTRIM(tblDegrees.EfeLevel),
    RTRIM(tblLicensures.ElcLicenseID),
    tblLicensures.EduType,
    tblDegrees.EduType
FROM EmpComp
    LEFT OUTER JOIN EmpPers ON empcom.eeceeid = EmpPers.eepEEID
    LEFT OUTER JOIN
        -- Select TOP 2 Licensure Ranks
        (
            SELECT TOP 2 a.EduType, a.EduRank, EmpLicns.ElcEEID
            FROM yvDegreeRanks a 
                INNER JOIN EmpLicns on a.EduCode = EmpLicns.ElcLicenseID
            WHERE EmpLincs.ElcEEID = empcomp.eeceeid
            ORDER BY a.EduRank ASC
        ) AS tblLicensures ON tblLicensures.ElcEEID = empcomp.Eeceeid
    LEFT OUTER JOIN 
        -- SELECT TOP 1 Degree
        (
            SELECT TOP 1 b.EduType, b.EduRank, EmpEduc.EfeEEID, EmpEduc.EfeLevel
            FROM yvDegreeRanks b 
                INNER JOIN EmpEduc on b.EduCode = EmpEduc.EfeLevel
            WHERE EmpEduc.EfeEEID = empcomp.Eeceeid
            ORDER BY b.EduRank ASC
        ) AS tblDegrees ON tblDegrees.EfeEEID = empcomp.Eeceeid

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, and TOP 2 certifications.

EDIT : Can you try this query ?

SELECT *
FROM employSELECT tblLicensures.EduRank as 'Licensure Rank',
    tblDegrees.EduRank as 'Degree Rank',
    EmpComp.EecEmpNo, 
    EmpPers.EepNameFirst, 
    EmpPers.EepNameLast, 
    RTRIM(tblDegrees.EfeLevel),
    RTRIM(tblLicensures.ElcLicenseID),
    tblLicensures.EduType,
    tblDegrees.EduType
FROM EmpComp
    LEFT OUTER JOIN EmpPers ON empcom.eeceeid = EmpPers.eepEEID
    LEFT OUTER JOIN
        -- Select TOP 2 Licensure Ranks
        (
            SELECT TOP 2 a.EduType, a.EduRank, EmpLicns.ElcEEID
            FROM yvDegreeRanks a 
                INNER JOIN EmpLicns on a.EduCode = EmpLicns.ElcLicenseID
            WHERE EmpLincs.ElcEEID = empcomp.eeceeid
            ORDER BY a.EduRank ASC
        ) AS tblLicensures ON tblLicensures.ElcEEID = empcomp.Eeceeid
    LEFT OUTER JOIN 
        -- SELECT TOP 1 Degree
        (
            SELECT TOP 1 b.EduType, b.EduRank, EmpEduc.EfeEEID, EmpEduc.EfeLevel
            FROM yvDegreeRanks b 
                INNER JOIN EmpEduc on b.EduCode = EmpEduc.EfeLevel
            WHERE EmpEduc.EfeEEID = empcomp.Eeceeid
            ORDER BY b.EduRank ASC
        ) AS tblDegrees ON tblDegrees.EfeEEID = empcomp.Eeceeid
笑看君怀她人 2024-11-15 12:43:08

这不是最优雅的解决方案,但希望它至少能以某种方式帮助您。

create table #dataset (
licensurerank [datatype],
degreerank [datatype],
employeeid [datatype],
firstname varchar,
lastname varchar,
efeLevel  [datatype],
elclicenseid [datatype],
edutype1 [datatype],
edutype2 [datatype]
)

select distinct identity(int,1,1) [ID], EecEmpNo into #employeeList from EmpComp

declare
    @count int,
    @rows int,
    @employeeNo int

select * from #employeeList
set @rows = @@rowcount
set @count = 1

while @count <= @ROWS
    begin

select @employeeNo = EecEmpNo from #employeeList where id = @count

        insert into #dataset
        select top 2 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
            where EmpComp.EecEmpNo = @employeeNo

    set @count = @count + 1
end

This is not the most elegant solution, but hopefully it will at least help you out in some way.

create table #dataset (
licensurerank [datatype],
degreerank [datatype],
employeeid [datatype],
firstname varchar,
lastname varchar,
efeLevel  [datatype],
elclicenseid [datatype],
edutype1 [datatype],
edutype2 [datatype]
)

select distinct identity(int,1,1) [ID], EecEmpNo into #employeeList from EmpComp

declare
    @count int,
    @rows int,
    @employeeNo int

select * from #employeeList
set @rows = @@rowcount
set @count = 1

while @count <= @ROWS
    begin

select @employeeNo = EecEmpNo from #employeeList where id = @count

        insert into #dataset
        select top 2 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
            where EmpComp.EecEmpNo = @employeeNo

    set @count = @count + 1
end
无法言说的痛 2024-11-15 12:43:08

拥有员工表、学位类型(包括等级)、证书类型(包括等级)以及连接表员工_学位和员工_证书。 [如果所有其他字段都相同,最好将学位和证书放在一个带有 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 need LEFT JOINs because not all employees have degrees and certs, and there is no ORDER 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文