Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 按月份查找从日期到日期的总天数_Sql Server - Fatal编程技术网

Sql server 按月份查找从日期到日期的总天数

Sql server 按月份查找从日期到日期的总天数,sql-server,Sql Server,我有这种离开的桌子 我想找到总休假天数,月份和年份 请尝试此查询,也将考虑在年末(如2016-1229至2017~01-05 )采取的休假。 欢迎光临。我们喜欢文本格式的表格而不是图片,你能展示一下你的努力吗?你打算如何处理假期跨越多个月的情况?你试过什么了吗?我同意你的看法,但你可能想把它作为评论而不是回答。 create table #Test(id INT IDENTITY(1,1),from_date datetime,to_date datetime) insert into #Te

我有这种离开的桌子

我想找到总休假天数,月份和年份


<什么地方在哪里?

你可能想考虑每天离开自己的条目。使用范围将导致在该范围内计算天数的问题,这些天数不计入假日甚至周末

SELECT SUM(datediff(dd,from_date,to_date)) as  leaves , year(from_date) as [year],month(from_date) as [month] FROM 
(SELECT 1 as Id , 2 as created_by , '2016-11-16' as from_date , '2016-11-17' as to_date
UNION
SELECT 2 as Id , 2 as created_by , '2016-02-10' as from_date , '2016-02-12' as to_date
UNION
SELECT 3 as Id , 2 as created_by , '2016-04-20' as from_date , '2016-04-20' as to_date
UNIOn
SELECT 4 as Id , 2 as created_by , '2016-11-25' as from_date , '2016-11-26' as to_date
UNION
SELECT 5 as Id , 2 as created_by , '2016-11-26' as from_date , '2016-11-28' as to_date
UNION
SELECT 6 as Id , 2 as created_by , '2016-10-10' as from_date , '2016-10-11' as to_date
UNION
SELECT 7 as Id , 2 as created_by , '2016-12-30' as from_date , '2016-12-30' as to_date) a
group by year(from_date),month(from_date),year(to_date),month(to_date)
输出:


如果需要,可以按月份排序,按年排序。

< P>请尝试此查询,也将考虑在年末(如2016-1229至2017~01-05

)采取的休假。
欢迎光临。我们喜欢文本格式的表格而不是图片,你能展示一下你的努力吗?你打算如何处理假期跨越多个月的情况?你试过什么了吗?我同意你的看法,但你可能想把它作为评论而不是回答。
create table #Test(id INT IDENTITY(1,1),from_date datetime,to_date datetime)

insert into #Test values('2016-11-16','2016-11-17')
insert into #Test values('2016-11-22','2016-11-25')
insert into #Test values('2016-09-22','2016-09-25')
insert into #Test values('2016-12-29','2017-01-05')
insert into #Test values('2017-01-08','2017-01-09')

select Sum(Leavestaken) as Leavestaken,LeaveYear,LeaveMonth from(select Sum(DATEDIFF(day,from_date,(case when YEAR(to_date)>YEAR(from_date) then DATEADD(d, -1, DATEADD(m, DATEDIFF(m, 0, from_date) + 1, 0)) else to_date end))+1) Leavestaken,
YEAR(from_date) as LeaveYear,
MONTH(from_date) as LeaveMonth
from #Test
group by YEAR(from_date),MONTH(from_date)
union all
select Sum(DATEDIFF(day,(case when YEAR(to_date)>YEAR(from_date) then DATEADD(month, DATEDIFF(month, 0, to_date), 0) else from_date end),to_date)+1) Leavestaken,
YEAR(to_date) as LeaveYear,
MONTH(to_date) as LeaveMonth
from #Test
where YEAR(from_date)<>YEAR(to_date)
group by YEAR(to_date),MONTH(to_date)) as tbl
group by LeaveYear,LeaveMonth
order by LeaveYear 

--drop table #Test