<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://sqlblog.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Search results matching tags 'T-SQL' and 'functions'</title><link>http://sqlblog.com/search/SearchResults.aspx?o=DateDescending&amp;tag=T-SQL,functions&amp;orTags=0</link><description>Search results matching tags 'T-SQL' and 'functions'</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP2 (Build: 61129.1)</generator><item><title>Calculating Holidays in SQL</title><link>http://sqlblog.com/blogs/michael_coles/archive/2010/02/25/calculating-holidays-in-sql.aspx</link><pubDate>Thu, 25 Feb 2010 21:51:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:22626</guid><dc:creator>Mike C</dc:creator><description>&lt;P&gt;Ask about&amp;nbsp;nearly any kind of SQL-based date calculation in the &lt;A title="SQL Public Newsgroup" href="http://groups.google.com/group/microsoft.public.sqlserver.programming/topics?hl=en"&gt;newsgroups&lt;/A&gt;, and you'll likely get responses that include use of an &lt;A title="Auxiliary Calendar Table @ ASPFAQ" href="http://sqlserver2000.databases.aspfaq.com/why-should-i-consider-using-an-auxiliary-calendar-table.html"&gt;auxiliary calendar table&lt;/A&gt;.&amp;nbsp; It's a really good idea, and something I highly recommend to anyone who has to do date-based calculations on the server.&amp;nbsp; For those who do dimensional modeling, you'll probably notice the auxiliary calendar table closely resembles a Time dimension with one-day intervals.&amp;nbsp; I won't get into too much detail on it, since ASPFAQ and the newsgroups cover it well.&lt;/P&gt;
&lt;P&gt;What I do want to talk about is calculating holidays, which can be important when populating your auxiliary calendar table.&amp;nbsp; When you create an auxiliary calendar table you may need to perform calculations based on business days, for instance; or you may need to schedule activities before, after, or even during certain holidays.&amp;nbsp; There are two types of holidays that I'll address:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Fixed holidays that fall on the same day every year&lt;/LI&gt;
&lt;LI&gt;Floating holidays for which the date can change from year to year&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;Fixed holidays include holidays that always fall on the same day each year.&amp;nbsp; Some examples of fixed holidays include &lt;A title="Merry Christmas!" href="http://en.wikipedia.org/wiki/Christmas"&gt;Christmas&lt;/A&gt; (always December 25 in the West), &lt;A title="Oh, Canada!" href="http://en.wikipedia.org/wiki/Canada_Day"&gt;Canada Day&lt;/A&gt; (always July 1), &lt;A title="July 4th!" href="http://en.wikipedia.org/wiki/Independence_Day_(United_States)"&gt;U.S. Independence Day&lt;/A&gt; (always July 4).&amp;nbsp; These are fairly easy to detect and set in your auxiliary calendar table.&amp;nbsp; Just look for the static month + day combinations in your table and update it accordingly.&lt;/P&gt;
&lt;P&gt;Floating holidays are far more interesting and difficult to calculate.&amp;nbsp; Take Easter.&amp;nbsp; Easter actually has two definitions.&amp;nbsp; The popular definition is:&amp;nbsp;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;"Easter Day is the first Sunday after the full moon that occurs next after the vernal equinox"&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;This&amp;nbsp;is not the exact ecclesiastical definition though.&amp;nbsp; The full moon here is not the astronomical full moon but an "ecclesiastical moon" that's determined&amp;nbsp;through precalculated lookup tables.&amp;nbsp; The ecclesiastical rules are:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Easter falls on the first Sunday following the first ecclesiastical full moon that occurs on or after the day of the vernal equinox&lt;/LI&gt;
&lt;LI&gt;This particular ecclesiastical full moon is the 14th day of a tabular lunation (new moon)&lt;/LI&gt;
&lt;LI&gt;The vernal equinox is fixed as March 21&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;I'm not going to recount the long and painful history leading to the modern definition of Easter--but the details&amp;nbsp;can be found on &lt;A title="Easter Controversy" href="http://en.wikipedia.org/wiki/Easter_controversy"&gt;Wikipedia&lt;/A&gt;&amp;nbsp;if you're interested.&amp;nbsp; The main point is that Easter calculation is fairly complex.&amp;nbsp; The udf_CalculateEaster function below accepts the year (integer) as a parameter and returns the date of Easter for that year (datetime).&amp;nbsp; Notice the large number of calculations required to make this function work:&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;CREATE FUNCTION dbo.udf_CalculateEaster &lt;BR&gt;(&lt;BR&gt;&lt;/STRONG&gt;&lt;STRONG&gt;&amp;nbsp; @Year INT&lt;BR&gt;)&lt;BR&gt;RETURNS DATETIME&lt;BR&gt;AS&lt;BR&gt;BEGIN&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; DECLARE @Date DATETIME,&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; @c INT, &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; @n INT, &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; @i INT, &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; @k INT, &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; @j INT, &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; @l INT, &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; @m INT, &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; @d INT;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; SELECT @n = @Year - 19 * (@Year / 19),&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; @c = @Year / 100,&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; @k = (@c - 17) / 25,&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; @i = @c - @c / 4 - (@c - @k) / 3 + 19 * @n + 15,&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; @i = @i - 30 * (@i / 30),&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; @i = @i - (@i / 28) * (1 - (@i / 28) * (29 / (@i + 1)) * ((21 - @n) / 11)),&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; @j = @Year + @Year / 4 + @i + 2 - @c + @c / 4,&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; @j = @j - 7 * (@j / 7),&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; @l = @i - @j,&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; @m = 3 + (@l + 40) / 44,&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; @d = @l + 28 - 31 * (@m / 4),&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; @Date = CAST&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; (&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; CAST(@Year AS CHAR(4)) + &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; RIGHT&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; (&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; '0' + CAST&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; (&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; @m AS VARCHAR(2)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ), 2&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ) + &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; RIGHT&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; (&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; '0' +CAST&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; (&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; @d AS VARCHAR(2)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ), 2&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ) AS DATETIME&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; );&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; RETURN @Date;&lt;BR&gt;END;&lt;BR&gt;GO&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;If you want the date for Easter 2010, just run a query like the following:&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;SELECT dbo.udf_CalculateEaster (2010);&lt;BR&gt;GO&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;The result is &lt;EM&gt;2010-04-04&lt;/EM&gt;.&lt;/P&gt;
&lt;P&gt;Another somewhat daunting type of floating holiday is the type that is based on the Nth weekday of the month. These holidays are defined using terms like "the 4th Thursday of November" (&lt;A title="Turkey Day!" href="http://en.wikipedia.org/wiki/Thanksgiving"&gt;U.S. Thanksgiving&lt;/A&gt;), "the&amp;nbsp;3rd&amp;nbsp;Monday in January" (&lt;A title="MLK Day!" href="http://en.wikipedia.org/wiki/Martin_Luther_King_Day"&gt;U.S. Martin Luther King Day&lt;/A&gt;), or even "the *last* Monday in May" (&lt;A title='Formerly "Decoration Day"' href="http://en.wikipedia.org/wiki/Memorial_day"&gt;U.S. Memorial Day&lt;/A&gt;). These floating holidays are not as hard to calculate as Easter, but still a little tricky (particularly Memorial Day).&lt;/P&gt;
&lt;P&gt;To calculate these dates we need a function like udf_nthWeekDay below. This function accepts 4 parameters:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;
&lt;DIV&gt;@n = integer that represents which instance of the weekday you're looking for.&amp;nbsp; This would be the 4 in "4th Thursday&amp;nbsp;in November".&lt;/DIV&gt;&lt;/LI&gt;
&lt;LI&gt;
&lt;DIV&gt;@weekDay = 3-character day of week ("SUN" = Sunday, "MON" = Monday, etc.)&amp;nbsp; I used character abbreviations instead of numbers just to make it a little easier.&lt;/DIV&gt;&lt;/LI&gt;
&lt;LI&gt;
&lt;DIV&gt;@year = integer year for which to calculate&lt;/DIV&gt;&lt;/LI&gt;
&lt;LI&gt;
&lt;DIV&gt;@month = integer month for which to calculate (1 = January, 2 = February, etc.)&lt;/DIV&gt;&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;As you can see below, the Nth weekday calculation is pretty simple.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;CREATE FUNCTION dbo.udf_nthWeekDay&lt;BR&gt;(&lt;BR&gt;&amp;nbsp; @n&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; INT, &lt;BR&gt;&amp;nbsp; @weekDay CHAR(3),&lt;BR&gt;&amp;nbsp; @year&amp;nbsp;&amp;nbsp;&amp;nbsp; INT, &lt;BR&gt;&amp;nbsp; @month&amp;nbsp;&amp;nbsp; INT&lt;BR&gt;)&lt;BR&gt;RETURNS DATETIME&lt;BR&gt;AS&lt;BR&gt;BEGIN&lt;BR&gt;&amp;nbsp; DECLARE @date&amp;nbsp;&amp;nbsp;&amp;nbsp; DATETIME,&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; @dow&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; INT,&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; @offset&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; INT,&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; @wd&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; INT;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;BR&gt;&amp;nbsp; SELECT @wd = CASE @weekDay&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; WHEN 'SUN' THEN 1&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; WHEN 'MON' THEN 2&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; WHEN 'TUE' THEN 3&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; WHEN 'WED' THEN 4&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; WHEN 'THU' THEN 5&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; WHEN 'FRI' THEN 6&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; WHEN 'SAT' THEN 7&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; END,&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; @date = CAST&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; (&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; CAST(@year AS VARCHAR(4)) +&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; RIGHT&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; (&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; '0' + CAST&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; (&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; @month AS VARCHAR(2)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ), 2&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ) +&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; '01' AS DATETIME&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ),&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; @dow = DATEPART(dw, @date),&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; @offset = @wd - @dow,&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; @date = DATEADD(day, @offset + (@n - CASE WHEN @offset &amp;gt;= 0 THEN 1 ELSE 0 END) * 7, @date);&lt;BR&gt;&amp;nbsp; RETURN @date;&lt;BR&gt;END;&lt;BR&gt;GO&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;To get the date for Thanksgiving 2024 you could call the function like this:&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;SELECT dbo.udf_nthWeekDay&lt;BR&gt;&amp;nbsp; (&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; 4, &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; 'THU',&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; 2024, &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; 11&lt;BR&gt;&amp;nbsp; );&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;The result is &lt;EM&gt;2024-11-28&lt;/EM&gt;.&amp;nbsp; Now for a holiday like U.S. Memorial Day ("the last Monday in May") the calculation is a little trickier.&amp;nbsp; You have no idea up front how many Mondays there are in any given May.&amp;nbsp; As an example, in 2010 and 2011 there are 5 Mondays in May; in 2012 and 2013 there are only 4.&amp;nbsp; You could try to figure out the number of Mondays in May of the given year, but that's way too much work.&lt;/P&gt;
&lt;P&gt;There's one thing we know about the last Monday in May with absolute certainty:&amp;nbsp; after the last Monday in May, the next Monday we encounter will *always* be the first Monday in June.&amp;nbsp; So the easier way to figure out the last Monday in May is to calculate the first Monday in June and then back into it (subtract 7 days), like this:&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;SELECT DATEADD&lt;BR&gt;(&lt;BR&gt;&amp;nbsp; day, &lt;BR&gt;&amp;nbsp; -7,&lt;BR&gt;&amp;nbsp; dbo.udf_nthWeekDay&lt;BR&gt;&amp;nbsp; (&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1, &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; 'MON',&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; 2012, &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; 6&lt;BR&gt;&amp;nbsp; )&lt;BR&gt;);&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;By calculating the date of the first Monday in June 2012, we can then subtract 7 days to get the date of the last Monday of May: &lt;EM&gt;2012-05-28&lt;/EM&gt;.&lt;/P&gt;
&lt;P&gt;You can use the functions to calculate holidays for your auxiliary calendar table.&amp;nbsp; I covered U.S. federal (and some other national) holidays in this one, but it can be extended to cover other national, state, and local holidays.&lt;/P&gt;</description></item><item><title>Please Vote: Windowing Enhancements in SQL Server</title><link>http://sqlblog.com/blogs/michael_coles/archive/2009/11/19/please-vote-windowing-enhancements-in-sql-server.aspx</link><pubDate>Fri, 20 Nov 2009 03:45:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:19010</guid><dc:creator>Mike C</dc:creator><description>&lt;P&gt;Back in January 2007, SQL guru &lt;A title="Itzik's Site" href="http://www.solidq.com/insidetsql/"&gt;Itzik Ben-Gan&lt;/A&gt; posted a series of &lt;A title="Itzik's Connect Requests" href="https://connect.microsoft.com/SQLServer/SearchResults.aspx?UserHandle=Itzik+Ben-Gan"&gt;MS Connect enhancement requests&lt;/A&gt; concerning windowing function enhancements.&amp;nbsp; Those who have used the &lt;A title="ROW_NUMBER() in BOL" href="http://msdn.microsoft.com/en-us/library/ms186734.aspx"&gt;ROW_NUMBER()&lt;/A&gt;, &lt;A title="RANK() in BOL" href="http://msdn.microsoft.com/en-us/library/ms176102.aspx"&gt;RANK()&lt;/A&gt;, &lt;A title="DENSE_RANK() in BOL" href="http://msdn.microsoft.com/en-us/library/ms173825.aspx"&gt;DENSE_RANK()&lt;/A&gt;, and &lt;A title="NTILE() at BOL" href="http://msdn.microsoft.com/en-us/library/ms175126.aspx"&gt;NTILE()&lt;/A&gt; functions on SQL 2005 and 2008, you already know how useful they are.&amp;nbsp; They simplify code and can improve performance considerably over the alternatives, which usually include self-joins, temp tables and/or cursors in various combinations.&lt;/P&gt;
&lt;P&gt;Well, the windowing functionality that you've seen in SQL 2005 and 2008 is just the tip of the iceberg.&amp;nbsp; The ISO SQL standard actually defines several additional options for these functions that SQL Server doesn't yet support. These additional options allow you to do some pretty amazing calculations. The ROWS and RANGE window subclauses that the standard defines allows you to perform "sliding window" calculations; the ORDER BY clause for aggregate functions which simplifies complex running sum (and other) calculations.&lt;/P&gt;
&lt;P&gt;As a SQL developer or DBA, these enhancements will simplify your life.&amp;nbsp; But don't take my word for it - read Itzik's white paper at &lt;A href="http://www.insidetsql.com/OVER_Clause_and_Ordered_Calculations.doc"&gt;http://www.insidetsql.com/OVER_Clause_and_Ordered_Calculations.doc&lt;/A&gt;.&amp;nbsp; Then let Microsoft know you want these enhancements in SQL Server by&amp;nbsp;voting on Itzik's enhancement requests at the following links:&lt;/P&gt;
&lt;DIV&gt;Progressive Ordered Calculations: &lt;A href="https://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=254397"&gt;https://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=254397&lt;/A&gt;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;FIRST_VALUE, LAST_VALUE functions: &lt;A href="https://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=254395"&gt;https://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=254395&lt;/A&gt;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;DISTINCT clause for aggregates: &lt;A href="https://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=254393"&gt;https://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=254393&lt;/A&gt;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;ROWS and RANGE window subclauses: &lt;A href="https://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=254392"&gt;https://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=254392&lt;/A&gt;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;Vector expressions: &lt;A href="https://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=254391"&gt;https://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=254391&lt;/A&gt;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;TOP OVER: &lt;A href="https://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=254390"&gt;https://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=254390&lt;/A&gt;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;LAG and LEAD functions: &lt;A href="https://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=254388"&gt;https://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=254388&lt;/A&gt;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;ORDER BY for aggregates:&amp;nbsp;&lt;A href="https://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=254387"&gt;https://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=254387&lt;/A&gt;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;Read Itzik's article, vote on the Connect items, and spread the word!&lt;/DIV&gt;</description></item></channel></rss>