sql server - Find maximum and minimum days between several dates in SQL -
i want find max. , min. days between several records in table. example, in following table have max. , min. days due date field each id. i'm using ms-sql 2013
i know there dateiff
finding days between 2 dates now, want find maximum , minimum days between several dates.
id date 10 2016/01/13 10 2016/01/10 10 2016/11/01 10 2015/12/28 11 2015/12/11 11 2016/02/01 11 2015/01/01
now, how can find max. , min. days between dates each id?
can please me have query in sql?
ok, i've re-read answer, looking below;
creating temp table , inserting data;
create table #datatable (id int, date datetime) insert #datatable (id, date) values (10, '2016-01-13') ,(10, '2016-01-10') ,(10, '2016-11-01') ,(10, '2015-12-11') ,(11, '2015-12-11') ,(11, '2016-02-01') ,(11, '2015-01-01')
select statement retrieve data;
declare @startdate datetime; set @startdate = '2015-12-01' declare @enddate datetime; set @enddate = '2016-12-01' select a.id ,min(date) firstdate ,max(date) lastdate ,datediff(day, min(date), max(date)) daydiff #datatable a.date between @startdate , @enddate group a.id
you can remove fields firstdate , lastdate, show dates being compared. you'll return 0 value if there 1 date between variable dates may have account this. idea put check null.
Comments
Post a Comment