显示该地区的日期列表

时间:2014-02-28 14:53:17

标签: tsql

也许有人可以帮助我,我选择了:

SELECT  p.pers_name + ' ' + p.pers_lastname ,
        a.hol_from ,
        a.hol_to ,
        ( DATEDIFF(day, a.at_nuo, a.at_iki) ) + 1 AS [Holidays days] ,
        YEAR(a.hol_from) AS Eayrs
FROM    dba.holidays AS a
        INNER JOIN dba.work AS d ON a.work_id = d.work
        INNER JOIN dba.pers AS p ON d.pers_id = p.pers_id
WHERE   p.pers_vard = 'Name'
        AND p.pers_pav = 'Lastname'
ORDER BY Eayrs DESC

此查询结果:

Name Lastname 2014-02-05 00:00:00   2014-02-07 00:00:00 3   2014
Name Lastname 2014-01-20 00:00:00   2014-01-23 00:00:00 4   2014

我有3天假期和4天假期第二次生。我怎么能得到它?

Name Lastname 2014-02-05 00:00:00
Name Lastname 2014-02-06 00:00:00
Name Lastname 2014-02-07 00:00:00
Name Lastname 2014-01-20 00:00:00
Name Lastname 2014-01-21 00:00:00
Name Lastname 2014-01-22 00:00:00
Name Lastname 2014-01-23 00:00:00

1 个答案:

答案 0 :(得分:0)

您可以通过将查询添加到日历表中来实现此目的的一种方法,在该日历表中您拥有所需范围内的所有日期(例如,每个日期365行一个)

SELECT  pers_name ,
        pers_lastname ,
        c.calendardate
FROM    ( SELECT    p.pers_name + ' ' + p.pers_lastname ,
                    a.hol_from ,
                    a.hol_to ,
                    ( DATEDIFF(day, a.at_nuo, a.at_iki) ) + 1 AS [Holidays days] ,
                    YEAR(a.hol_from) AS Eayrs
          FROM      dba.holidays AS a
                    INNER JOIN dba.work AS d ON a.work_id = d.work
                    INNER JOIN dba.pers AS p ON d.pers_id = p.pers_id
          WHERE     p.pers_vard = 'Name'
                    AND p.pers_pav = 'Lastname'
          ORDER BY  Eayrs DESC
        ) t
        INNER JOIN Calendar c ON c.calendardate BETWEEN hol_from AND hol_to