我的代码是:
SELECT
Sum(DR.TimeOfWork) "Skutečnost",
SU.name "Jmeno"
FROM DevTimeRecords DR
left join SecurityUsers SU on SU.ID=DR.Worker_ID
left join DevBugs DB on DB.ID=DR.DevDocument_ID
left join DevProducts DPR on DPR.ID=DB.Product_ID
left join DevParts DPA on DPA.ID=DB.Part_ID
where
SU.id = '@{_osoba}'
and DPR.Code = 'ZP'
and DPA.Code='A'
and not(DB.Status_ID IN ('TUPER00101','UUPER00101'))
group by
SU.Name
order by
SU.Name ASC
我需要添加列,其中将计算工作日直到今天。例如:到今天这个列的日期返回数字18(月中27天 - 周末)。 如何添加此列?
答案 0 :(得分:3)
我处理这个问题的方法是简单地利用我创建的“日历”表,其中包含2100年的日期,以及表示工作日为真/假的字段。
技术含量低但效果好 - 您可以将假期标记为工作日,如果您的公司决定更改假期,则更改一个表而不是计算它的所有语句。
答案 1 :(得分:0)
我最近开始使用递归CTE SQL根据需要生成这样的数据。下面的SQL生成一个包含开始日期和结束日期的数据日历。我添加了一个额外的列来指示周末天数,如果日期在current_date之前,它是星期六或星期日。
with recursive calendar (dte,num) as (
select CAST('1/1/2013' /* Start Date */ as date) as dte, CAST(1 as integer) as num from rdb$database
union all
select DATEADD(day,1,calendar.dte) as dte, calendar.num+1 as num from calendar
where DATEADD(day,1,calendar.dte) <= '1/31/2013' /* End Date */
)
select
calendar.*,
EXTRACT(weekday FROM calendar.dte) as wkday,
IIF(calendar.dte <= current_date and EXTRACT(weekday FROM calendar.dte) IN (0,6) /* Sunday & Saturday */,1,0) as wkdaytodate
from calendar
通过一些修改,您可以获得该月的单个结果。
with recursive calendar (dte,num) as (
select CAST('1/1/2013' /* Start Date */ as date) as dte, CAST(1 as integer) as num from rdb$database
union all
select DATEADD(day,1,calendar.dte) as dte, calendar.num+1 as num from calendar
where DATEADD(day,1,calendar.dte) <= '1/31/2013' /* End Date */
)
select
MIN(calendar.dte) as dte,
SUM(IIF(calendar.dte <= current_date and EXTRACT(weekday FROM calendar.dte) IN (0,6) /* Sunday & Saturday */,1,0)) as wkdaytodate
from calendar
如果您需要它更小,您可以简单地将其定义为单列。
select
'test' as text,
(
with recursive calendar (dte,num) as (
select CAST('1/1/2013' /* Start Date */ as date) as dte, CAST(1 as integer) as num from rdb$database
union all
select DATEADD(day,1,calendar.dte) as dte, calendar.num+1 as num from calendar
where DATEADD(day,1,calendar.dte) <= '1/31/2013' /* End Date */
)
select
SUM(IIF(calendar.dte <= current_date and EXTRACT(weekday FROM calendar.dte) IN (0,6) /* Sunday & Saturday */,1,0)) as wkdaytodate
from calendar
) as numofweekenddays
from rdb$database
其他人表示,除了“工作日是星期一至星期五”的简单规则之外,任何事情都应该存放在日历表中,我同意这一点。