Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 max() ,datediff()

Author  Topic 

sql999
Starting Member

8 Posts

Posted - 2012-10-23 : 23:38:47
here is the question

start-date end-date
2008-01-01 2008-11-21
2000-04-30 2001-10-10
2009-01-09 2009-10-19
.....
how to write the query to get the maximum date difference of the list
Thanks

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-10-24 : 01:57:18
[code]
SELECT TOP 1 WITH TIES *
FROM table
ORDER BY DATEDIFF(dd,Start_date,End_date) DESC
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

sql999
Starting Member

8 Posts

Posted - 2012-10-24 : 19:49:57
I tried it works,
seems order by asc/desc + top can replace max(), min()
thank you.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-10-25 : 00:00:47
quote:
Originally posted by sql999

I tried it works,
seems order by asc/desc + top can replace max(), min()
thank you.


Not always. it depends on scenario

in cases you want max,min out of group you cant implement them using top and order by unless you use a subquery

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

sql999
Starting Member

8 Posts

Posted - 2012-11-16 : 00:58:27
thank for your help
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-11-16 : 22:24:31
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -