需要SQL查询的帮助

时间:2010-11-19 15:16:45

标签: sql database

以下是我的表格:

employee
 -id
project
 -id
 -employee_id
role
 -id
 -project_id
 -start_date
 -end_date
role_skill
 -id
 -role_id
 -skill_id

skill
 -id
 -name

所以基本上一个员工拥有多个项目,一个项目拥有多个角色,一个角色拥有多个角色技能,一个角色技能拥有一个技能

我正在尝试查询“具有至少10年经验的技能1和具有至少3年经验的技能2”

如果员工拥有多年的技能经验,则基于员工具备该技能的角色。

查找具有特定技能的员工的查询没有问题,但是一旦我知道他们具备计算经验年数的技能,我该如何“回顾”?

谢谢!

以下是存储过程的示例,其中包含当前代码:

PROCEDURE [dbo].[EmployeeQuerySkill]
    @csvList varchar(400),
    @years int,
    @fudge int,
    @hitAll int = 0
AS
BEGIN

-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

DECLARE @csvLength int
set @csvLength = (select count(*) from SplitCSVStrings(@csvList))

IF @csvLength > 0
BEGIN
    if @hitAll=0 SET @csvLength = 0
    SELECT distinct hits.attribHits as hits, id, last_name+', '+first_name AS emp_name, SUM(CAST(total_experience/12.0 as DECIMAL(5,2))) AS total_exp, labor_grade, title, ptype, @csvList AS pval FROM
        (SELECT e.id, e.last_name, e.first_name, e.labor_grade, e.title, 'skill' AS ptype,
                CASE WHEN r.end_date ='01/01/1901' THEN SUM(datediff(MM,r.start_date, GETDATE())) ELSE SUM(datediff(MM,r.start_date, r.end_date)) END AS total_experience
        FROM [Skillsdb_dev].[dbo].[employee] AS e
        JOIN [Skillsdb_dev].[dbo].[project]             AS prj ON prj.employee_id = e.id
        JOIN [Skillsdb_dev].[dbo].[role]                AS r   ON r.project_id = prj.id
        JOIN [Skillsdb_dev].[dbo].[role_skill]          AS rs  ON rs.role_id = r.id
        JOIN [Skillsdb_dev].[dbo].[skill]               AS s   ON s.id = rs.skill_id
        JOIN SplitCSVStrings(@csvList)            AS CSV ON CSV.val = s.name
        GROUP BY e.id, e.last_name, e.first_name, e.labor_grade, e.title, r.end_date) total_aggregate
    JOIN csvSkillAttribHits(@csvList) AS hits ON hits.empRecId = id
    WHERE total_aggregate.total_experience >= ((@years * 12)-@fudge) AND hits.attribHits >= @csvLength
    GROUP BY id, last_name, first_name, labor_grade, title, ptype,hits.attribHits
    ORDER BY emp_name DESC

END

END

这似乎有效,直到我尝试在那里添加第二个参数,这就是csvList,所以我可以搜索“Java,C ++”例如

3 个答案:

答案 0 :(得分:0)

试试这个,根据你的描述,我认为它应该得到你想要的。如果不是它会让你关闭...

   SELECT e.id, s.name, (r.End_date - r.Start_Date) 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
    WHERE s.id = <whatever skill you want to see>

答案 1 :(得分:0)

我认为你需要对skill_id进行分组,并总结项目结束和开始日期之间的差异。

select e.id, s.name, sum(p.end_date-p.start_date) experience
from employee e
, project p
, role r
, role_skill rs
, skill s
where e.id = p.employee_id
and p.id = r.project_id
and r.id = rs.role_id
and rs.skill_id = s.id
group by e.id, s.name;

答案 2 :(得分:0)

您可以在WHERE子句中使用DATEDIFF函数来检查日期之间的差异是否至少为三年。

“... WHERE DATEDIFF(yy,start_date,end_date)&gt; = 3”