如何在此 SQL 存储过程中搜索值对

发布于 2024-10-03 20:51:58 字数 1537 浏览 3 评论 0原文

我正在修改以下 SQL 过程/选择语句。我从之前的一个问题中得出了这个问题,可以在这里引用:需要 SQL 查询的帮助

当我没有条件检查多年的经验时,这非常有效,但我实际上想修改它,以便如果我有一个传入 @csvList 的列表和另一个列表@csvYears,然后我可以将这些值配对 - 也就是说,我可以搜索具有 5 年经验的 Skill1 和具有 3 年经验的 Skill2的经验。

我可以在 csvYears 表上再进行一次 JOIN 操作吗? (SplitCSVStrings 函数返回一个表,并拆分 CSV 值列表)

所以看看现有的代码:

    ALTER PROCEDURE [dbo].[EmployeeQuerySkill]
    @csvList varchar(400),
    @years int,
    @fudge int,
    @hitAll int = 0
AS
BEGIN
  SELECT last_name, id, name, SUM(Experience ) AS 'YearsExperience'
  FROM 
  (
      SELECT e.last_name, e.id, s.name, CASE WHEN r.end_date ='01/01/1901' THEN MAX(datediff(YY,r.start_date, GETDATE())) ELSE MAX(datediff(YY,r.start_date, r.end_date)) END AS 'Experience'
      FROM employee as e
      INNER JOIN project AS p ON e.id = p.employee_id
      INNER JOIN role AS r ON p.id = r.project_id
      INNER JOIN role_skill AS rs ON rs.role_id = r.id
      INNER JOIN skill AS s ON s.id = rs.skill_id
      JOIN SplitCSVStrings(@csvList)            AS CSV ON CSV.val = s.name

     GROUP BY e.last_name, e.id, s.name, r.end_date
  )  table1    
  WHERE table1.Experience >= @years
  GROUP BY last_name, id, name

  END
END

如何修改它以允许我为每种技能搜索不同的条件? @csvList 像“C++, Java”一样出现,我希望 @years"5, 3" 的形式出现以及相应的搜索向我展示拥有 5 年 C++ 经验和 3 年 Java 经验的人。

谢谢!

I have the following SQL procedure/select statement that I am working on modifying. I derived this from a previous question I had which can be referenced here: Need help with SQL query

This works pretty good when I do not have the conditional in there checking for years of experience, but I actually want to modify this so that if I have a list passed in @csvList and another list of @csvYears, then I can pair the values - that is, I can search for Skill1 with 5 years of experience, and Skill2 with 3 years of experience.

Can I just do another JOIN on the table of csvYears? (The SplitCSVStrings function returns a table, and splits the CSV list of values)

So behold the existing code:

    ALTER PROCEDURE [dbo].[EmployeeQuerySkill]
    @csvList varchar(400),
    @years int,
    @fudge int,
    @hitAll int = 0
AS
BEGIN
  SELECT last_name, id, name, SUM(Experience ) AS 'YearsExperience'
  FROM 
  (
      SELECT e.last_name, e.id, s.name, CASE WHEN r.end_date ='01/01/1901' THEN MAX(datediff(YY,r.start_date, GETDATE())) ELSE MAX(datediff(YY,r.start_date, r.end_date)) END AS 'Experience'
      FROM employee as e
      INNER JOIN project AS p ON e.id = p.employee_id
      INNER JOIN role AS r ON p.id = r.project_id
      INNER JOIN role_skill AS rs ON rs.role_id = r.id
      INNER JOIN skill AS s ON s.id = rs.skill_id
      JOIN SplitCSVStrings(@csvList)            AS CSV ON CSV.val = s.name

     GROUP BY e.last_name, e.id, s.name, r.end_date
  )  table1    
  WHERE table1.Experience >= @years
  GROUP BY last_name, id, name

  END
END

How can I modify this to allow me to search for a different condition for each of the skills? @csvList is coming in like "C++, Java" and I want @years to come in as "5, 3" and the corresponding search to show me people that have 5 years of C++ experience, and 3 years of Java experience.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

爱*していゐ 2024-10-10 20:51:58

我认为您需要修改函数 SplitCSVStrings 才能使其工作。您应该这样做,以便它返回的表有一个额外的列指示 csv 中的位置。例如,如果参数是“C++,Java”,结果应该是:

id val
1  C++
2  Java

如果你这样做,那么你可以将你的查询修改为这样:

ALTER PROCEDURE [dbo].[EmployeeQuerySkill]
    @csvList varchar(400),
    @years int,
    @fudge int,
    @hitAll int = 0
AS
BEGIN
  SELECT last_name, id, name, SUM(Experience ) AS 'YearsExperience'
  FROM 
  (
      SELECT e.last_name, e.id, s.name, CASE WHEN r.end_date ='01/01/1901' THEN MAX(datediff(YY,r.start_date, GETDATE())) ELSE MAX(datediff(YY,r.start_date, r.end_date)) END AS 'Experience', CSV2.val Years
      FROM employee as e
      INNER JOIN project AS p ON e.id = p.employee_id
      INNER JOIN role AS r ON p.id = r.project_id
      INNER JOIN role_skill AS rs ON rs.role_id = r.id
      INNER JOIN skill AS s ON s.id = rs.skill_id
      JOIN SplitCSVStrings(@csvList) AS CSV ON CSV.val = s.name
      LEFT JOIN SplitCSVStrings(@years) AS CSV2 ON CSV.id = CSV2.id

     GROUP BY e.last_name, e.id, s.name, r.end_date, CSV2.val
  )  table1    
  WHERE table1.Experience >= years
  GROUP BY last_name, id, name

  END
END

在任何情况下,你应该小心@csvlist中传递的参数和@years是一样的。

I think that you need to modify the function SplitCSVStrings for this to work. Yo should make it so the table that it returns has one extra column indicating the position in the csv. For example, if the parameter is "C++, Java", the result should be:

id val
1  C++
2  Java

If you do that, then you can modify your query to be like this:

ALTER PROCEDURE [dbo].[EmployeeQuerySkill]
    @csvList varchar(400),
    @years int,
    @fudge int,
    @hitAll int = 0
AS
BEGIN
  SELECT last_name, id, name, SUM(Experience ) AS 'YearsExperience'
  FROM 
  (
      SELECT e.last_name, e.id, s.name, CASE WHEN r.end_date ='01/01/1901' THEN MAX(datediff(YY,r.start_date, GETDATE())) ELSE MAX(datediff(YY,r.start_date, r.end_date)) END AS 'Experience', CSV2.val Years
      FROM employee as e
      INNER JOIN project AS p ON e.id = p.employee_id
      INNER JOIN role AS r ON p.id = r.project_id
      INNER JOIN role_skill AS rs ON rs.role_id = r.id
      INNER JOIN skill AS s ON s.id = rs.skill_id
      JOIN SplitCSVStrings(@csvList) AS CSV ON CSV.val = s.name
      LEFT JOIN SplitCSVStrings(@years) AS CSV2 ON CSV.id = CSV2.id

     GROUP BY e.last_name, e.id, s.name, r.end_date, CSV2.val
  )  table1    
  WHERE table1.Experience >= years
  GROUP BY last_name, id, name

  END
END

In any case, you shoud be careful that the parameters passed in both @csvlist AND @years are the same.

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