SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 max() ,datediff()
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

sql999
Starting Member

8 Posts

Posted - 10/23/2012 :  23:38:47  Show Profile  Reply with Quote
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

India
52309 Posts

Posted - 10/24/2012 :  01:57:18  Show Profile  Reply with Quote

SELECT TOP 1 WITH TIES *
FROM table
ORDER BY DATEDIFF(dd,Start_date,End_date) DESC


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

Go to Top of Page

sql999
Starting Member

8 Posts

Posted - 10/24/2012 :  19:49:57  Show Profile  Reply with Quote
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

India
52309 Posts

Posted - 10/25/2012 :  00:00:47  Show Profile  Reply with Quote
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 - 11/16/2012 :  00:58:27  Show Profile  Reply with Quote
thank for your help
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52309 Posts

Posted - 11/16/2012 :  22:24:31  Show Profile  Reply with Quote
welcome

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

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000