<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>SQL Server Journey with SQL Authority &#187; SQL Scripts</title>
	<atom:link href="http://blog.sqlauthority.com/category/tech/sql-scripts/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.sqlauthority.com</link>
	<description>Personal Notes of Pinal Dave</description>
	<lastBuildDate>Sun, 12 Feb 2012 09:51:07 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='blog.sqlauthority.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://0.gravatar.com/blavatar/08e35387c05b61340e885b1763a69d9f?s=96&#038;d=http%3A%2F%2Fs2.wp.com%2Fi%2Fbuttonw-com.png</url>
		<title>SQL Server Journey with SQL Authority &#187; SQL Scripts</title>
		<link>http://blog.sqlauthority.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://blog.sqlauthority.com/osd.xml" title="SQL Server Journey with SQL Authority" />
	<atom:link rel='hub' href='http://blog.sqlauthority.com/?pushpress=hub'/>
		<item>
		<title>SQL SERVER &#8211; Solution &#8211; A Quick Puzzle on JOIN and NULL &#8211; SQL Brain Teaser</title>
		<link>http://blog.sqlauthority.com/2012/02/11/sql-server-solution-a-quick-puzzle-on-join-and-null-sql-brain-teaser/</link>
		<comments>http://blog.sqlauthority.com/2012/02/11/sql-server-solution-a-quick-puzzle-on-join-and-null-sql-brain-teaser/#comments</comments>
		<pubDate>Sat, 11 Feb 2012 01:30:19 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[PostADay]]></category>
		<category><![CDATA[Readers Question]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Puzzle]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[SQLServer]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=17291</guid>
		<description><![CDATA[Yesterday was really fun. I asked a simple Brain Teaser and we had excellent conversation on SQLAuthority Page as well SQL SERVER – A Quick Puzzle on JOIN and NULL – SQL Brain Teaser. That was an easy puzzle for those who have attended the SQL Server Questions and Answers online course. Here is a quick recap of the puzzle. Lots [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=17291&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">Yesterday was really fun. I asked a simple Brain Teaser and we had excellent conversation on <a href="http://www.facebook.com/SQLAuth" target="_blank"><strong>SQLAuthority Page</strong></a><strong> </strong>as well <a title="Permanent Link to SQL SERVER – A Quick Puzzle on JOIN and NULL – SQL Brain Teaser" href="http://blog.sqlauthority.com/2012/02/10/sql-server-a-quick-puzzle-on-join-and-null-brain-teaser/"><strong>SQL SERVER – A Quick Puzzle on JOIN and NULL – SQL Brain Teaser</strong></a>. That was an easy puzzle for those who have attended the <a href="http://bit.ly/sql-qa" target="_blank"><strong>SQL Server Questions and Answers</strong></a> online course.</p>
<p style="text-align:justify;">Here is a quick recap of the puzzle. Lots of people said it is a very easy puzzle, but the correct answer was provided by only a few readers. There were lots of conversation on Facebook page and lots of emails I received, many saying that there was some sort of an error in the puzzle when there was really none. I am glad that lots of reader have attempted this simple-looking puzzle and learned something for sure.</p>
<p style="text-align:justify;">The puzzle had three parts:</p>
<ol style="text-align:justify;" start="1">
<li>Setup</li>
<li><strong>Fill in the blanks &#8211; T-SQL code</strong></li>
<li>Query and Resultset</li>
</ol>
<div style="text-align:justify;">When query was ran, it was giving following result:</div>
<div style="text-align:justify;">
<p><img src="http://www.pinaldave.com/bimg/puzcurrent.jpg" alt="" width="168" height="115" /></p>
<p>The challenge was to write an additional code and produce the following result:</p>
<p><img src="http://www.pinaldave.com/bimg/puzexpected.jpg" alt="" width="207" height="154" /></p>
<p>Here is an interesting observation in running this puzzle.</p>
</div>
<h2 style="text-align:justify;">Comments Ignored</h2>
<div style="text-align:justify;">This puzzle was extremely simple, but the real complex part was how people comprehend the instruction. It was expected that they do not change any other part of the code, but add T-SQL where there was a commented code. Most of the readers, however, <strong>DID NOT</strong> read the comments properly. They modified the query which was producing the result. The learning is that as a DBA and Developer, one must pay attention to the details and read every instruction.</div>
<h2 style="text-align:justify;">Changed the Final Query</h2>
<div style="text-align:justify;">
<p>Many changed the query which was producing the final result. Well, this was good practice for the brain but would not lead to the intended outcome. Here are a few queries which will produce the desired result. However, the puzzle was to change the data set and not to modify the query provided.</p>
</div>
<h2 style="text-align:justify;">Dropping Unique Constraint</h2>
<p style="text-align:justify;">A few readers sent me email writing that I have problem in the schema of the tables. As they wanted to insert another NULL value in the table, they wanted to suggest that there should not be Unique Constraint on the tables. This way, they can insert another value in the table and produce the desired result. Honestly, the reason why I had put the Unique Constraint there was because I wanted people not to insert NULL value again and produce the result very quickly. It was important to understand how NULL works and how JOIN works with NULL.</p>
<h2 style="text-align:justify;">Correct Answer</h2>
<div style="text-align:justify;">
<p>The answer was very easy and Samarth posted the correct answer a few moments after­ the question was posted.</p>
</div>
<pre style="text-align:justify;">INSERT INTO FirstTable (Col1)
VALUES (1)</pre>
<div style="text-align:justify;">
<p>Just like this puzzle, there are many other learning nuggets in our <a href="http://bit.ly/sql-qa" target="_blank"><strong>SQL Server Questions and Answers</strong></a> course online.</p>
</div>
<h2 style="text-align:justify;">Complete Solution Query</h2>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">TempDB<br />
GO<br />
</span><span style="color:green;">-- Sample Table<br />
</span><span style="color:blue;">CREATE TABLE </span><span style="color:black;">FirstTable </span><span style="color:gray;">(</span><span style="color:black;">Col1 </span><span style="color:blue;">INT</span><span style="color:gray;">)<br />
</span><span style="color:blue;">CREATE TABLE </span><span style="color:black;">SecondTable </span><span style="color:gray;">(</span><span style="color:black;">Col1 </span><span style="color:blue;">INT</span><span style="color:gray;">)<br />
</span><span style="color:black;">GO<br />
</span><span style="color:blue;">ALTER TABLE </span><span style="color:black;">FirstTable </span><span style="color:blue;">ADD CONSTRAINT<br />
</span><span style="color:black;">UX_FirstTable </span><span style="color:blue;">UNIQUE NONCLUSTERED </span><span style="color:gray;">(</span><span style="color:black;">Col1</span><span style="color:gray;">);<br />
</span><span style="color:blue;">ALTER TABLE </span><span style="color:black;">SecondTable </span><span style="color:blue;">ADD CONSTRAINT<br />
</span><span style="color:black;">UX_SecondTable </span><span style="color:blue;">UNIQUE NONCLUSTERED </span><span style="color:gray;">(</span><span style="color:black;">Col1</span><span style="color:gray;">);<br />
</span><span style="color:black;">GO<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">FirstTable </span><span style="color:gray;">(</span><span style="color:black;">Col1</span><span style="color:gray;">)<br />
</span><span style="color:blue;">VALUES </span><span style="color:gray;">(NULL)<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">----------------------------------------------<br />
-- Solution of the puzzle - START - http://wp.me/p2NUQ-4uJ<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">FirstTable </span><span style="color:gray;">(</span><span style="color:black;">Col1</span><span style="color:gray;">)<br />
</span><span style="color:blue;">VALUES </span><span style="color:gray;">(</span><span style="color:black;">1</span><span style="color:gray;">)<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">-- Solution of the puzzle - END<br />
----------------------------------------------<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">SecondTable </span><span style="color:gray;">(</span><span style="color:black;">Col1</span><span style="color:gray;">)<br />
</span><span style="color:blue;">VALUES </span><span style="color:gray;">(NULL)<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">--------------------------------------<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">st.col1<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">FirstTable ft<br />
</span><span style="color:magenta;">LEFT </span><span style="color:blue;">JOIN </span><span style="color:black;">SecondTable st </span><span style="color:blue;">ON </span><span style="color:black;">st.Col1 </span><span style="color:blue;">= </span><span style="color:black;">ft.Col1<br />
GO<br />
</span><span style="color:blue;">DROP TABLE </span><span style="color:black;">FirstTable</span><span style="color:gray;">;<br />
</span><span style="color:blue;">DROP TABLE </span><span style="color:black;">SecondTable</span><span style="color:gray;">;<br />
</span><span style="color:black;">GO</span></code></p>
<p style="text-align:justify;">Reference: <strong>Pinal Dave (<a href="http://blog.sqlauthority.com/" target="_blank">http://blog.sqlauthority.com</a>)</strong></p>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/postaday/'>PostADay</a>, <a href='http://blog.sqlauthority.com/category/readers-question/'>Readers Question</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-puzzle/'>SQL Puzzle</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/tech/sql-scripts/'>SQL Scripts</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/sqlserver/'>SQLServer</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/17291/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/17291/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/17291/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/17291/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/17291/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/17291/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/17291/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/17291/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/17291/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/17291/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/17291/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/17291/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/17291/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/17291/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=17291&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2012/02/11/sql-server-solution-a-quick-puzzle-on-join-and-null-sql-brain-teaser/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/puzcurrent.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/puzexpected.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; A Quick Puzzle on JOIN and NULL &#8211; SQL Brain Teaser</title>
		<link>http://blog.sqlauthority.com/2012/02/10/sql-server-a-quick-puzzle-on-join-and-null-brain-teaser/</link>
		<comments>http://blog.sqlauthority.com/2012/02/10/sql-server-a-quick-puzzle-on-join-and-null-brain-teaser/#comments</comments>
		<pubDate>Fri, 10 Feb 2012 01:30:26 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[PostADay]]></category>
		<category><![CDATA[Readers Question]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Puzzle]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[SQLServer]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=17281</guid>
		<description><![CDATA[It seems that we all love to solve puzzles. On SQLAuthority Page, we have been playing the number game and those who are playing with us know how much fun we are having. Sometimes, the answers are so innovative and informative that they open up those aspects of the technology which I have not thought [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=17281&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">It seems that we all love to solve puzzles. On <a href="http://www.facebook.com/SQLAuth" target="_blank"><strong>SQLAuthority Page</strong></a>, we have been playing the number game and those who are playing with us know how much fun we are having. Sometimes, the answers are so innovative and informative that they open up those aspects of the technology which I have not thought of.</p>
<p style="text-align:justify;">Today, I have a very relaxing puzzle for all of you. It is based on my earlier blog post on <a href="http://blog.sqlauthority.com/2012/02/09/sql-server-inner-join-returning-more-records-than-exists-in-table/" target="_blank"><strong>INNER JOIN and NULL</strong></a>, so I suggest reading the said post first if you want to get the complete idea.</p>
<p style="text-align:justify;">The puzzle has three parts:</p>
<ol style="text-align:justify;" start="1">
<li>Setup</li>
<li><strong>Fill in the blanks &#8211; T-SQL code</strong></li>
<li>Query and Resultset</li>
</ol>
<p style="text-align:justify;">We will set up the sample table first. We will run the sample query, which will give us some result. You will have to figure out what the missing script will be, which in turn will generate a specific displayed result.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">TempDB<br />
GO<br />
</span><span style="color:green;">-- Sample Table<br />
</span><span style="color:blue;">CREATE TABLE </span><span style="color:black;">FirstTable </span><span style="color:gray;">(</span><span style="color:black;">Col1 </span><span style="color:blue;">INT</span><span style="color:gray;">)<br />
</span><span style="color:blue;">CREATE TABLE </span><span style="color:black;">SecondTable </span><span style="color:gray;">(</span><span style="color:black;">Col1 </span><span style="color:blue;">INT</span><span style="color:gray;">)<br />
</span><span style="color:black;">GO<br />
</span><span style="color:blue;">ALTER TABLE </span><span style="color:black;">FirstTable </span><span style="color:blue;">ADD CONSTRAINT<br />
</span><span style="color:black;">UX_FirstTable </span><span style="color:blue;">UNIQUE NONCLUSTERED </span><span style="color:gray;">(</span><span style="color:black;">Col1</span><span style="color:gray;">);<br />
</span><span style="color:blue;">ALTER TABLE </span><span style="color:black;">SecondTable </span><span style="color:blue;">ADD CONSTRAINT<br />
</span><span style="color:black;">UX_SecondTable </span><span style="color:blue;">UNIQUE NONCLUSTERED </span><span style="color:gray;">(</span><span style="color:black;">Col1</span><span style="color:gray;">);<br />
</span><span style="color:black;">GO<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">FirstTable </span><span style="color:gray;">(</span><span style="color:black;">Col1</span><span style="color:gray;">)<br />
</span><span style="color:blue;">VALUES </span><span style="color:gray;">(NULL)<br />
</span><span style="color:black;">GO<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">SecondTable </span><span style="color:gray;">(</span><span style="color:black;">Col1</span><span style="color:gray;">)<br />
</span><span style="color:blue;">VALUES </span><span style="color:gray;">(NULL)<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">--------------------------------------<br />
-- Missing T-SQL Statement<br />
--------------------------------------<br />
-- SQL Query<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">st.col1<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">FirstTable ft<br />
</span><span style="color:magenta;">LEFT </span><span style="color:blue;">JOIN </span><span style="color:black;">SecondTable st </span><span style="color:blue;">ON </span><span style="color:black;">st.Col1 </span><span style="color:blue;">= </span><span style="color:black;">ft.Col1<br />
GO<br />
</span><span style="color:green;">-- Clean up<br />
</span><span style="color:blue;">DROP TABLE </span><span style="color:black;">FirstTable</span><span style="color:gray;">;<br />
</span><span style="color:blue;">DROP TABLE </span><span style="color:black;">SecondTable</span><span style="color:gray;">;<br />
</span><span style="color:black;">GO</span></code></p>
<p style="text-align:justify;">Current Resultset:</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/puzcurrent.jpg" alt="" width="168" height="115" /></p>
<p style="text-align:justify;">Expected Resultset:</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/puzexpected.jpg" alt="" width="207" height="154" /></p>
<p style="text-align:justify;">Try harder. You will figure it out. If you cannot figure out and you want to give up, <a href="http://facebook.com/SQLAuth" target="_blank"><strong>here is the answer</strong></a>.</p>
<p style="text-align:justify;">If you do not feel like doing SQL any further, try the following image of Mars taken by Viking 1 in 1976.</p>
<p style="text-align:justify;">Try to find the human face:</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/facesonmars.gif" alt="" width="255" height="215" /></p>
<p style="text-align:justify;">Do let me know if you like this kind of puzzle and we will have many more of  these.</p>
<p style="text-align:justify;">Reference: <strong>Pinal Dave (<a href="http://blog.sqlauthority.com/" target="_blank">http://blog.sqlauthority.com</a>)</strong></p>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/postaday/'>PostADay</a>, <a href='http://blog.sqlauthority.com/category/readers-question/'>Readers Question</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-puzzle/'>SQL Puzzle</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/tech/sql-scripts/'>SQL Scripts</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/sqlserver/'>SQLServer</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/17281/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/17281/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/17281/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/17281/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/17281/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/17281/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/17281/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/17281/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/17281/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/17281/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/17281/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/17281/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/17281/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/17281/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=17281&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2012/02/10/sql-server-a-quick-puzzle-on-join-and-null-brain-teaser/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/puzcurrent.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/puzexpected.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/facesonmars.gif" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; INNER JOIN Returning More Records than Exists in Table</title>
		<link>http://blog.sqlauthority.com/2012/02/09/sql-server-inner-join-returning-more-records-than-exists-in-table/</link>
		<comments>http://blog.sqlauthority.com/2012/02/09/sql-server-inner-join-returning-more-records-than-exists-in-table/#comments</comments>
		<pubDate>Thu, 09 Feb 2012 01:30:29 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[PostADay]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Joins]]></category>
		<category><![CDATA[SQL Puzzle]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=17261</guid>
		<description><![CDATA[I blog and engage with the community because it gives me satisfaction when someone resolves an issue. A few days ago, I blogged about a DBA who began his first day at a new company and could not find out where the installation summary file was. He was very happy when I featured his story [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=17261&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;"><img class="alignleft" src="http://www.pinaldave.com/bimg/join.jpg" alt="" width="200" height="199" /></p>
<p style="text-align:justify;">I blog and engage with the community because it gives me satisfaction when someone resolves an issue. A few days ago, I blogged about a DBA who began his first day at a new company and could not find out where the <a href="http://blog.sqlauthority.com/2012/02/07/sql-server-installation-log-summary-file-location-2012-2008-r2/" target="_blank">installation summary file</a> was. He was very happy when I featured his story on our blog. Today he asked me another question and when I received his question my first reaction was &#8211; not possible. Later I said, may be possible, and when he shared more information, I said of course it is possible and natural. Let us go over these conversations in the exact order of how they occurred.</p>
<p style="text-align:justify;">(I have modified the emails to chat format as it is easier to understand)</p>
<p style="text-align:justify;"><strong><em>Jeff:</em></strong> <em><span style="color:#ff0000;">Pinal, help needed.</span></em> My query is returning more data than I need.<br />
<strong><em>Pinal:</em></strong> Well, check your WHERE clause, JOINS etc.<br />
<strong><em>Jeff</em></strong>: The problem is that INNER JOIN is returning more records than exist in the table.<br />
<strong><em>Pinal:</em></strong> Not possible. How can a query return data that does not exist. Are you sure that you are connected to the correct database and running the query on the same server where you are validating the data?<br />
<strong><em>Jeff: </em></strong>Yes, yes, everything is in the right place. Can Inner Join ever return more data than exists in the table?<br />
<strong><em>Pinal:</em></strong> Well, maybe. Here is the story. Inner Join can for sure return more records than exists in the table, but it cannot return records that do not exist. Does that make sense?<br />
<strong><em>Jeff:</em></strong> No.<br />
<strong><em>Pinal:</em></strong> Okay, in simple words, if your table has three rows (values 1, 2, 3), your inner join can return 10 rows but it cannot return you the value 4 as part of the result.<br />
<strong><em>Jeff:</em></strong> In simple English please&#8230;<br />
<strong><em>Pinal:</em></strong> Honestly, I cannot make it more simple than what I said just now. Here is the example for you.<br />
<em>(Jeff goes through example and after few minutes)</em><br />
<strong><em>Jeff:</em></strong> Exactly, I had no equal condition which did this. You helped me again.<br />
<strong><em>Pinal:</em></strong> You’re welcome!<br />
(After few minutes&#8230;)<br />
<strong><em>Jeff:</em></strong> <span style="color:#ff0000;"><em>Pinal, help needed&#8230;</em></span></p>
<p style="text-align:justify;">Well, this goes on. Anyway, here is the script that I sent him.</p>
<p style="text-align:justify;">Let us create a scenario. We will create three sample tables and insert values in it.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">TempDB<br />
GO<br />
</span><span style="color:green;">-- Sample Table<br />
</span><span style="color:blue;">CREATE TABLE </span><span style="color:black;">FirstTable </span><span style="color:gray;">(</span><span style="color:black;">Col1 </span><span style="color:blue;">INT</span><span style="color:gray;">)<br />
</span><span style="color:blue;">CREATE TABLE </span><span style="color:black;">SecondTable </span><span style="color:gray;">(</span><span style="color:black;">Col1 </span><span style="color:blue;">INT</span><span style="color:gray;">)<br />
</span><span style="color:blue;">CREATE TABLE </span><span style="color:black;">ThirdTable </span><span style="color:gray;">(</span><span style="color:black;">Col1 </span><span style="color:blue;">INT</span><span style="color:gray;">)<br />
</span><span style="color:black;">GO<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">FirstTable </span><span style="color:gray;">(</span><span style="color:black;">Col1</span><span style="color:gray;">)<br />
</span><span style="color:blue;">VALUES </span><span style="color:gray;">(</span><span style="color:black;">1</span><span style="color:gray;">), (</span><span style="color:black;">2</span><span style="color:gray;">), (</span><span style="color:black;">3</span><span style="color:gray;">), (NULL)<br />
</span><span style="color:black;">GO<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">SecondTable </span><span style="color:gray;">(</span><span style="color:black;">Col1</span><span style="color:gray;">)<br />
</span><span style="color:blue;">VALUES </span><span style="color:gray;">(</span><span style="color:black;">1</span><span style="color:gray;">), (</span><span style="color:black;">2</span><span style="color:gray;">), (</span><span style="color:black;">3</span><span style="color:gray;">), (NULL)<br />
</span><span style="color:black;">GO<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">ThirdTable </span><span style="color:gray;">(</span><span style="color:black;">Col1</span><span style="color:gray;">)<br />
</span><span style="color:blue;">VALUES </span><span style="color:gray;">(</span><span style="color:black;">2</span><span style="color:gray;">), (</span><span style="color:black;">2</span><span style="color:gray;">), (</span><span style="color:black;">2</span><span style="color:gray;">), (</span><span style="color:black;">2</span><span style="color:gray;">), (</span><span style="color:black;">2</span><span style="color:gray;">), (NULL)<br />
</span><span style="color:black;">GO<br />
</span></code></p>
<p style="text-align:justify;">Here you will see the normal use of INNER JOIN where a query returns the results as the number of rows or fewer than the number of rows in the table. This happens because the data is unique in both of the tables.</p>
<h2 style="text-align:justify;">Query 1: Inner Join with Equal to Sign</h2>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:green;">-- Query 1<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">f.Col1 fCol1<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">FirstTable f<br />
</span><span style="color:blue;">INNER JOIN </span><span style="color:black;">SecondTable s </span><span style="color:blue;">ON </span><span style="color:black;">s.Col1 </span><span style="color:blue;">= </span><span style="color:black;">f.col1<br />
GO</span></code></p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/morequeryresult1.jpg" alt="" width="159" height="153" /></p>
<h2 style="text-align:justify;">Query 2: Inner Join with Not Equal to Sign</h2>
<p style="text-align:justify;">Here is a query where the data is still unique (distinct) among tables but the result set contains more records than the numbers of rows the table contains. The condition &lt;&gt; (not equal to) returns the result which was excluded in the query where = (equal to) condition was used.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:green;">-- Query 2<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">f.Col1 fCol1<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">FirstTable f<br />
</span><span style="color:blue;">INNER JOIN </span><span style="color:black;">SecondTable s </span><span style="color:blue;">ON </span><span style="color:black;">s.Col1 </span><span style="color:gray;">&lt;&gt; </span><span style="color:black;">f.col1<br />
GO<br />
</span></code></p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/morequeryresult2.jpg" alt="" width="143" height="228" /></p>
<p style="text-align:justify;">Note: If you combine the data of Query 1 and Query 2, you will get the same result as CROSS JOIN of both the tables. If you want to understand further, read my earlier article <a href="http://blog.sqlauthority.com/2009/04/13/sql-server-introduction-to-joins-basic-of-joins/" target="_blank"><strong>Introduction to Join &#8211; Visual Explanation</strong></a>.</p>
<h2 style="text-align:justify;">Query 3: Inner Join with Non-Distinct Row Table</h2>
<p style="text-align:justify;">Here is a query where data in another table is not unique and INNER JOIN returns more records than one of the tables.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:green;">-- Query 3<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">f.Col1 fCol1<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">FirstTable f<br />
</span><span style="color:blue;">INNER JOIN </span><span style="color:black;">ThirdTable s </span><span style="color:blue;">ON </span><span style="color:black;">s.Col1 </span><span style="color:blue;">= </span><span style="color:black;">f.col1<br />
GO</span></code></p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/morequeryresult3.jpg" alt="" width="145" height="185" /></p>
<h2 style="text-align:justify;">Summary</h2>
<p style="text-align:justify;">Inner Join can for sure return more records than the records of the table. Inner join returns the results based on the condition specified in the JOIN condition. If there are more rows that satisfy the condition (as seen in query 2), it will return you more results. Reference: <a href="http://blog.sqlauthority.com/2009/04/13/sql-server-introduction-to-joins-basic-of-joins/" target="_blank"><strong>Introduction to Join &#8211; Visual Explanation</strong></a>.</p>
<p style="text-align:justify;">Well, feel free to send your questions or even solutions. If I find them interesting I will share them with everybody with due credit.</p>
<p style="text-align:justify;">Reference: <strong>Pinal Dave (<a href="http://blog.sqlauthority.com/" target="_blank">http://blog.sqlauthority.com</a>)</strong></p>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/postaday/'>PostADay</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-joins/'>SQL Joins</a>, <a href='http://blog.sqlauthority.com/category/sql-puzzle/'>SQL Puzzle</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/tech/sql-scripts/'>SQL Scripts</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/17261/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/17261/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/17261/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/17261/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/17261/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/17261/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/17261/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/17261/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/17261/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/17261/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/17261/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/17261/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/17261/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/17261/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=17261&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2012/02/09/sql-server-inner-join-returning-more-records-than-exists-in-table/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/join.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/morequeryresult1.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/morequeryresult2.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/morequeryresult3.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Convert Subquery to CTE &#8211; SQL in Sixty Seconds #001 &#8211; Video</title>
		<link>http://blog.sqlauthority.com/2012/02/08/sql-server-convert-subquery-to-cte-sql-in-sixty-seconds-001-video/</link>
		<comments>http://blog.sqlauthority.com/2012/02/08/sql-server-convert-subquery-to-cte-sql-in-sixty-seconds-001-video/#comments</comments>
		<pubDate>Wed, 08 Feb 2012 01:30:58 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[PostADay]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL in Sixty Seconds]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[SQLServer]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Video]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=17245</guid>
		<description><![CDATA[SQL Server is an ocean of information. I believe if one starts learning today, after 60 years he/she may still be learning the subject (there are always a few exceptions)! Recently, I published the SQL Server Questions and Answers video tutorial, and since the course came out, I have been receiving lots of request to [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=17245&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;"><img class="alignleft" src="http://www.pinaldave.com/bimg/60.jpg" alt="" width="153" height="108" /></p>
<p style="text-align:justify;">SQL Server is an ocean of information. I believe if one starts learning today, after 60 years he/she may still be learning the subject (there are always a few exceptions)! Recently, I published the <a href="http://bit.ly/sql-qa" target="_blank"><strong>SQL Server Questions and Answers</strong></a> video tutorial, and since the course came out, I have been receiving lots of request to share SQL Tips which are small and easy to digest.</p>
<p style="text-align:justify;">While writing the SQL books with my co-authors <a href="http://blogs.extremeexperts.com/" target="_blank">Vinod Kumar</a> and <a href="http://joes2pros.com/" target="_blank">Rick Morelan</a>, we often came across very interesting and useful tips which we believe would be helpful to readers. Sometimes the tips are so small that we could not find an appropriate place for them in the book. We call them <strong><em>Sixty Seconds Tips</em></strong>. The tips are so small that sometimes sixty seconds are too long for them. Keeping the notion of quick and easy tips &#8211; we have decided to come up with new series called “SQL in Sixty Seconds.” Every week we will post one small but useful video tip. We believe that <strong><em>Sixty Seconds </em></strong>is metaphor for<strong><em> quick and easy tip.</em></strong></p>
<p style="text-align:justify;">I have seen many developers staying away from CTE (Common Table Expression), their excuse is that they do not know how to convert subqueries to CTE or they think it is very complicated to do so. In the very first episode of <strong><em>SQL in Sixty Seconds</em></strong> we will see how easily one can convert Subqueries to CTE and CTE to Subqueries.</p>
<p style="text-align:justify;"><span style="text-align:center; display: block;"><a href="http://blog.sqlauthority.com/2012/02/08/sql-server-convert-subquery-to-cte-sql-in-sixty-seconds-001-video/"><img src="http://img.youtube.com/vi/Z7VuYBXX2dg/2.jpg" alt="" /></a></span></p>
<p style="text-align:justify;"><strong>More on CTE:</strong><br />
<a href="http://blog.sqlauthority.com/2008/07/28/sql-server-simple-example-of-recursive-cte/" target="_blank">Simple Example of Recursive CTE</a><br />
<a href="http://blog.sqlauthority.com/2009/08/08/sql-server-multiple-cte-in-one-select-statement-query/" target="_blank">Multiple CTE in One SELECT Statement Query</a><br />
<a href="http://blog.sqlauthority.com/2011/05/10/sql-server-common-table-expression-cte-and-few-observation/" target="_blank">Common Table Expression (CTE) and Few Observation</a><br />
<a href="http://blog.sqlauthority.com/2009/06/23/sql-server-2005-2008-delete-duplicate-rows/" target="_blank">Delete Duplicate Rows</a><br />
<a href="http://blog.sqlauthority.com/2008/07/29/sql-server-sql-server-simple-example-of-recursive-cte-part-2-maxrecursion-prevent-cte-infinite-loop/" target="_blank">Simple Example of Recursive CTE – Part 2 – MAXRECURSION – Prevent CTE Infinite Loop</a><br />
<a href="http://blog.sqlauthority.com/2007/06/11/sql-server-2005-t-sql-paging-query-technique-comparison-over-and-row_number-cte-vs-derived-table/" target="_blank">T-SQL Paging Query Technique Comparison (OVER and ROW_NUMBER()) – CTE vs. Derived Table</a></p>
<p style="text-align:justify;">I encourage you to submit your ideas for <strong><em>SQL in Sixty Seconds</em></strong>. We will try to accommodate as many as we can.</p>
<p style="text-align:justify;">Reference: <strong>Pinal Dave (<a href="http://blog.sqlauthority.com/" target="_blank">http://blog.sqlauthority.com</a>)</strong></p>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/database/'>Database</a>, <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/postaday/'>PostADay</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-in-sixty-seconds/'>SQL in Sixty Seconds</a>, <a href='http://blog.sqlauthority.com/category/tech/sql-scripts/'>SQL Scripts</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/sqlserver/'>SQLServer</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/video/'>Video</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/17245/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/17245/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/17245/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/17245/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/17245/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/17245/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/17245/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/17245/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/17245/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/17245/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/17245/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/17245/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/17245/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/17245/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=17245&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2012/02/08/sql-server-convert-subquery-to-cte-sql-in-sixty-seconds-001-video/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/60.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Finding Count of Logical CPU using T-SQL Script &#8211; Identify Virtual Processors</title>
		<link>http://blog.sqlauthority.com/2012/02/04/sql-server-finding-count-of-logical-cpu-using-t-sql-script-identify-virtual-processors/</link>
		<comments>http://blog.sqlauthority.com/2012/02/04/sql-server-finding-count-of-logical-cpu-using-t-sql-script-identify-virtual-processors/#comments</comments>
		<pubDate>Sat, 04 Feb 2012 01:30:03 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[PostADay]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL DMV]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=17208</guid>
		<description><![CDATA[I recently received email from one of my very close friend from California. His question was very interesting. He wanted to know how many virtual processors are there available for SQL Server. He already had script for SQL Server 2008 but was mainly looking for SQL Server 2000. He made me go to my past. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=17208&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">I recently received email from one of my very close friend from California. His question was very interesting. He wanted to know how many virtual processors are there available for SQL Server. He already had script for SQL Server 2008 but was mainly looking for SQL Server 2000. He made me go to my past. I found following script from my old emails (I have no reference listed along with it, so not sure the original source).</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:green;">-- Identify Virtual Processors in for SQL Server 2005, 2008, 2008R2, 2012<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">cpu_count<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">sys.dm_os_sys_info<br />
GO<br />
</span><span style="color:green;">-- Identify Virtual Processors in for SQL Server 2000<br />
</span><span style="color:blue;">CREATE TABLE </span><span style="color:#434343;">#TempTable<br />
</span><span style="color:gray;">(</span><span style="color:black;">[Index] </span><span style="color:blue;">VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">2000</span><span style="color:gray;">),<br />
</span><span style="color:black;">[Name] </span><span style="color:blue;">VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">2000</span><span style="color:gray;">),<br />
</span><span style="color:black;">[Internal_Value] </span><span style="color:blue;">VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">2000</span><span style="color:gray;">),<br />
</span><span style="color:black;">[Character_Value] </span><span style="color:blue;">VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">2000</span><span style="color:gray;">)) ;<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:#434343;">#TempTable<br />
</span><span style="color:blue;">EXEC </span><span style="color:darkred;">xp_msver</span><span style="color:gray;">;<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">Internal_Value </span><span style="color:blue;">AS </span><span style="color:black;">VirtualCPUCount<br />
</span><span style="color:blue;">FROM </span><span style="color:#434343;">#TempTable<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">Name </span><span style="color:blue;">= </span><span style="color:red;">'ProcessorCount'</span><span style="color:gray;">;<br />
</span><span style="color:blue;">DROP TABLE </span><span style="color:#434343;">#TempTable<br />
</span><span style="color:black;">GO</span></code></p>
<p style="text-align:justify;">Yesterday I shared on <strong><a href="http://www.facebook.com/SQLAuth" target="_blank">facebook page</a></strong> about I am writing this blog post, SQL Server Expert<strong> <a href="http://simranjindal.wordpress.com/" target="_blank">Simran Jindal</a></strong> shared following script which is applicable to SQL Server 2005 and later versions. I just got update from her that this query is of my dear friend and <strong><a href="http://sqlserverperformance.wordpress.com/" target="_blank">SQL Server MVP Glenn Berry</a></strong>. Thanks Glenn.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">SELECT </span><span style="color:black;">cpu_count </span><span style="color:blue;">AS </span><span style="color:black;">[Logical CPU Count]</span><span style="color:gray;">, </span><span style="color:black;">hyperthread_ratio </span><span style="color:blue;">AS </span><span style="color:black;">Hyperthread_Ratio</span><span style="color:gray;">,<br />
</span><span style="color:black;">cpu_count</span><span style="color:gray;">/</span><span style="color:black;">hyperthread_ratio </span><span style="color:blue;">AS </span><span style="color:black;">Physical_CPU_Count</span><span style="color:gray;">,<br />
</span><span style="color:black;">physical_memory_in_bytes</span><span style="color:gray;">/</span><span style="color:black;">1048576 </span><span style="color:blue;">AS </span><span style="color:black;">Physical_Memory_in_MB</span><span style="color:gray;">,<br />
</span><span style="color:black;">sqlserver_start_time</span><span style="color:gray;">, </span><span style="color:black;">affinity_type_desc </span><span style="color:green;">-- (affinity_type_desc is only in 2008 R2)<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">sys.dm_os_sys_info</span></code></p>
<p style="text-align:justify;">If know any other reliable method to get the count of logical CPU, please share that in comment and I will update this blog post with due credit.</p>
<p style="text-align:justify;">Reference: <strong>Pinal Dave (<a href="http://blog.sqlauthority.com/" target="_blank">http://blog.sqlauthority.com</a>)</strong></p>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/technology/postaday/'>PostADay</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-dmv/'>SQL DMV</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/tech/sql-scripts/'>SQL Scripts</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/17208/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/17208/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/17208/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/17208/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/17208/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/17208/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/17208/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/17208/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/17208/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/17208/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/17208/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/17208/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/17208/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/17208/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=17208&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2012/02/04/sql-server-finding-count-of-logical-cpu-using-t-sql-script-identify-virtual-processors/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>
	</item>
		<item>
		<title>SQL SERVER &#8211; A Simple Puzzle and Simple Solution of Datatype and Computed Column</title>
		<link>http://blog.sqlauthority.com/2011/12/24/sql-server-a-simple-puzzle-and-simple-solution-of-datatype-and-computed-column/</link>
		<comments>http://blog.sqlauthority.com/2011/12/24/sql-server-a-simple-puzzle-and-simple-solution-of-datatype-and-computed-column/#comments</comments>
		<pubDate>Sat, 24 Dec 2011 01:30:10 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[PostADay]]></category>
		<category><![CDATA[Readers Contribution]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Puzzle]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=16422</guid>
		<description><![CDATA[Christmas is just near and happy holidays to all of you. Today is Christmas eve and I decided to share something very simple but interesting with you. Recently some one reading my SQL Server Interview Questions and Answers book asked me following question. &#8220;Pinal, Instead of puzzle, or difficult interview question, I was asked following [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=16422&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">Christmas is just near and happy holidays to all of you. Today is Christmas eve and I decided to share something very simple but interesting with you. Recently some one reading my <strong><a href="http://blog.sqlauthority.com/sql-server-books/sql-server-interview-questions-and-answers-for-all-database-developers-and-developers-administrators/" target="_blank">SQL Server Interview Questions and Answers</a></strong> book asked me following question.</p>
<p style="text-align:justify;">&#8220;Pinal,</p>
<p style="text-align:justify;">Instead of puzzle, or difficult interview question, I was asked following riddle in my interview. I could not answer it, do you have any idea.</p>
<p style="text-align:justify;"><em><strong>Riddle: </strong></em><strong>Create a table with two columns but you are allowed to specify datatypes only once. Additionally, write a mechanism that your data is copied from first column to second column without using any update statement. Also the column name should not contain any alphanumeric character. </strong></p>
<p style="text-align:justify;">How do I do this?&#8221;</p>
<p style="text-align:justify;">For a moment I was confused too. As the requirement were very interesting, coping data without using update statement, no use of alphanumeric statement etc.</p>
<p style="text-align:justify;">Finally I came up with following small and quick script.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">CREATE TABLE </span><span style="color:black;">MyTabs </span><span style="color:gray;">(</span><span style="color:black;">[!] </span><span style="color:blue;">INT</span><span style="color:gray;">, </span><span style="color:black;">[!!] </span><span style="color:blue;">AS </span><span style="color:black;">[!]</span><span style="color:gray;">);<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">MyTabs </span><span style="color:blue;">VALUES </span><span style="color:gray;">(</span><span style="color:black;">1</span><span style="color:gray;">);<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">* </span><span style="color:blue;">FROM </span><span style="color:black;">MyTabs</span><span style="color:gray;">;<br />
</span></code></p>
<p style="text-align:justify;">If you are free, can you come up with further optimize script?</p>
<p style="text-align:justify;">Reference: <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>)</strong></p>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/postaday/'>PostADay</a>, <a href='http://blog.sqlauthority.com/category/readers-contribution/'>Readers Contribution</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-puzzle/'>SQL Puzzle</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/tech/sql-scripts/'>SQL Scripts</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/16422/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/16422/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/16422/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/16422/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/16422/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/16422/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/16422/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/16422/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/16422/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/16422/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/16422/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/16422/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/16422/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/16422/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=16422&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2011/12/24/sql-server-a-simple-puzzle-and-simple-solution-of-datatype-and-computed-column/feed/</wfw:commentRss>
		<slash:comments>26</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>
	</item>
		<item>
		<title>SQL SERVER &#8211; A Quick Script for Point in Time Recovery &#8211; Back Up and Restore</title>
		<link>http://blog.sqlauthority.com/2011/12/23/sql-server-a-quick-script-for-point-in-time-recovery-back-up-and-restore/</link>
		<comments>http://blog.sqlauthority.com/2011/12/23/sql-server-a-quick-script-for-point-in-time-recovery-back-up-and-restore/#comments</comments>
		<pubDate>Fri, 23 Dec 2011 01:30:44 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[PostADay]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Backup and Restore]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[SQLServer]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=16413</guid>
		<description><![CDATA[Blogging is like writing a big novel in parts. It has its own mood and it has its own colors. Someday I feel like writing philosophy and some day I like writing theory and some day just a script. Today is one of the day when I just feel like providing working script for user [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=16413&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">Blogging is like writing a big novel in parts. It has its own mood and it has its own colors. Someday I feel like writing philosophy and some day I like writing theory and some day just a script. Today is one of the day when I just feel like providing working script for user requested frequently. Here is one of the script which I refer whenever I faced situation about restoring the database at point in time.</p>
<p style="text-align:justify;">In this demo we will see three step operations:</p>
<ol style="text-align:justify;">
<li>Set up script and backup database</li>
<li>Restore the database in point in time</li>
<li>Clean up database</li>
</ol>
<p style="text-align:justify;">Let us see each step with simple script:</p>
<h2 style="text-align:justify;">1. Set up Script and backup database</h2>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:green;">------------------------------------------------<br />
-- Creating environment<br />
------------------------------------------------<br />
-- Create Database<br />
</span><span style="color:blue;">CREATE DATABASE </span><span style="color:black;">SQLAuthority<br />
GO<br />
</span><span style="color:green;">-- Make sure database is in full recovery<br />
</span><span style="color:blue;">ALTER DATABASE </span><span style="color:black;">SQLAuthority<br />
</span><span style="color:blue;">SET </span><span style="color:black;">RECOVERY </span><span style="color:blue;">FULL<br />
</span><span style="color:black;">GO<br />
</span><span style="color:blue;">USE </span><span style="color:black;">SQLAuthority<br />
GO<br />
</span><span style="color:green;">-- Create Table<br />
</span><span style="color:blue;">CREATE TABLE </span><span style="color:black;">TestTable </span><span style="color:gray;">(</span><span style="color:black;">ID </span><span style="color:blue;">INT</span><span style="color:gray;">)<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">-- Taking full backup<br />
</span><span style="color:blue;">BACKUP DATABASE </span><span style="color:black;">[SQLAuthority]<br />
</span><span style="color:blue;">TO DISK = </span><span style="color:red;">N'D:\SQLAuthority.bak'<br />
</span><span style="color:black;">GO<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">TestTable </span><span style="color:gray;">(</span><span style="color:black;">ID</span><span style="color:gray;">)<br />
</span><span style="color:blue;">VALUES </span><span style="color:gray;">(</span><span style="color:black;">1</span><span style="color:gray;">)<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">-- Selecting the data from TestTable<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">TestTable<br />
GO<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">TestTable </span><span style="color:gray;">(</span><span style="color:black;">ID</span><span style="color:gray;">)<br />
</span><span style="color:blue;">VALUES </span><span style="color:gray;">(</span><span style="color:black;">2</span><span style="color:gray;">)<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">-- Taking log backup<br />
</span><span style="color:blue;">BACKUP </span><span style="color:magenta;">LOG </span><span style="color:black;">[SQLAuthority] </span><span style="color:blue;">TO<br />
DISK = </span><span style="color:red;">N'D:\SQLAuthority1.trn'<br />
</span><span style="color:black;">GO<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">TestTable </span><span style="color:gray;">(</span><span style="color:black;">ID</span><span style="color:gray;">)<br />
</span><span style="color:blue;">VALUES </span><span style="color:gray;">(</span><span style="color:black;">3</span><span style="color:gray;">)<br />
</span><span style="color:black;">GO<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">TestTable </span><span style="color:gray;">(</span><span style="color:black;">ID</span><span style="color:gray;">)<br />
</span><span style="color:blue;">VALUES </span><span style="color:gray;">(</span><span style="color:black;">4</span><span style="color:gray;">)<br />
</span><span style="color:black;">GO<br />
</span><span style="color:blue;">BACKUP </span><span style="color:magenta;">LOG </span><span style="color:black;">[SQLAuthority] </span><span style="color:blue;">TO<br />
DISK = </span><span style="color:red;">N'D:\SQLAuthority2.trn'<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">-- Selecting the data from TestTable<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">TestTable<br />
GO<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">TestTable </span><span style="color:gray;">(</span><span style="color:black;">ID</span><span style="color:gray;">)<br />
</span><span style="color:blue;">VALUES </span><span style="color:gray;">(</span><span style="color:black;">5</span><span style="color:gray;">)<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">-- Selecting the data from TestTable<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">TestTable<br />
GO<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">TestTable </span><span style="color:gray;">(</span><span style="color:black;">ID</span><span style="color:gray;">)<br />
</span><span style="color:blue;">VALUES </span><span style="color:gray;">(</span><span style="color:black;">6</span><span style="color:gray;">)<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">-- Selecting the data from TestTable<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">TestTable<br />
GO<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">TestTable </span><span style="color:gray;">(</span><span style="color:black;">ID</span><span style="color:gray;">)<br />
</span><span style="color:blue;">VALUES </span><span style="color:gray;">(</span><span style="color:black;">7</span><span style="color:gray;">)<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">-- Selecting the data from TestTable<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">TestTable<br />
GO<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">TestTable </span><span style="color:gray;">(</span><span style="color:black;">ID</span><span style="color:gray;">)<br />
</span><span style="color:blue;">VALUES </span><span style="color:gray;">(</span><span style="color:black;">8</span><span style="color:gray;">)<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">-- Marking Time Stamp<br />
</span><span style="color:blue;">SELECT </span><span style="color:magenta;">GETDATE</span><span style="color:gray;">() </span><span style="color:black;">BeforeTruncateTime</span><span style="color:gray;">;<br />
</span><span style="color:green;">-- Selecting the data from TestTable<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">TestTable<br />
GO<br />
</span><span style="color:green;">-- Quick Delay before Truncate<br />
</span><span style="color:blue;">WAITFOR </span><span style="color:black;">DELAY </span><span style="color:red;">'00:00:01'<br />
</span><span style="color:black;">GO<br />
</span><span style="color:blue;">TRUNCATE TABLE </span><span style="color:black;">TestTable<br />
GO<br />
</span><span style="color:green;">-- Quick Delay after Truncate<br />
</span><span style="color:blue;">WAITFOR </span><span style="color:black;">DELAY </span><span style="color:red;">'00:00:01'<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">-- Marking Time Stamp<br />
</span><span style="color:blue;">SELECT </span><span style="color:magenta;">GETDATE</span><span style="color:gray;">() </span><span style="color:black;">AfterTruncateTime</span><span style="color:gray;">;<br />
</span><span style="color:green;">-- Selecting the data from TestTable<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">TestTable<br />
GO<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">TestTable </span><span style="color:gray;">(</span><span style="color:black;">ID</span><span style="color:gray;">)<br />
</span><span style="color:blue;">VALUES </span><span style="color:gray;">(</span><span style="color:black;">9</span><span style="color:gray;">)<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">-- Taking log backup<br />
</span><span style="color:blue;">BACKUP </span><span style="color:magenta;">LOG </span><span style="color:black;">[SQLAuthority] </span><span style="color:blue;">TO<br />
DISK = </span><span style="color:red;">N'D:\SQLAuthority3.trn'<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">-- Marking Time Stamp<br />
</span><span style="color:blue;">SELECT </span><span style="color:magenta;">GETDATE</span><span style="color:gray;">() </span><span style="color:black;">CurrentTime</span><span style="color:gray;">;<br />
</span><span style="color:green;">-- Selecting the data from TestTable<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">TestTable<br />
GO<br />
</span><span style="color:blue;">USE MASTER<br />
</span><span style="color:black;">GO<br />
</span></code></p>
<h2 style="text-align:justify;">2. Restore the database in point in time</h2>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:green;">-----------------------------------------------<br />
-- Restoring Database<br />
------------------------------------------------<br />
</span><span style="color:blue;">USE </span><span style="color:black;">[master]<br />
GO<br />
</span><span style="color:green;">-- Taking tail log<br />
</span><span style="color:blue;">BACKUP </span><span style="color:magenta;">LOG </span><span style="color:black;">[SQLAuthority] </span><span style="color:blue;">TO<br />
DISK = </span><span style="color:red;">N'D:\SQLAuthority5.trn'<br />
</span><span style="color:blue;">WITH </span><span style="color:black;">NORECOVERY<br />
GO<br />
</span><span style="color:green;">-- Restore full backup<br />
</span><span style="color:blue;">RESTORE DATABASE </span><span style="color:black;">[SQLAuthority]<br />
</span><span style="color:blue;">FROM DISK = </span><span style="color:red;">N'D:\SQLAuthority.bak'<br />
</span><span style="color:blue;">WITH<br />
</span><span style="color:black;">STANDBY </span><span style="color:blue;">= </span><span style="color:red;">N'D:\SQLAuthority11.bak'<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">-- Restore transaction backup<br />
</span><span style="color:blue;">RESTORE </span><span style="color:magenta;">LOG </span><span style="color:black;">[SQLAuthority]<br />
</span><span style="color:blue;">FROM DISK = </span><span style="color:red;">N'D:\SQLAuthority1.trn'<br />
</span><span style="color:blue;">WITH </span><span style="color:black;">STANDBY </span><span style="color:blue;">= </span><span style="color:red;">N'D:\SQLAuthority11.trn'<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">-- Selecting the data from TestTable<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">SQLAuthority.dbo.TestTable<br />
GO<br />
</span><span style="color:green;">-- Restore transaction backup<br />
</span><span style="color:blue;">RESTORE </span><span style="color:magenta;">LOG </span><span style="color:black;">[SQLAuthority]<br />
</span><span style="color:blue;">FROM DISK = </span><span style="color:red;">N'D:\SQLAuthority2.trn'<br />
</span><span style="color:blue;">WITH </span><span style="color:black;">STANDBY </span><span style="color:blue;">= </span><span style="color:red;">N'D:\SQLAuthority21.trn'<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">-- Selecting the data from TestTable<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">SQLAuthority.dbo.TestTable<br />
GO<br />
</span><span style="color:green;">-- Restore transaction backup<br />
</span><span style="color:blue;">RESTORE </span><span style="color:magenta;">LOG </span><span style="color:black;">[SQLAuthority]<br />
</span><span style="color:blue;">FROM DISK = </span><span style="color:red;">N'D:\SQLAuthority3.trn'<br />
</span><span style="color:blue;">WITH </span><span style="color:black;">STOPAT </span><span style="color:blue;">= </span><span style="color:red;">'2011-12-21 11:12:18.797'</span><span style="color:gray;">, </span><span style="color:green;">-- Insert Your Time<br />
</span><span style="color:black;">STANDBY </span><span style="color:blue;">= </span><span style="color:red;">N'D:\SQLAuthority33.trn'<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">-- Rolling database forward<br />
</span><span style="color:blue;">RESTORE </span><span style="color:magenta;">LOG </span><span style="color:black;">[SQLAuthority]<br />
</span><span style="color:blue;">WITH </span><span style="color:black;">RECOVERY<br />
GO<br />
</span><span style="color:green;">-- Selecting the data from TestTable<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">SQLAuthority.dbo.TestTable<br />
GO<br />
</span></code></p>
<h2 style="text-align:justify;">3. Clean up database</h2>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:green;">------------------------------------------------<br />
-- Clean up<br />
------------------------------------------------<br />
</span><span style="color:blue;">USE MASTER<br />
</span><span style="color:black;">GO<br />
</span><span style="color:blue;">ALTER DATABASE </span><span style="color:black;">[SQLAuthority]<br />
</span><span style="color:blue;">SET </span><span style="color:black;">SINGLE_USER </span><span style="color:blue;">WITH ROLLBACK IMMEDIATE<br />
</span><span style="color:black;">GO<br />
</span><span style="color:blue;">DROP DATABASE </span><span style="color:black;">[SQLAuthority]<br />
GO</span></code></p>
<p style="text-align:justify;">Few quick points to note: Database has to be either in full recovery or bulk recovery mode. While restoring the database it should be done either WITH NORECOVERY (Leave the database non-operational, and do not roll back the uncommitted transactions. Additional transaction logs can be restored) or WITH STANDBY (Leave the database in read-only mode. Undo uncommitted transactions, but save the undo actions in a standby file so that recovery effects can be reverted).</p>
<p style="text-align:justify;">Lots of scripts today but if you are in need of Point in Time Recovery script, this script is for you. I am open for suggestion, enhancements etc. Let me know your comments.</p>
<p style="text-align:justify;">Reference: <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>)</strong></p>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/postaday/'>PostADay</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-backup-and-restore/'>SQL Backup and Restore</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/tech/sql-scripts/'>SQL Scripts</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/sqlserver/'>SQLServer</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/16413/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/16413/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/16413/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/16413/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/16413/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/16413/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/16413/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/16413/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/16413/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/16413/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/16413/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/16413/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/16413/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/16413/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=16413&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2011/12/23/sql-server-a-quick-script-for-point-in-time-recovery-back-up-and-restore/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>
	</item>
		<item>
		<title>SQL SERVER &#8211; A Quick Look at Performance &#8211; A Quick Look at Configuration</title>
		<link>http://blog.sqlauthority.com/2011/12/13/sql-server-a-quick-look-at-performance-a-quick-look-at-configuration/</link>
		<comments>http://blog.sqlauthority.com/2011/12/13/sql-server-a-quick-look-at-performance-a-quick-look-at-configuration/#comments</comments>
		<pubDate>Tue, 13 Dec 2011 01:30:00 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[PostADay]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Optimization]]></category>
		<category><![CDATA[SQL Performance]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[SQLServer]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=16293</guid>
		<description><![CDATA[This blog post is written in response to the T-SQL Tuesday post of Tips and Tricks. For me, this is a very interesting subject. I perfectly enjoy a discussion when it is about performance tuning. I commonly get follow-up questions regarding this subject, but most of them do not give the complete information about their [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=16293&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;"><img class="alignleft" src="http://www.pinaldave.com/bimg/TSQL2sDay.jpg" alt="" width="123" height="123" /></p>
<p style="text-align:justify;">This blog post is written in response to the T-SQL Tuesday post of <a href="http://sqlblog.com/blogs/allen_white/archive/2011/12/05/t-sql-tuesday-025-invitation-to-share-your-tricks.aspx" target="_blank">Tips and Tricks</a>. For me, this is a very interesting subject. I perfectly enjoy a discussion when it is about performance tuning. I commonly get follow-up questions regarding this subject, but most of them do not give the complete information about their environment.</p>
<p style="text-align:justify;">Whenever I get a question which does not have complete information but is obviously requesting for my help, my initial reaction is to ask more questions. When I ask more details, I usually get more questions from them rather than the details I was asking for. Indeed, this is an endless loop. I prefer to resolve a query or a problem quickly, easily and accurately so that there is no more confusion or further problems in the future.</p>
<p style="text-align:justify;">Here is the method I follow: I send people who request my help a couple of SQL Server scripts and ask them to run these scripts on their system. Once they send me the results,  I would then have a good idea on what the status of their system is.</p>
<p style="text-align:justify;">Here are a couple of scripts which I usually request them to run on their respective machines and get back to me with results in Excel.</p>
<p style="text-align:justify;"><strong>1) System Configuration</strong></p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">sys.configurations<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">name </span><span style="color:blue;">OPTION </span><span style="color:gray;">(</span><span style="color:black;">RECOMPILE</span><span style="color:gray;">);<br />
</span></code></p>
<p style="text-align:justify;"><strong>2) Filename and Paths of Database</strong></p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">SELECT </span><span style="color:magenta;">DB_NAME</span><span style="color:gray;">(</span><span style="color:black;">[database_id]</span><span style="color:gray;">)</span><span style="color:blue;">AS </span><span style="color:black;">[DBName]</span><span style="color:gray;">,<br />
</span><span style="color:black;">name</span><span style="color:gray;">, </span><span style="color:black;">physical_name</span><span style="color:gray;">, </span><span style="color:black;">type_desc</span><span style="color:gray;">, </span><span style="color:black;">state_desc</span><span style="color:gray;">,<br />
</span><span style="color:magenta;">CONVERT</span><span style="color:gray;">( </span><span style="color:black;">bigint</span><span style="color:gray;">, </span><span style="color:black;">size</span><span style="color:gray;">/</span><span style="color:black;">128.0</span><span style="color:gray;">) </span><span style="color:black;">[SizeinMB]<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">sys.master_files<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:magenta;">DB_NAME</span><span style="color:gray;">(</span><span style="color:black;">[database_id]</span><span style="color:gray;">)</span></code></p>
<p style="text-align:justify;"><strong>3) Capturing Wait Types and Wait Stats Information at Interva</strong></p>
<p style="text-align:justify;">You can review the script mentioned in <a href="http://blog.sqlauthority.com/2011/02/05/sql-server-capturing-wait-types-and-wait-stats-information-at-interval-wait-type-day-5-of-28/" target="_blank"><strong>my earlier blog post over here</strong></a>.</p>
<p style="text-align:justify;">Honestly, there is a lot of information one needs to solve a query, but this is how I start and get all the basic information from the questioner. Once I get these, I review the results and continue to ask more questions or help right away if I am able to reach the root cause of the issue.</p>
<p style="text-align:justify;">Reference: <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>)</strong></p>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/postaday/'>PostADay</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-optimization/'>SQL Optimization</a>, <a href='http://blog.sqlauthority.com/category/sql-performance/'>SQL Performance</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/tech/sql-scripts/'>SQL Scripts</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/sqlserver/'>SQLServer</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/16293/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/16293/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/16293/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/16293/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/16293/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/16293/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/16293/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/16293/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/16293/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/16293/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/16293/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/16293/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/16293/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/16293/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=16293&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2011/12/13/sql-server-a-quick-look-at-performance-a-quick-look-at-configuration/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/TSQL2sDay.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Bad Practice of Using Keywords as an Object Name &#8211; Avoid Using Keywords as an Object</title>
		<link>http://blog.sqlauthority.com/2011/12/09/sql-server-bad-practice-of-using-keywords-as-an-object-name-avoid-using-keywords-as-an-object/</link>
		<comments>http://blog.sqlauthority.com/2011/12/09/sql-server-bad-practice-of-using-keywords-as-an-object-name-avoid-using-keywords-as-an-object/#comments</comments>
		<pubDate>Fri, 09 Dec 2011 01:30:41 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[CodeProject]]></category>
		<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[PostADay]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Stored Procedure]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[SQLServer]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=16261</guid>
		<description><![CDATA[Madhivanan is SQL Server MVP and very talented SQL expert. Here is one of the nugget he shared on Just Learned. He shared a tip where there were two interesting point to learn. Do not use keywords as an object name [read DHall's excellent comment below] He has given excellent example how GO can be [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=16261&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Madhivanan is SQL Server MVP and very talented SQL expert. Here is one of the nugget he shared on <a href="http://beyondrelational.com/justlearned/" target="_blank">Just Learned</a>.</p>
<p>He shared a tip where there were two interesting point to learn.</p>
<ol>
<li>Do not use keywords as an object name</li>
<li><strong><a href="http://blog.sqlauthority.com/2011/12/09/sql-server-bad-practice-of-using-keywords-as-an-object-name-avoid-using-keywords-as-an-object/#comment-213797" target="_blank">[read DHall's excellent comment below]</a></strong></li>
</ol>
<p>He has given excellent example how GO can be executed as stored procedure. Here is the extension of the tip. Create a small table and now just hit EXEC GO; and you will notice that there is row in the table.</p>
<p>Create Stored Procedure<br />
<code style="font-size:12px;"><span style="color:blue;">CREATE PROCEDURE </span><span style="color:black;">GO<br />
</span><span style="color:blue;">AS<br />
SELECT </span><span style="color:black;">1 </span><span style="color:blue;">AS </span><span style="color:black;">NUMBER<br />
</span></code><br />
Create Table<br />
<code style="font-size:12px;"><span style="color:blue;">CREATE TABLE </span><span style="color:black;">T1 </span><span style="color:gray;">(</span><span style="color:black;">ID </span><span style="color:blue;">INT</span><span style="color:gray;">)</span></code></p>
<p>Now execute following code<br />
<code style="font-size:12px;"><span style="color:blue;">INSERT INTO </span><span style="color:black;">T1</span><span style="color:gray;">(</span><span style="color:black;">ID</span><span style="color:gray;">)<br />
</span><span style="color:blue;">EXEC </span><span style="color:black;">GO</span><span style="color:gray;">;</span></code></p>
<p>Now when selecting from table it will give us following result:<br />
<code style="font-size:12px;"><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">T1</span></code></p>
<p>Now see following resultset:</p>
<p><img class="alignnone" src="http://www.pinaldave.com/bimg/execgo.jpg" alt="" width="236" height="124" /></p>
<p>So without inserting any data we inserted the data, well indeed good puzzle but very bad practical practice. Every body should be aware of this gotcha and avoid it. Thanks Madhivanan for teaching this interesting learning.</p>
<p>Republishing here with authorization of <a href="http://beyondrelational.com/justlearned/" target="_blank">Just Learned</a>.</p>
<p>Reference: <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>)</strong></p>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/codeproject-2/'>CodeProject</a>, <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/postaday/'>PostADay</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/tech/sql-scripts/'>SQL Scripts</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/sql-stored-procedure/'>SQL Stored Procedure</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/sqlserver/'>SQLServer</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/16261/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/16261/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/16261/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/16261/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/16261/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/16261/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/16261/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/16261/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/16261/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/16261/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/16261/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/16261/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/16261/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/16261/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=16261&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2011/12/09/sql-server-bad-practice-of-using-keywords-as-an-object-name-avoid-using-keywords-as-an-object/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/execgo.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Effect of SET NOCOUNT on @@ROWCOUNT</title>
		<link>http://blog.sqlauthority.com/2011/12/02/sql-server-effect-of-set-no-count-on-rowcount/</link>
		<comments>http://blog.sqlauthority.com/2011/12/02/sql-server-effect-of-set-no-count-on-rowcount/#comments</comments>
		<pubDate>Fri, 02 Dec 2011 01:30:26 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[PostADay]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Function]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=16112</guid>
		<description><![CDATA[Today I had very interesting experience when I was presenting on SQL Server. While I was presenting the session when I ran query SQL Server Management Studio returned message like (8 row(s) affected) and (2 row(s) affected) etc. After a while at one point, I started to prove usage of @@ROWCOUNT function. As I was [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=16112&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">Today I had very interesting experience when I was presenting on SQL Server. While I was presenting the session when I ran query SQL Server Management Studio returned message like (8 row(s) affected) and (2 row(s) affected) etc. After a while at one point, I started to prove usage of @@ROWCOUNT function.</p>
<p style="text-align:justify;">As I was in hurry, I quickly wrote very similar to following example on my screen. (The real query is much complected, but for the purpose of this post I am simplifing it here.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">DECLARE </span><span style="color:#434343;">@table </span><span style="color:blue;">TABLE</span><span style="color:gray;">(</span><span style="color:black;">col1 </span><span style="color:blue;">INT </span><span style="color:#434343;">IDENTITY</span><span style="color:gray;">(</span><span style="color:black;">1</span><span style="color:gray;">,</span><span style="color:black;">1</span><span style="color:gray;">), </span><span style="color:black;">col2 </span><span style="color:blue;">INT</span><span style="color:gray;">)<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:#434343;">@table</span><span style="color:gray;">(</span><span style="color:black;">col2</span><span style="color:gray;">) </span><span style="color:blue;">VALUES </span><span style="color:gray;">(</span><span style="color:black;">2932</span><span style="color:gray;">),(</span><span style="color:black;">142</span><span style="color:gray;">),(</span><span style="color:black;">352</span><span style="color:gray;">),(</span><span style="color:black;">342</span><span style="color:gray;">)<br />
</span><span style="color:blue;">SET NOCOUNT ON<br />
SELECT </span><span style="color:gray;">* </span><span style="color:blue;">FROM </span><span style="color:#434343;">@table<br />
</span><span style="color:blue;">SELECT </span><span style="color:#434343;">@@rowcount<br />
</span><span style="color:blue;">SET NOCOUNT OFF<br />
</span></code></p>
<p style="text-align:justify;">Before I execute the script nearly 10 hands went up in audience. This was bit surprise to me as I do not know why they all got alerted. I assumed that there should be something wrong with either project, screen or my display. I quickly asked to one person what is wrong, he pointed out that I will not get right results in @@ROWCOUNT as I am using immediately before SET NOCOUNT ON. When I asked the reason they told me that because any statement when ran right before @@ROWCOUNT resets its value.</p>
<p style="text-align:justify;">Let us stop here for a second &#8211; <em><strong>do YOU think same way as well?</strong></em></p>
<p style="text-align:justify;">If yes, run above code and check your result in following image.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/rowcountwithnocount.jpg" alt="" width="328" height="287" /></p>
<p style="text-align:justify;">If you believed the same I guess we learned something new. I have no problem confessing I learn something new every single time, I learn something new. However, I was bit surprised with this common myth of SET NOCOUNT ON and @@ROWCOUNT.</p>
<p style="text-align:justify;">Are there any other common myths &#8211; if yes, do share here with your comment. If I think it is interesting, I will publish it this blog with due credit and you can win my new <strong><a href="http://blog.sqlauthority.com/sql-server-books/sql-server-interview-questions-and-answers-for-all-database-developers-and-developers-administrators/" target="_blank">SQL Server Interview Questions and Answers</a></strong> book as well.</p>
<p style="text-align:justify;">Reference: <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>)</strong></p>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/postaday/'>PostADay</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-function/'>SQL Function</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/tech/sql-scripts/'>SQL Scripts</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/16112/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/16112/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/16112/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/16112/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/16112/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/16112/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/16112/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/16112/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/16112/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/16112/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/16112/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/16112/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/16112/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/16112/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=16112&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2011/12/02/sql-server-effect-of-set-no-count-on-rowcount/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/rowcountwithnocount.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Fix: Error: File cannot be loaded because the execution of scripts is disabled on this system. Please see “get-help about_signing” for more details</title>
		<link>http://blog.sqlauthority.com/2011/11/30/sql-server-fix-error-file-cannot-be-loaded-because-the-execution-of-scripts-is-disabled-on-this-system-please-see-get-help-about_signing-for-more-details/</link>
		<comments>http://blog.sqlauthority.com/2011/11/30/sql-server-fix-error-file-cannot-be-loaded-because-the-execution-of-scripts-is-disabled-on-this-system-please-see-get-help-about_signing-for-more-details/#comments</comments>
		<pubDate>Wed, 30 Nov 2011 01:30:17 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[PostADay]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Error Messages]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Powershell]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=16089</guid>
		<description><![CDATA[Yesterday I formatted my computer and did fresh install as it was due from long time. After the fresh install when I tried to install Semantic Search application using powershell, I was stopped by following error. File cannot be loaded because the execution of scripts is disabled on this system. Please see “get-help about_signing” for [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=16089&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">Yesterday I formatted my computer and did fresh install as it was due from long time. After the fresh install when I tried to install Semantic Search application using powershell, I was stopped by following error.</p>
<p style="text-align:justify;">File cannot be loaded because the execution of scripts is disabled on this system. Please see “get-help about_signing” for more details</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/pserror.jpg" alt="" width="501" height="253" /></p>
<p style="text-align:justify;"><strong>Fix/Solution/Workaround:</strong></p>
<p style="text-align:justify;">The solution is very simple. Open the Powershell window and type following two lines and everything will fine right after that.</p>
<pre>Set-ExecutionPolicy Unrestricted
Set-ExecutionPolicy RemoteSigned</pre>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/pserrorsol1.jpg" alt="" width="500" /></p>
<p style="text-align:justify;">Again, this is I have done for my environment where I am very careful what I will run. You can change the policy back to original restricted policy if you want to restrict future execution of the powershell scripts.</p>
<p style="text-align:justify;">Simple &#8211; isn&#8217;t it? Well all complex looking problems are very simple to solve.</p>
<p style="text-align:justify;">Reference: <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>)</strong></p>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/postaday/'>PostADay</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-error-messages/'>SQL Error Messages</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/tech/sql-scripts/'>SQL Scripts</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a> Tagged: <a href='http://blog.sqlauthority.com/tag/powershell/'>Powershell</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/16089/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/16089/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/16089/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/16089/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/16089/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/16089/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/16089/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/16089/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/16089/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/16089/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/16089/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/16089/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/16089/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/16089/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=16089&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2011/11/30/sql-server-fix-error-file-cannot-be-loaded-because-the-execution-of-scripts-is-disabled-on-this-system-please-see-get-help-about_signing-for-more-details/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/pserror.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/pserrorsol1.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Powershell &#8211; Get a List of Fixed Hard Drive and Free Space on Server</title>
		<link>http://blog.sqlauthority.com/2011/11/27/sql-server-powershell-get-a-list-of-fixed-hard-drive-and-free-space-on-server/</link>
		<comments>http://blog.sqlauthority.com/2011/11/27/sql-server-powershell-get-a-list-of-fixed-hard-drive-and-free-space-on-server/#comments</comments>
		<pubDate>Sun, 27 Nov 2011 01:30:46 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[PostADay]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Stored Procedure]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Powershell]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=16062</guid>
		<description><![CDATA[Earlier I have written this article SQL SERVER – Get a List of Fixed Hard Drive and Free Space on Server. I recently received excellent comment by MVP Ravikanth. He demonstrated that how the same can be done using Powershell. It is very sweet and quick solution. Here is the powershell script. Run the same [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=16062&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Earlier I have written this article <strong><a href="blog.sqlauthority.com/2009/08/05/sql-server-get-a-list-of-fixed-hard-drive-and-free-space-on-server/" target="_blank">SQL SERVER – Get a List of Fixed Hard Drive and Free Space on Server</a></strong>. I recently received excellent comment by MVP <strong><a href="http://blog.sqlauthority.com/2009/08/05/sql-server-get-a-list-of-fixed-hard-drive-and-free-space-on-server/#comment-204215" target="_blank">Ravikanth</a></strong>. He demonstrated that how the same can be done using Powershell. It is very sweet and quick solution.</p>
<p>Here is the powershell script. Run the same in your powershell windows.</p>
<pre>Get-WmiObject -Class Win32_LogicalDisk | Select -Property DeviceID, @{Name=’FreeSpaceMB’;Expression={$_.FreeSpace/1MB} } | Format-Table -AutoSize</pre>
<p>Well, I ran this script in my powershell window, it gave me following result &#8211; very accurately and easily.</p>
<p>Get-WmiObject -Class Win32_LogicalDisk | Select -Property DeviceID, @{Name=’FreeSpaceMB’;Expression={$_.FreeSpace/1MB} } | Format-Table -AutoSize</p>
<p><img class="alignnone" src="http://www.pinaldave.com/bimg/pssize.jpg" alt="" width="515" height="234" /></p>
<p>Thanks <strong><a href="http://blog.sqlauthority.com/2009/08/05/sql-server-get-a-list-of-fixed-hard-drive-and-free-space-on-server/#comment-204215" target="_blank">Ravikanth</a></strong> one more time for excellent tip.</p>
<p>Reference: <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>)</strong></p>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/postaday/'>PostADay</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/tech/sql-scripts/'>SQL Scripts</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/sql-stored-procedure/'>SQL Stored Procedure</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a> Tagged: <a href='http://blog.sqlauthority.com/tag/powershell/'>Powershell</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/16062/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/16062/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/16062/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/16062/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/16062/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/16062/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/16062/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/16062/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/16062/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/16062/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/16062/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/16062/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/16062/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/16062/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=16062&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2011/11/27/sql-server-powershell-get-a-list-of-fixed-hard-drive-and-free-space-on-server/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/pssize.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; DVM sys.dm_os_sys_info Column Name Changed in SQL Server 2012</title>
		<link>http://blog.sqlauthority.com/2011/11/25/sql-server-dvm-sys-dm_os_sys_info-column-name-changed-in-sql-server-2012/</link>
		<comments>http://blog.sqlauthority.com/2011/11/25/sql-server-dvm-sys-dm_os_sys_info-column-name-changed-in-sql-server-2012/#comments</comments>
		<pubDate>Fri, 25 Nov 2011 01:30:02 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[PostADay]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL DMV]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=16034</guid>
		<description><![CDATA[Have you ever faced situation where something does not work? When you try to fix it ‑ you enjoy fixing it and started to appreciate the breaking changes. Well, this is exactly I felt yesterday. Before I begin my story, I want to candidly state that I do not encourage anybody to use * in [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=16034&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">Have you ever faced situation where something does not work? When you try to fix it ‑ you enjoy fixing it and started to appreciate the breaking changes. Well, this is exactly I felt yesterday. Before I begin my story, I want to candidly state that I do not encourage anybody to use * in the SELECT statement.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/deleteerase.gif" alt="" width="400" height="400" /></p>
<p style="text-align:justify;">One of the my DBA friends, who always used my performance tuning script, sent me an email yesterday with the following question -</p>
<p style="text-align:justify;">&#8220;Every time I want to retrieve OS related information in SQL Server, I use DMV sys.dm_os_sys_info. I just upgraded my SQL Server edition from 2008 R2 to SQL Server 2012 RC0, and it suddenly stopped working. Well, this is not the production server; so the issue is not big yet – but, eventually I need to resolve this error. Any suggestion?&#8221;</p>
<p style="text-align:justify;">The funny thing about this was that the original email was very long, but it did not talk about what the exact error is besides that the query is not working. I think this is the disadvantage of being too friendly on email sometimes. Well, nevertheless, I quickly looked at the DMV on my SQL Server 2008 R2 and SQL Server 2012 RC0 version.</p>
<p style="text-align:justify;">To my surprise, I found out that there were few columns that are renamed in SQL Server 2012 RC0. Usually, when people see breaking changes, they do not like it; but when I see these changes, I was happy as new names were meaningful, and additionally, their new conversion is much more practical and useful.</p>
<p style="text-align:justify;">Here are the columns’ previous names:</p>
<p>&nbsp;</p>
<table border="1">
<tbody>
<tr>
<td><strong>Previous Column Name</strong></td>
<td><strong>New Column Name</strong></td>
</tr>
<tr>
<td>physical_memory_in_bytes</td>
<td>physical_memory_kb</td>
</tr>
<tr>
<td>bpool_commit_target</td>
<td>committed_target_kb</td>
</tr>
<tr>
<td>bpool_visible</td>
<td>visible_target_kb</td>
</tr>
<tr>
<td>virtual_memory_in_bytes</td>
<td>virtual_memory_kb</td>
</tr>
<tr>
<td>bpool_commited</td>
<td>committed_kb</td>
</tr>
</tbody>
</table>
<p style="text-align:justify;">If you read it carefully, then you will notice that new columns now display few results in the kb, whereas earlier results were in bytes. When I see the results in bytes, I always get confused as I cannot guess what exactly it will convert into. I like to see results in kb, and I am glad that new columns are now displaying the results in kb.</p>
<p style="text-align:justify;">I sent the details of the new columns to my friend and ask him to check the columns used in application. From my comment, he immediately realized why he was facing such an error and fixed it.</p>
<p style="text-align:justify;">Overall, all is well at the end, and I learned something new.</p>
<p style="text-align:justify;">Reference: <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>)</strong></p>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/postaday/'>PostADay</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-dmv/'>SQL DMV</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/tech/sql-scripts/'>SQL Scripts</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/16034/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/16034/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/16034/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/16034/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/16034/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/16034/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/16034/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/16034/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/16034/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/16034/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/16034/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/16034/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/16034/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/16034/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=16034&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2011/11/25/sql-server-dvm-sys-dm_os_sys_info-column-name-changed-in-sql-server-2012/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/deleteerase.gif" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Solution to Puzzle &#8211; Simulate LEAD() and LAG() without Using SQL Server 2012 Analytic Function</title>
		<link>http://blog.sqlauthority.com/2011/11/24/sql-server-solution-to-puzzle-simulate-lead-and-lag-without-using-sql-server-2012-analytic-function/</link>
		<comments>http://blog.sqlauthority.com/2011/11/24/sql-server-solution-to-puzzle-simulate-lead-and-lag-without-using-sql-server-2012-analytic-function/#comments</comments>
		<pubDate>Thu, 24 Nov 2011 01:30:36 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[PostADay]]></category>
		<category><![CDATA[Readers Contribution]]></category>
		<category><![CDATA[Readers Question]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Function]]></category>
		<category><![CDATA[SQL Puzzle]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=16017</guid>
		<description><![CDATA[Earlier I wrote a series on SQL Server Analytic Functions of SQL Server 2012. During the series to keep the learning maximum and having fun, we had few puzzles. One of the puzzle was simulating LEAD() and LAG() without using SQL Server 2012 Analytic Function. Please read the puzzle here first before reading the solution [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=16017&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">Earlier I wrote a series on <strong><a href="http://blog.sqlauthority.com/2011/11/23/sql-server-2012-summary-of-all-the-analytic-functions-msdn-and-sqlauthority/" target="_blank">SQL Server Analytic Functions of SQL Server 2012</a></strong>. During the series to keep the learning maximum and having fun, we had few puzzles. One of the puzzle was simulating LEAD() and LAG() without using SQL Server 2012 Analytic Function.</p>
<p style="text-align:justify;">Please read the puzzle here first before reading the solution : <strong><a href="http://blog.sqlauthority.com/2011/11/16/sql-server-puzzle-to-win-print-book-write-t-sql-self-join-without-using-first-_value-and-last_value/" target="_blank">Write T-SQL Self Join Without Using LEAD and LAG</a></strong>. When I was originally wrote the puzzle I had done small blunder and the question was a bit confusing which I corrected later on but wrote a <strong><a href="http://blog.sqlauthority.com/2011/11/17/sql-server-puzzle-to-win-print-book-and-free-30-days-online-training-material/" target="_blank">follow up blog post</a></strong> on over here where I describe the give-away.</p>
<h2 style="text-align:justify;">Quick Recap:</h2>
<p style="text-align:justify;">Generate following results without using SQL Server 2012 analytic functions.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/leadlag1.jpg" alt="" width="514" height="262" /></p>
<p style="text-align:justify;">I had received so many valid answers. Some answers were similar to other and some were very innovative. Some answers were very adaptive and some did not work when I changed where condition. After selecting all the valid answer, I put them in table and ran RANDOM function on the same and selected winners. Here are the valid answers.</p>
<h2 style="text-align:justify;">No Joins and No Analytic Functions</h2>
<p style="text-align:justify;">Excellent Solution by <a href="http://blog.sqlauthority.com/2011/11/16/sql-server-puzzle-to-win-print-book-write-t-sql-self-join-without-using-first-_value-and-last_value/#comment-197710" target="_blank">Geri Reshef</a> &#8211; Winner of <strong>SQL Server Interview Questions and Answers (<a href="http://bit.ly/indiasqlinterview" target="_blank">India </a>| <a href="http://amzn.to/qTOXoG" target="_blank">USA</a>)</strong></p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">WITH </span><span style="color:black;">T1 </span><span style="color:blue;">AS<br />
</span><span style="color:gray;">(</span><span style="color:blue;">SELECT </span><span style="color:black;">Row_Number</span><span style="color:gray;">() </span><span style="color:blue;">OVER</span><span style="color:gray;">(</span><span style="color:blue;">ORDER BY </span><span style="color:black;">SalesOrderDetailID</span><span style="color:gray;">) </span><span style="color:black;">N</span><span style="color:gray;">,<br />
</span><span style="color:black;">s.SalesOrderID</span><span style="color:gray;">,<br />
</span><span style="color:black;">s.SalesOrderDetailID</span><span style="color:gray;">,<br />
</span><span style="color:black;">s.OrderQty<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Sales.SalesOrderDetail s<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">SalesOrderID </span><span style="color:blue;">IN </span><span style="color:gray;">(</span><span style="color:black;">43670</span><span style="color:gray;">, </span><span style="color:black;">43669</span><span style="color:gray;">, </span><span style="color:black;">43667</span><span style="color:gray;">, </span><span style="color:black;">43663</span><span style="color:gray;">))<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">SalesOrderID</span><span style="color:gray;">,</span><span style="color:black;">SalesOrderDetailID</span><span style="color:gray;">,</span><span style="color:black;">OrderQty</span><span style="color:gray;">,<br />
</span><span style="color:magenta;">CASE </span><span style="color:blue;">WHEN </span><span style="color:black;">N</span><span style="color:gray;">%</span><span style="color:black;">2</span><span style="color:blue;">=</span><span style="color:black;">1 </span><span style="color:blue;">THEN </span><span style="color:magenta;">MAX</span><span style="color:gray;">(</span><span style="color:magenta;">CASE </span><span style="color:blue;">WHEN </span><span style="color:black;">N</span><span style="color:gray;">%</span><span style="color:black;">2</span><span style="color:blue;">=</span><span style="color:black;">0 </span><span style="color:blue;">THEN </span><span style="color:black;">SalesOrderDetailID </span><span style="color:blue;">END</span><span style="color:gray;">) </span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:black;">Partition </span><span style="color:blue;">BY </span><span style="color:gray;">(</span><span style="color:black;">N</span><span style="color:gray;">+</span><span style="color:black;">1</span><span style="color:gray;">)/</span><span style="color:black;">2</span><span style="color:gray;">) </span><span style="color:blue;">ELSE </span><span style="color:magenta;">MAX</span><span style="color:gray;">(</span><span style="color:magenta;">CASE </span><span style="color:blue;">WHEN </span><span style="color:black;">N</span><span style="color:gray;">%</span><span style="color:black;">2</span><span style="color:blue;">=</span><span style="color:black;">1 </span><span style="color:blue;">THEN </span><span style="color:black;">SalesOrderDetailID </span><span style="color:blue;">END</span><span style="color:gray;">) </span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:black;">Partition </span><span style="color:blue;">BY </span><span style="color:black;">N</span><span style="color:gray;">/</span><span style="color:black;">2</span><span style="color:gray;">) </span><span style="color:blue;">END </span><span style="color:black;">LeadVal</span><span style="color:gray;">,<br />
</span><span style="color:magenta;">CASE </span><span style="color:blue;">WHEN </span><span style="color:black;">N</span><span style="color:gray;">%</span><span style="color:black;">2</span><span style="color:blue;">=</span><span style="color:black;">1 </span><span style="color:blue;">THEN </span><span style="color:magenta;">MAX</span><span style="color:gray;">(</span><span style="color:magenta;">CASE </span><span style="color:blue;">WHEN </span><span style="color:black;">N</span><span style="color:gray;">%</span><span style="color:black;">2</span><span style="color:blue;">=</span><span style="color:black;">0 </span><span style="color:blue;">THEN </span><span style="color:black;">SalesOrderDetailID </span><span style="color:blue;">END</span><span style="color:gray;">) </span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:black;">Partition </span><span style="color:blue;">BY </span><span style="color:black;">N</span><span style="color:gray;">/</span><span style="color:black;">2</span><span style="color:gray;">) </span><span style="color:blue;">ELSE </span><span style="color:magenta;">MAX</span><span style="color:gray;">(</span><span style="color:magenta;">CASE </span><span style="color:blue;">WHEN </span><span style="color:black;">N</span><span style="color:gray;">%</span><span style="color:black;">2</span><span style="color:blue;">=</span><span style="color:black;">1 </span><span style="color:blue;">THEN </span><span style="color:black;">SalesOrderDetailID </span><span style="color:blue;">END</span><span style="color:gray;">) </span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:black;">Partition </span><span style="color:blue;">BY </span><span style="color:gray;">(</span><span style="color:black;">N</span><span style="color:gray;">+</span><span style="color:black;">1</span><span style="color:gray;">)/</span><span style="color:black;">2</span><span style="color:gray;">) </span><span style="color:blue;">END </span><span style="color:black;">LagVal<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">T1<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">SalesOrderID</span><span style="color:gray;">,<br />
</span><span style="color:black;">SalesOrderDetailID</span><span style="color:gray;">,<br />
</span><span style="color:black;">OrderQty</span><span style="color:gray;">;<br />
</span><span style="color:black;">GO<br />
</span></code></p>
<h2 style="text-align:justify;">No Analytic Function and Early Bird</h2>
<p style="text-align:justify;">Excellent Solution by<a href="http://blog.sqlauthority.com/2011/11/16/sql-server-puzzle-to-win-print-book-write-t-sql-self-join-without-using-first-_value-and-last_value/#comment-196511" target="_blank"> DHall</a> &#8211; Winner of <strong><a href="http://www.pluralsight-training.net/microsoft/?utm_source=sqlauthority&amp;utm_medium=sidebar&amp;utm_content=all-the-latest&amp;utm_campaign=m1-kickoff" target="_blank">Pluralsight 30 days Subscription</a></strong></p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:green;">-- a query to emulate LEAD() and LAG()<br />
</span><span style="color:gray;">;</span><span style="color:blue;">WITH </span><span style="color:black;">s </span><span style="color:blue;">AS </span><span style="color:gray;">(<br />
</span><span style="color:blue;">SELECT<br />
</span><span style="color:black;">1 </span><span style="color:blue;">AS </span><span style="color:black;">ldOffset</span><span style="color:gray;">, </span><span style="color:green;">-- equiv to 2nd param of LEAD<br />
</span><span style="color:black;">1 </span><span style="color:blue;">AS </span><span style="color:black;">lgOffset</span><span style="color:gray;">, </span><span style="color:green;">-- equiv to 2nd param of LAG<br />
</span><span style="color:gray;">NULL </span><span style="color:blue;">AS </span><span style="color:black;">ldDefVal</span><span style="color:gray;">, </span><span style="color:green;">-- equiv to 3rd param of LEAD<br />
</span><span style="color:gray;">NULL </span><span style="color:blue;">AS </span><span style="color:black;">lgDefVal</span><span style="color:gray;">, </span><span style="color:green;">-- equiv to 3rd param of LAG<br />
</span><span style="color:black;">ROW_NUMBER</span><span style="color:gray;">() </span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:blue;">ORDER BY </span><span style="color:black;">SalesOrderDetailID</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">row</span><span style="color:gray;">,<br />
</span><span style="color:black;">SalesOrderID</span><span style="color:gray;">,<br />
</span><span style="color:black;">SalesOrderDetailID</span><span style="color:gray;">,<br />
</span><span style="color:black;">OrderQty<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Sales.SalesOrderDetail<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">SalesOrderID </span><span style="color:blue;">IN </span><span style="color:gray;">(</span><span style="color:black;">43670</span><span style="color:gray;">, </span><span style="color:black;">43669</span><span style="color:gray;">, </span><span style="color:black;">43667</span><span style="color:gray;">, </span><span style="color:black;">43663</span><span style="color:gray;">)<br />
)<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">s.SalesOrderID</span><span style="color:gray;">,<br />
</span><span style="color:black;">s.SalesOrderDetailID</span><span style="color:gray;">,<br />
</span><span style="color:black;">s.OrderQty</span><span style="color:gray;">,<br />
</span><span style="color:magenta;">ISNULL</span><span style="color:gray;">( </span><span style="color:black;">sLd.SalesOrderDetailID</span><span style="color:gray;">, </span><span style="color:black;">s.ldDefVal</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">LeadValue</span><span style="color:gray;">,<br />
</span><span style="color:magenta;">ISNULL</span><span style="color:gray;">( </span><span style="color:black;">sLg.SalesOrderDetailID</span><span style="color:gray;">, </span><span style="color:black;">s.lgDefVal</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">LagValue<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">s<br />
</span><span style="color:magenta;">LEFT </span><span style="color:gray;">OUTER </span><span style="color:blue;">JOIN </span><span style="color:black;">s </span><span style="color:blue;">AS </span><span style="color:black;">sLd </span><span style="color:blue;">ON </span><span style="color:black;">s.row </span><span style="color:blue;">= </span><span style="color:black;">sLd.row </span><span style="color:gray;">- </span><span style="color:black;">s.ldOffset<br />
</span><span style="color:magenta;">LEFT </span><span style="color:gray;">OUTER </span><span style="color:blue;">JOIN </span><span style="color:black;">s </span><span style="color:blue;">AS </span><span style="color:black;">sLg </span><span style="color:blue;">ON </span><span style="color:black;">s.row </span><span style="color:blue;">= </span><span style="color:black;">sLg.row </span><span style="color:gray;">+ </span><span style="color:black;">s.lgOffset<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">s.SalesOrderID</span><span style="color:gray;">, </span><span style="color:black;">s.SalesOrderDetailID</span><span style="color:gray;">, </span><span style="color:black;">s.OrderQty</span></code></p>
<h2 style="text-align:justify;">No Analytic Function and Partition By</h2>
<p style="text-align:justify;">Excellent Solution by<a href="http://blog.sqlauthority.com/2011/11/16/sql-server-puzzle-to-win-print-book-write-t-sql-self-join-without-using-first-_value-and-last_value/#comment-196511" target="_blank"> DHall</a> &#8211; Winner of <strong><a href="http://www.pluralsight-training.net/microsoft/?utm_source=sqlauthority&amp;utm_medium=sidebar&amp;utm_content=all-the-latest&amp;utm_campaign=m1-kickoff" target="_blank">Pluralsight 30 days Subscription</a></strong></p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:green;">/* a query to emulate LEAD() and LAG() */<br />
</span><span style="color:gray;">;</span><span style="color:blue;">WITH </span><span style="color:black;">s </span><span style="color:blue;">AS </span><span style="color:gray;">(<br />
</span><span style="color:blue;">SELECT<br />
</span><span style="color:black;">1 </span><span style="color:blue;">AS </span><span style="color:black;">LeadOffset</span><span style="color:gray;">, </span><span style="color:green;">/* equiv to 2nd param of LEAD */<br />
</span><span style="color:black;">1 </span><span style="color:blue;">AS </span><span style="color:black;">LagOffset</span><span style="color:gray;">, </span><span style="color:green;">/* equiv to 2nd param of LAG */<br />
</span><span style="color:gray;">NULL </span><span style="color:blue;">AS </span><span style="color:black;">LeadDefVal</span><span style="color:gray;">, </span><span style="color:green;">/* equiv to 3rd param of LEAD */<br />
</span><span style="color:gray;">NULL </span><span style="color:blue;">AS </span><span style="color:black;">LagDefVal</span><span style="color:gray;">, </span><span style="color:green;">/* equiv to 3rd param of LAG */<br />
/* Try changing the values of the 4 integer values above to see their effect on the results */<br />
/* The values given above of 0, 0, null and null<br />
behave the same as the default 2nd and 3rd parameters to LEAD() and LAG() */<br />
</span><span style="color:black;">ROW_NUMBER</span><span style="color:gray;">() </span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:blue;">ORDER BY </span><span style="color:black;">SalesOrderDetailID</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">row</span><span style="color:gray;">,<br />
</span><span style="color:black;">SalesOrderID</span><span style="color:gray;">,<br />
</span><span style="color:black;">SalesOrderDetailID</span><span style="color:gray;">,<br />
</span><span style="color:black;">OrderQty<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Sales.SalesOrderDetail<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">SalesOrderID </span><span style="color:blue;">IN </span><span style="color:gray;">(</span><span style="color:black;">43670</span><span style="color:gray;">, </span><span style="color:black;">43669</span><span style="color:gray;">, </span><span style="color:black;">43667</span><span style="color:gray;">, </span><span style="color:black;">43663</span><span style="color:gray;">)<br />
)<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">s.SalesOrderID</span><span style="color:gray;">,<br />
</span><span style="color:black;">s.SalesOrderDetailID</span><span style="color:gray;">,<br />
</span><span style="color:black;">s.OrderQty</span><span style="color:gray;">,<br />
</span><span style="color:magenta;">ISNULL</span><span style="color:gray;">( </span><span style="color:black;">sLead.SalesOrderDetailID</span><span style="color:gray;">, </span><span style="color:black;">s.LeadDefVal</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">LeadValue</span><span style="color:gray;">,<br />
</span><span style="color:magenta;">ISNULL</span><span style="color:gray;">( </span><span style="color:black;">sLag.SalesOrderDetailID</span><span style="color:gray;">, </span><span style="color:black;">s.LagDefVal</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">LagValue<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">s<br />
</span><span style="color:magenta;">LEFT </span><span style="color:gray;">OUTER </span><span style="color:blue;">JOIN </span><span style="color:black;">s </span><span style="color:blue;">AS </span><span style="color:black;">sLead<br />
</span><span style="color:blue;">ON </span><span style="color:black;">s.row </span><span style="color:blue;">= </span><span style="color:black;">sLead.row </span><span style="color:gray;">- </span><span style="color:black;">s.LeadOffset<br />
</span><span style="color:green;">/* Try commenting out this next line when LeadOffset != 0 */<br />
</span><span style="color:gray;">AND </span><span style="color:black;">s.SalesOrderID </span><span style="color:blue;">= </span><span style="color:black;">sLead.SalesOrderID<br />
</span><span style="color:green;">/* The additional join criteria on SalesOrderID above<br />
is equivalent to PARTITION BY SalesOrderID<br />
in the OVER clause of the LEAD() function */<br />
</span><span style="color:magenta;">LEFT </span><span style="color:gray;">OUTER </span><span style="color:blue;">JOIN </span><span style="color:black;">s </span><span style="color:blue;">AS </span><span style="color:black;">sLag<br />
</span><span style="color:blue;">ON </span><span style="color:black;">s.row </span><span style="color:blue;">= </span><span style="color:black;">sLag.row </span><span style="color:gray;">+ </span><span style="color:black;">s.LagOffset<br />
</span><span style="color:green;">/* Try commenting out this next line when LagOffset != 0 */<br />
</span><span style="color:gray;">AND </span><span style="color:black;">s.SalesOrderID </span><span style="color:blue;">= </span><span style="color:black;">sLag.SalesOrderID<br />
</span><span style="color:green;">/* The additional join criteria on SalesOrderID above<br />
is equivalent to PARTITION BY SalesOrderID<br />
in the OVER clause of the LAG() function */<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">s.SalesOrderID</span><span style="color:gray;">, </span><span style="color:black;">s.SalesOrderDetailID</span><span style="color:gray;">, </span><span style="color:black;">s.OrderQty</span></code></p>
<h2 style="text-align:justify;">No Analytic Function and CTE Usage</h2>
<p style="text-align:justify;">Excellent Solution by <a href="http://blog.sqlauthority.com/2011/11/16/sql-server-puzzle-to-win-print-book-write-t-sql-self-join-without-using-first-_value-and-last_value/#comment-196633" target="_blank">Pravin Patel</a> <strong>- </strong>Winner of<strong> SQL Server Interview Questions and Answers (<a href="http://bit.ly/indiasqlinterview" target="_blank">India </a>| <a href="http://amzn.to/qTOXoG" target="_blank">USA</a>)</strong></p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:green;">--CTE based solution<br />
</span><span style="color:gray;">;<br />
</span><span style="color:blue;">WITH </span><span style="color:black;">cteMain<br />
</span><span style="color:blue;">AS<br />
</span><span style="color:gray;">(<br />
</span><span style="color:blue;">SELECT<br />
</span><span style="color:black;">SalesOrderID</span><span style="color:gray;">,<br />
</span><span style="color:black;">SalesOrderDetailID</span><span style="color:gray;">,<br />
</span><span style="color:black;">OrderQty</span><span style="color:gray;">,<br />
</span><span style="color:black;">ROW_NUMBER</span><span style="color:gray;">() </span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:blue;">ORDER BY </span><span style="color:black;">SalesOrderDetailID</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">sn<br />
</span><span style="color:blue;">FROM<br />
</span><span style="color:black;">Sales.SalesOrderDetail<br />
</span><span style="color:blue;">WHERE<br />
</span><span style="color:black;">SalesOrderID </span><span style="color:blue;">IN </span><span style="color:gray;">(</span><span style="color:black;">43670</span><span style="color:gray;">, </span><span style="color:black;">43669</span><span style="color:gray;">, </span><span style="color:black;">43667</span><span style="color:gray;">, </span><span style="color:black;">43663</span><span style="color:gray;">)<br />
)<br />
</span><span style="color:blue;">SELECT<br />
</span><span style="color:black;">m.SalesOrderID</span><span style="color:gray;">, </span><span style="color:black;">m.SalesOrderDetailID</span><span style="color:gray;">, </span><span style="color:black;">m.OrderQty</span><span style="color:gray;">,<br />
</span><span style="color:black;">sLead.SalesOrderDetailID </span><span style="color:blue;">AS </span><span style="color:black;">leadvalue</span><span style="color:gray;">,<br />
</span><span style="color:black;">sLeg.SalesOrderDetailID </span><span style="color:blue;">AS </span><span style="color:black;">leagvalue<br />
</span><span style="color:blue;">FROM<br />
</span><span style="color:black;">cteMain </span><span style="color:blue;">AS </span><span style="color:black;">m<br />
</span><span style="color:magenta;">LEFT </span><span style="color:gray;">OUTER </span><span style="color:blue;">JOIN </span><span style="color:black;">cteMain </span><span style="color:blue;">AS </span><span style="color:black;">sLead<br />
</span><span style="color:blue;">ON </span><span style="color:black;">sLead.sn </span><span style="color:blue;">= </span><span style="color:black;">m.sn</span><span style="color:gray;">+</span><span style="color:black;">1<br />
</span><span style="color:magenta;">LEFT </span><span style="color:gray;">OUTER </span><span style="color:blue;">JOIN </span><span style="color:black;">cteMain </span><span style="color:blue;">AS </span><span style="color:black;">sLeg<br />
</span><span style="color:blue;">ON </span><span style="color:black;">sLeg.sn </span><span style="color:blue;">= </span><span style="color:black;">m.sn</span><span style="color:gray;">-</span><span style="color:black;">1<br />
</span><span style="color:blue;">ORDER BY<br />
</span><span style="color:black;">m.SalesOrderID</span><span style="color:gray;">, </span><span style="color:black;">m.SalesOrderDetailID</span><span style="color:gray;">, </span><span style="color:black;">m.OrderQty </span></code></p>
<h2 style="text-align:justify;">No Analytic Function and Co-Related Subquery Usage</h2>
<p style="text-align:justify;">Excellent Solution by <a href="http://blog.sqlauthority.com/2011/11/16/sql-server-puzzle-to-win-print-book-write-t-sql-self-join-without-using-first-_value-and-last_value/#comment-196633" target="_blank">Pravin Patel</a> &#8211; Winner of<strong> SQL Server Interview Questions and Answers (<a href="http://bit.ly/indiasqlinterview" target="_blank">India </a>| <a href="http://amzn.to/qTOXoG" target="_blank">USA</a>)</strong></p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:green;">-- Co-Related subquery<br />
</span><span style="color:blue;">SELECT<br />
</span><span style="color:black;">m.SalesOrderID</span><span style="color:gray;">,<br />
</span><span style="color:black;">m.SalesOrderDetailID</span><span style="color:gray;">,<br />
</span><span style="color:black;">m.OrderQty</span><span style="color:gray;">,<br />
( </span><span style="color:blue;">SELECT </span><span style="color:magenta;">MIN</span><span style="color:gray;">(</span><span style="color:black;">SalesOrderDetailID</span><span style="color:gray;">)<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Sales.SalesOrderDetail </span><span style="color:blue;">AS </span><span style="color:black;">l<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">l.SalesOrderID </span><span style="color:blue;">IN </span><span style="color:gray;">(</span><span style="color:black;">43670</span><span style="color:gray;">, </span><span style="color:black;">43669</span><span style="color:gray;">, </span><span style="color:black;">43667</span><span style="color:gray;">, </span><span style="color:black;">43663</span><span style="color:gray;">)<br />
AND </span><span style="color:black;">l.SalesOrderID </span><span style="color:gray;">&gt;= </span><span style="color:black;">m.SalesOrderID </span><span style="color:gray;">AND </span><span style="color:black;">l.SalesOrderDetailID </span><span style="color:gray;">&gt; </span><span style="color:black;">m.SalesOrderDetailID<br />
</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">lead</span><span style="color:gray;">,<br />
( </span><span style="color:blue;">SELECT </span><span style="color:magenta;">MAX</span><span style="color:gray;">(</span><span style="color:black;">SalesOrderDetailID</span><span style="color:gray;">)<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Sales.SalesOrderDetail </span><span style="color:blue;">AS </span><span style="color:black;">l<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">l.SalesOrderID </span><span style="color:blue;">IN </span><span style="color:gray;">(</span><span style="color:black;">43670</span><span style="color:gray;">, </span><span style="color:black;">43669</span><span style="color:gray;">, </span><span style="color:black;">43667</span><span style="color:gray;">, </span><span style="color:black;">43663</span><span style="color:gray;">)<br />
AND </span><span style="color:black;">l.SalesOrderID </span><span style="color:gray;">&lt;= </span><span style="color:black;">m.SalesOrderID </span><span style="color:gray;">AND </span><span style="color:black;">l.SalesOrderDetailID </span><span style="color:gray;">&lt; </span><span style="color:black;">m.SalesOrderDetailID<br />
</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">leag<br />
</span><span style="color:blue;">FROM<br />
</span><span style="color:black;">Sales.SalesOrderDetail </span><span style="color:blue;">AS </span><span style="color:black;">m<br />
</span><span style="color:blue;">WHERE<br />
</span><span style="color:black;">m.SalesOrderID </span><span style="color:blue;">IN </span><span style="color:gray;">(</span><span style="color:black;">43670</span><span style="color:gray;">, </span><span style="color:black;">43669</span><span style="color:gray;">, </span><span style="color:black;">43667</span><span style="color:gray;">, </span><span style="color:black;">43663</span><span style="color:gray;">)<br />
</span><span style="color:blue;">ORDER BY<br />
</span><span style="color:black;">m.SalesOrderID</span><span style="color:gray;">, </span><span style="color:black;">m.SalesOrderDetailID</span><span style="color:gray;">, </span><span style="color:black;">m.OrderQty </span></code></p>
<p style="text-align:justify;">This was one of the most interesting Puzzle on this blog.</p>
<h2 style="text-align:justify;">Giveaway</h2>
<p style="text-align:justify;">Winners will get following giveaways.</p>
<p style="text-align:justify;"><a href="http://blog.sqlauthority.com/2011/11/16/sql-server-puzzle-to-win-print-book-write-t-sql-self-join-without-using-first-_value-and-last_value/#comment-197710" target="_blank">Geri Reshef</a> and <a href="http://blog.sqlauthority.com/2011/11/16/sql-server-puzzle-to-win-print-book-write-t-sql-self-join-without-using-first-_value-and-last_value/#comment-196633" target="_blank">Pravin Patel</a></p>
<p style="text-align:justify;"><strong>SQL Server Interview Questions and Answers (<a href="http://bit.ly/indiasqlinterview" target="_blank">India </a>| <a href="http://amzn.to/qTOXoG" target="_blank">USA</a>)</strong></p>
<p style="text-align:justify;"><a href="http://blog.sqlauthority.com/2011/11/16/sql-server-puzzle-to-win-print-book-write-t-sql-self-join-without-using-first-_value-and-last_value/#comment-196511" target="_blank"> DHall</a></p>
<p style="text-align:justify;"><strong><a href="http://www.pluralsight-training.net/microsoft/?utm_source=sqlauthority&amp;utm_medium=sidebar&amp;utm_content=all-the-latest&amp;utm_campaign=m1-kickoff" target="_blank">Pluralsight 30 days Subscription</a></strong></p>
<p style="text-align:justify;">Reference: <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>)</strong></p>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/postaday/'>PostADay</a>, <a href='http://blog.sqlauthority.com/category/readers-contribution/'>Readers Contribution</a>, <a href='http://blog.sqlauthority.com/category/readers-question/'>Readers Question</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-function/'>SQL Function</a>, <a href='http://blog.sqlauthority.com/category/sql-puzzle/'>SQL Puzzle</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/tech/sql-scripts/'>SQL Scripts</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/16017/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/16017/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/16017/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/16017/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/16017/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/16017/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/16017/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/16017/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/16017/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/16017/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/16017/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/16017/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/16017/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/16017/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=16017&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2011/11/24/sql-server-solution-to-puzzle-simulate-lead-and-lag-without-using-sql-server-2012-analytic-function/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/leadlag1.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Introduction to PERCENTILE_DISC()  &#8211; Analytic Functions Introduced in SQL Server 2012</title>
		<link>http://blog.sqlauthority.com/2011/11/22/sql-server-introduction-to-percentile_disc-analytic-functions-introduced-in-sql-server-2012/</link>
		<comments>http://blog.sqlauthority.com/2011/11/22/sql-server-introduction-to-percentile_disc-analytic-functions-introduced-in-sql-server-2012/#comments</comments>
		<pubDate>Tue, 22 Nov 2011 01:30:03 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[PostADay]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Function]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=15977</guid>
		<description><![CDATA[SQL Server 2012 introduces new analytical function PERCENTILE_DISC(). The book online gives following definition of this function: Computes a specific percentile for sorted values in an entire rowset or within distinct partitions of a rowset in Microsoft SQL Server 2012 Release Candidate 0 (RC 0). For a given percentile value P, PERCENTILE_DISC sorts the values of [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=15977&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">SQL Server 2012 introduces new analytical function PERCENTILE_DISC().</p>
<p style="text-align:justify;">The book online gives following definition of this function: Computes a specific percentile for sorted values in an entire rowset or within distinct partitions of a rowset in Microsoft SQL Server 2012 Release Candidate 0 (RC 0). For a given percentile value P, PERCENTILE_DISC sorts the values of the expression in the ORDER BY clause and returns the value with the smallest CUME_DIST value (with respect to the same sort specification) that is greater than or equal to P.</p>
<p style="text-align:justify;">If you are clear with understanding of the function &#8211; no need to read further. If you got lost here is the same in simple words &#8211; find value of the column which is equal or more than CUME_DIST.</p>
<p style="text-align:justify;">Before you continue reading this blog I strongly suggest you read about CUME_DIST function over here <strong><a title="Permanent Link to SQL SERVER – Introduction to CUME_DIST – Analytic Functions Introduced in SQL Server 2012" href="http://blog.sqlauthority.com/2011/11/08/sql-server-introduction-to-cume_dist-analytic-functions-introduced-in-sql-server-2012/" rel="bookmark">Introduction to CUME_DIST – Analytic Functions Introduced in SQL Server 2012</a></strong>.</p>
<p style="text-align:justify;">Now let’s have fun following query:</p>
<p><code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks<br />
GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">SalesOrderID</span><span style="color:gray;">, </span><span style="color:black;">OrderQty</span><span style="color:gray;">, </span><span style="color:black;">ProductID</span><span style="color:gray;">,<br />
</span><span style="color:black;">CUME_DIST</span><span style="color:gray;">() </span><span style="color:blue;">OVER</span><span style="color:gray;">(</span><span style="color:black;">PARTITION </span><span style="color:blue;">BY </span><span style="color:black;">SalesOrderID<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">ProductID </span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">CDist</span><span style="color:gray;">,<br />
</span><span style="color:black;">PERCENTILE_DISC</span><span style="color:gray;">(</span><span style="color:black;">0.5</span><span style="color:gray;">) </span><span style="color:black;">WITHIN </span><span style="color:blue;">GROUP </span><span style="color:gray;">(</span><span style="color:blue;">ORDER BY </span><span style="color:black;">ProductID</span><span style="color:gray;">)<br />
</span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:black;">PARTITION </span><span style="color:blue;">BY </span><span style="color:black;">SalesOrderID</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">PercentileDisc<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Sales.SalesOrderDetail<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">SalesOrderID </span><span style="color:blue;">IN </span><span style="color:gray;">(</span><span style="color:black;">43670</span><span style="color:gray;">, </span><span style="color:black;">43669</span><span style="color:gray;">, </span><span style="color:black;">43667</span><span style="color:gray;">, </span><span style="color:black;">43663</span><span style="color:gray;">)<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">SalesOrderID </span><span style="color:blue;">DESC<br />
</span><span style="color:black;">GO<br />
</span></code></p>
<p style="text-align:justify;">The above query will give us the following result:</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/percentiledisc11.jpg" alt="" width="499" height="272" /></p>
<p style="text-align:justify;">You can see that I have used PERCENTILE_DISC(0.5) in query, which is similar to finding median but not exactly. PERCENTILE_DISC() function takes a percentile as a passing parameters. It returns the value as answer which value is equal or great to the percentile value which is passed into the example. For example in above example we are passing 0.5 into the PERCENTILE_DISC() function. It will go through the resultset and identify which rows has values which are equal to or great than 0.5. In first example it found two rows which are equal to 0.5 and the value of ProductID of that row is the answer of PERCENTILE_DISC(). In some third windowed resultset there is only single row with the CUME_DIST() value as 1 and that is for sure higher than 0.5 making it as a answer.</p>
<p style="text-align:justify;">To make sure that we are clear with this example properly. Here is one more example where I am passing 0.6 as a percentile.</p>
<p>Now let’s have fun following query:<br />
<code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks<br />
GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">SalesOrderID</span><span style="color:gray;">, </span><span style="color:black;">OrderQty</span><span style="color:gray;">, </span><span style="color:black;">ProductID</span><span style="color:gray;">,<br />
</span><span style="color:black;">CUME_DIST</span><span style="color:gray;">() </span><span style="color:blue;">OVER</span><span style="color:gray;">(</span><span style="color:black;">PARTITION </span><span style="color:blue;">BY </span><span style="color:black;">SalesOrderID<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">ProductID </span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">CDist</span><span style="color:gray;">,<br />
</span><span style="color:black;">PERCENTILE_DISC</span><span style="color:gray;">(</span><span style="color:black;">0.6</span><span style="color:gray;">) </span><span style="color:black;">WITHIN </span><span style="color:blue;">GROUP </span><span style="color:gray;">(</span><span style="color:blue;">ORDER BY </span><span style="color:black;">ProductID</span><span style="color:gray;">)<br />
</span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:black;">PARTITION </span><span style="color:blue;">BY </span><span style="color:black;">SalesOrderID</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">PercentileDisc<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Sales.SalesOrderDetail<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">SalesOrderID </span><span style="color:blue;">IN </span><span style="color:gray;">(</span><span style="color:black;">43670</span><span style="color:gray;">, </span><span style="color:black;">43669</span><span style="color:gray;">, </span><span style="color:black;">43667</span><span style="color:gray;">, </span><span style="color:black;">43663</span><span style="color:gray;">)<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">SalesOrderID </span><span style="color:blue;">DESC<br />
</span><span style="color:black;">GO<br />
</span></code></p>
<p>The above query will give us the following result:</p>
<p><img class="alignnone" src="http://www.pinaldave.com/bimg/percentiledisc12.jpg" alt="" width="500" height="280" /></p>
<p style="text-align:justify;">The result of the PERCENTILE_DISC(0.6) is ProductID of which CUME_DIST() is more than 0.6. This means for SalesOrderID 43670 has row with CUME_DIST() 0.75 is the qualified row, resulting answer 773 for ProductID.</p>
<p style="text-align:justify;">I hope this explanation makes it further clear.</p>
<p style="text-align:justify;">Reference: <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>)</strong></p>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/postaday/'>PostADay</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-function/'>SQL Function</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/tech/sql-scripts/'>SQL Scripts</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/15977/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/15977/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/15977/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/15977/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/15977/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/15977/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/15977/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/15977/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/15977/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/15977/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/15977/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/15977/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/15977/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/15977/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=15977&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2011/11/22/sql-server-introduction-to-percentile_disc-analytic-functions-introduced-in-sql-server-2012/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/percentiledisc11.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/percentiledisc12.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Puzzle to Win Print Book &#8211; Explain Value of PERCENTILE_CONT() Using Simple Example</title>
		<link>http://blog.sqlauthority.com/2011/11/21/sql-server-puzzle-to-win-print-book-explain-value-of-percentile_cont-using-simple-example/</link>
		<comments>http://blog.sqlauthority.com/2011/11/21/sql-server-puzzle-to-win-print-book-explain-value-of-percentile_cont-using-simple-example/#comments</comments>
		<pubDate>Mon, 21 Nov 2011 01:30:00 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[PostADay]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Function]]></category>
		<category><![CDATA[SQL Puzzle]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=15968</guid>
		<description><![CDATA[From last several days I am working on various Denali Analytical functions and it is indeed really fun to refresh the concept which I studied in the school. Earlier I wrote article where I explained how we can use PERCENTILE_CONT() to find median over here SQL SERVER – Introduction to PERCENTILE_CONT() – Analytic Functions Introduced in [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=15968&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">From last several days I am working on various Denali Analytical functions and it is indeed really fun to refresh the concept which I studied in the school.</p>
<p style="text-align:justify;">Earlier I wrote article where I explained how we can use PERCENTILE_CONT() to find median over here <strong><a title="Permanent Link to SQL SERVER – Introduction to PERCENTILE_CONT()  – Analytic Functions Introduced in SQL Server 2012" href="http://blog.sqlauthority.com/2011/11/20/sql-server-introduction-to-percentile_cont-analytic-functions-introduced-in-sql-server-2012/" rel="bookmark">SQL SERVER – Introduction to PERCENTILE_CONT() – Analytic Functions Introduced in SQL Server 2012</a></strong>. Today I am going to ask question based on the same blog post. Again just like last time the intention of this puzzle is as following:</p>
<ol style="text-align:justify;">
<li>Learn new concept of SQL Server 2012</li>
<li>Learn new concept of SQL Server 2012 even if you are on earlier version of SQL Server.</li>
</ol>
<p style="text-align:justify;">On another note, SQL Server 2012 RC0 has been announced and available to download <strong><a title="Permanent Link to SQL SERVER – 2012 RC0 Various Resources and Downloads" href="http://blog.sqlauthority.com/2011/11/19/sql-server-2012-rc0-various-resources-and-downloads/" rel="bookmark">SQL SERVER – 2012 RC0 Various Resources and Downloads</a></strong>.</p>
<p style="text-align:justify;">Now let’s have fun following query:</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks<br />
GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">SalesOrderID</span><span style="color:gray;">, </span><span style="color:black;">OrderQty</span><span style="color:gray;">, </span><span style="color:black;">ProductID</span><span style="color:gray;">,<br />
</span><span style="color:black;">PERCENTILE_CONT</span><span style="color:gray;">(</span><span style="color:black;">0.5</span><span style="color:gray;">) </span><span style="color:black;">WITHIN </span><span style="color:blue;">GROUP </span><span style="color:gray;">(</span><span style="color:blue;">ORDER BY </span><span style="color:black;">ProductID</span><span style="color:gray;">)<br />
</span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:black;">PARTITION </span><span style="color:blue;">BY </span><span style="color:black;">SalesOrderID</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">MedianCont<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Sales.SalesOrderDetail<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">SalesOrderID </span><span style="color:blue;">IN </span><span style="color:gray;">(</span><span style="color:black;">43670</span><span style="color:gray;">, </span><span style="color:black;">43669</span><span style="color:gray;">, </span><span style="color:black;">43667</span><span style="color:gray;">, </span><span style="color:black;">43663</span><span style="color:gray;">)<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">SalesOrderID </span><span style="color:blue;">DESC<br />
</span><span style="color:black;">GO<br />
</span></code></p>
<p style="text-align:justify;">The above query will give us the following result:</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/percentiledisc1.jpg" alt="" width="514" height="262" /></p>
<p style="text-align:justify;">The reason we get median is because we are passing value .05 to PERCENTILE_COUNT() function. Now run read the puzzle.</p>
<h2 style="text-align:justify;"><strong>Puzzle: </strong></h2>
<p style="text-align:justify;">Run following T-SQL code:</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks<br />
GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">SalesOrderID</span><span style="color:gray;">, </span><span style="color:black;">OrderQty</span><span style="color:gray;">, </span><span style="color:black;">ProductID</span><span style="color:gray;">,<br />
</span><span style="color:black;">PERCENTILE_CONT</span><span style="color:gray;">(</span><span style="color:black;">0.9</span><span style="color:gray;">) </span><span style="color:black;">WITHIN </span><span style="color:blue;">GROUP </span><span style="color:gray;">(</span><span style="color:blue;">ORDER BY </span><span style="color:black;">ProductID</span><span style="color:gray;">)<br />
</span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:black;">PARTITION </span><span style="color:blue;">BY </span><span style="color:black;">SalesOrderID</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">MedianCont<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Sales.SalesOrderDetail<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">SalesOrderID </span><span style="color:blue;">IN </span><span style="color:gray;">(</span><span style="color:black;">43670</span><span style="color:gray;">, </span><span style="color:black;">43669</span><span style="color:gray;">, </span><span style="color:black;">43667</span><span style="color:gray;">, </span><span style="color:black;">43663</span><span style="color:gray;">)<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">SalesOrderID </span><span style="color:blue;">DESC<br />
</span><span style="color:black;">GO<br />
</span></code><br />
Observe the result and you will notice that MidianCont has different value than before, the reason is PERCENTILE_CONT function has 0.9 value passed. For first four value the value is 775.1.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/median1.jpg" alt="" width="499" height="259" /></p>
<p style="text-align:justify;">Now run following T-SQL code:<br />
<code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks<br />
GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">SalesOrderID</span><span style="color:gray;">, </span><span style="color:black;">OrderQty</span><span style="color:gray;">, </span><span style="color:black;">ProductID</span><span style="color:gray;">,<br />
</span><span style="color:black;">PERCENTILE_CONT</span><span style="color:gray;">(</span><span style="color:black;">0.1</span><span style="color:gray;">) </span><span style="color:black;">WITHIN </span><span style="color:blue;">GROUP </span><span style="color:gray;">(</span><span style="color:blue;">ORDER BY </span><span style="color:black;">ProductID</span><span style="color:gray;">)<br />
</span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:black;">PARTITION </span><span style="color:blue;">BY </span><span style="color:black;">SalesOrderID</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">MedianCont<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Sales.SalesOrderDetail<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">SalesOrderID </span><span style="color:blue;">IN </span><span style="color:gray;">(</span><span style="color:black;">43670</span><span style="color:gray;">, </span><span style="color:black;">43669</span><span style="color:gray;">, </span><span style="color:black;">43667</span><span style="color:gray;">, </span><span style="color:black;">43663</span><span style="color:gray;">)<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">SalesOrderID </span><span style="color:blue;">DESC<br />
</span><span style="color:black;">GO<br />
</span></code></p>
<p style="text-align:justify;">Observe the result and you will notice that MidianCont has different value than before, the reason is PERCENTILE_CONT function has 0.1 value passed. For first four value the value is 709.3.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/median2.jpg" alt="" width="501" height="255" /></p>
<p style="text-align:justify;">Now in my example I have explained how the median is found using this function. You have to explain using mathematics and explain (in easy words) why the value in last columns are 709.3 and 775.1</p>
<h2 style="text-align:justify;">Hint:</h2>
<ul style="text-align:justify;">
<li><strong><a href="http://blog.sqlauthority.com/2009/04/13/sql-server-introduction-to-joins-basic-of-joins/" target="_blank">SQL SERVER – Introduction to PERCENTILE_CONT() – Analytic Functions Introduced in SQL Server 2012</a></strong></li>
</ul>
<h2 style="text-align:justify;">Rules</h2>
<ul style="text-align:justify;">
<li>Leave a comment with your detailed answer by Nov 25′s blog post.</li>
<li>Open world-wide (where Amazon ships books)</li>
<li>If you blog about puzzle’s solution and if you win, you win additional surprise gift as well.</li>
</ul>
<h2 style="text-align:justify;">Prizes</h2>
<p style="text-align:justify;"><a href="http://blog.sqlauthority.com/sql-server-books/sql-server-interview-questions-and-answers-for-all-database-developers-and-developers-administrators/" target="_blank"><img src="http://www.pinaldave.com/bimg/sqlinterview_s" alt="" width="169" height="258" /></a></p>
<p style="text-align:justify;">Print copy of my new book SQL Server Interview Questions <strong><a href="http://amzn.to/qTOXoG" target="_blank">Amazon</a>|<a href="http://bit.ly/indiasqlinterview" target="_blank">Flipkart</a></strong></p>
<p style="text-align:justify;">If you already have this book, you can opt for any of my other books SQL Wait Stats [<strong><a href="http://amzn.to/p3tL1n" target="_blank">Amazon</a>|<a href="http://amzn.to/waitstatskindle" target="_blank">Flipkart</a>|<a href="http://amzn.to/waitstatskindle" target="_blank">Kindle</a></strong>] and SQL Programming [<strong><a href="http://amzn.to/rsC2La" target="_blank">Amazon</a>|<a href="http://bit.ly/pinalbook4" target="_blank">Flipkart</a>|<a href="http://amzn.to/sqlbook4onkindle" target="_blank">Kindle</a></strong>].</p>
<p style="text-align:justify;">Reference: <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>)</strong></p>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/postaday/'>PostADay</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-function/'>SQL Function</a>, <a href='http://blog.sqlauthority.com/category/sql-puzzle/'>SQL Puzzle</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/tech/sql-scripts/'>SQL Scripts</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/15968/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/15968/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/15968/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/15968/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/15968/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/15968/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/15968/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/15968/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/15968/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/15968/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/15968/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/15968/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/15968/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/15968/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=15968&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2011/11/21/sql-server-puzzle-to-win-print-book-explain-value-of-percentile_cont-using-simple-example/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/percentiledisc1.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/median1.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/median2.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/sqlinterview_s" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Introduction to PERCENTILE_CONT()  &#8211; Analytic Functions Introduced in SQL Server 2012</title>
		<link>http://blog.sqlauthority.com/2011/11/20/sql-server-introduction-to-percentile_cont-analytic-functions-introduced-in-sql-server-2012/</link>
		<comments>http://blog.sqlauthority.com/2011/11/20/sql-server-introduction-to-percentile_cont-analytic-functions-introduced-in-sql-server-2012/#comments</comments>
		<pubDate>Sun, 20 Nov 2011 01:30:01 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[PostADay]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Function]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=15960</guid>
		<description><![CDATA[SQL Server 2012 introduces new analytical function PERCENTILE_CONT(). The book online gives following definition of this function: Calculates a percentile based on a continuous distribution of the column value in Microsoft SQL Server 2012 Release Candidate 0 (RC 0). The result is interpolated and might not be equal to any of the specific values in [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=15960&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">SQL Server 2012 introduces new analytical function PERCENTILE_CONT().</p>
<p style="text-align:justify;">The book online gives following definition of this function: Calculates a percentile based on a continuous distribution of the column value in Microsoft SQL Server 2012 Release Candidate 0 (RC 0). The result is interpolated and might not be equal to any of the specific values in the column.</p>
<p style="text-align:justify;">If you are clear with understanding of the function &#8211; no need to read further. If you got lost here is the same in simple words &#8211; it is lot like finding median with percentile value.</p>
<p style="text-align:justify;">Now let’s have fun following query:</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks<br />
GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">SalesOrderID</span><span style="color:gray;">, </span><span style="color:black;">OrderQty</span><span style="color:gray;">, </span><span style="color:black;">ProductID</span><span style="color:gray;">,<br />
</span><span style="color:black;">PERCENTILE_CONT</span><span style="color:gray;">(</span><span style="color:black;">0.5</span><span style="color:gray;">) </span><span style="color:black;">WITHIN </span><span style="color:blue;">GROUP </span><span style="color:gray;">(</span><span style="color:blue;">ORDER BY </span><span style="color:black;">ProductID</span><span style="color:gray;">)<br />
</span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:black;">PARTITION </span><span style="color:blue;">BY </span><span style="color:black;">SalesOrderID</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">MedianCont<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Sales.SalesOrderDetail<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">SalesOrderID </span><span style="color:blue;">IN </span><span style="color:gray;">(</span><span style="color:black;">43670</span><span style="color:gray;">, </span><span style="color:black;">43669</span><span style="color:gray;">, </span><span style="color:black;">43667</span><span style="color:gray;">, </span><span style="color:black;">43663</span><span style="color:gray;">)<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">SalesOrderID </span><span style="color:blue;">DESC<br />
</span><span style="color:black;">GO<br />
</span></code></p>
<p style="text-align:justify;">The above query will give us the following result:</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/percentiledisc2.jpg" alt="" width="500" height="265" /></p>
<p style="text-align:justify;">You can see that I have used PERCENTILE_COUNT(0.5) in query, which is similar to finding median. Let me explain above diagram with little more explanation. The defination of median is as following:</p>
<p style="text-align:justify;">In case of Even Number of elements = In ordered list add the two digits from the middle and devide by 2<br />
In case of Odd Numbers of elements = In ordered list select the digits from the middle</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/percentiledisc1.jpg" alt="" width="500" height="265" /></p>
<p style="text-align:justify;">I hope this example gives clear idea how PERCENTILE_CONT() works.</p>
<p style="text-align:justify;">Reference: <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>)</strong></p>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/postaday/'>PostADay</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-function/'>SQL Function</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/tech/sql-scripts/'>SQL Scripts</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/15960/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/15960/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/15960/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/15960/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/15960/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/15960/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/15960/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/15960/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/15960/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/15960/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/15960/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/15960/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/15960/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/15960/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=15960&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2011/11/20/sql-server-introduction-to-percentile_cont-analytic-functions-introduced-in-sql-server-2012/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/percentiledisc2.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/percentiledisc1.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Introduction to PERCENT_RANK() &#8211; Analytic Functions Introduced in SQL Server 2012</title>
		<link>http://blog.sqlauthority.com/2011/11/18/sql-server-introduction-to-percent_rank-analytic-functions-introduced-in-sql-server-2012/</link>
		<comments>http://blog.sqlauthority.com/2011/11/18/sql-server-introduction-to-percent_rank-analytic-functions-introduced-in-sql-server-2012/#comments</comments>
		<pubDate>Fri, 18 Nov 2011 01:30:44 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[PostADay]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Function]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=15944</guid>
		<description><![CDATA[SQL Server 2012 introduces new analytical functions PERCENT_RANK(). This function returns relative standing of a value within a query result set or partition. It will be very difficult to explain this in words so I’d like to attempt to explain its function through a brief example. Instead of creating a new table, I will be [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=15944&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">SQL Server 2012 introduces new analytical functions PERCENT_RANK(). This function returns relative standing of a value within a query result set or partition. It will be very difficult to explain this in words so I’d like to attempt to explain its function through a brief example. Instead of creating a new table, I will be using the AdventureWorks sample database as most developers use that for experiment purposes.</p>
<p style="text-align:justify;">Now let’s have fun following query:</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks<br />
GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">SalesOrderID</span><span style="color:gray;">, </span><span style="color:black;">OrderQty</span><span style="color:gray;">,<br />
</span><span style="color:black;">RANK</span><span style="color:gray;">() </span><span style="color:blue;">OVER</span><span style="color:gray;">(</span><span style="color:blue;">ORDER BY </span><span style="color:black;">SalesOrderID</span><span style="color:gray;">) </span><span style="color:black;">Rnk</span><span style="color:gray;">,<br />
</span><span style="color:black;">PERCENT_RANK</span><span style="color:gray;">() </span><span style="color:blue;">OVER</span><span style="color:gray;">(</span><span style="color:blue;">ORDER BY </span><span style="color:black;">SalesOrderID</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">PctDist<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Sales.SalesOrderDetail<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">SalesOrderID </span><span style="color:blue;">IN </span><span style="color:gray;">(</span><span style="color:black;">43670</span><span style="color:gray;">, </span><span style="color:black;">43669</span><span style="color:gray;">, </span><span style="color:black;">43667</span><span style="color:gray;">, </span><span style="color:black;">43663</span><span style="color:gray;">)<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">PctDist </span><span style="color:blue;">DESC<br />
</span><span style="color:black;">GO<br />
</span></code></p>
<p style="text-align:justify;">The above query will give us the following result:</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/percentrank.jpg" alt="" width="500" height="390" /></p>
<p style="text-align:justify;">Now let us understand the resultset. You will notice that I have also included the RANK() function along with this query. The reason to include RANK() function was as this query is infect uses RANK function and find the relative standing of the query.</p>
<p style="text-align:justify;">The formula to find PERCENT_RANK() is as following:</p>
<p style="text-align:justify;"><strong>PERCENT_RANK() = (RANK() – 1) / (Total Rows – 1)</strong></p>
<p style="text-align:justify;">If you want to read more about this function read <a href="http://en.wikipedia.org/wiki/Percentile" target="_blank">here</a>.</p>
<p style="text-align:justify;">Now let us attempt the same example with PARTITION BY clause</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks<br />
GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">SalesOrderID</span><span style="color:gray;">, </span><span style="color:black;">OrderQty</span><span style="color:gray;">, </span><span style="color:black;">ProductID</span><span style="color:gray;">,<br />
</span><span style="color:black;">RANK</span><span style="color:gray;">() </span><span style="color:blue;">OVER</span><span style="color:gray;">(</span><span style="color:black;">PARTITION </span><span style="color:blue;">BY </span><span style="color:black;">SalesOrderID<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">ProductID </span><span style="color:gray;">) </span><span style="color:black;">Rnk</span><span style="color:gray;">,<br />
</span><span style="color:black;">PERCENT_RANK</span><span style="color:gray;">() </span><span style="color:blue;">OVER</span><span style="color:gray;">(</span><span style="color:black;">PARTITION </span><span style="color:blue;">BY </span><span style="color:black;">SalesOrderID<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">ProductID </span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">PctDist<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Sales.SalesOrderDetail s<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">SalesOrderID </span><span style="color:blue;">IN </span><span style="color:gray;">(</span><span style="color:black;">43670</span><span style="color:gray;">, </span><span style="color:black;">43669</span><span style="color:gray;">, </span><span style="color:black;">43667</span><span style="color:gray;">, </span><span style="color:black;">43663</span><span style="color:gray;">)<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">PctDist </span><span style="color:blue;">DESC<br />
</span><span style="color:black;">GO<br />
</span></code></p>
<p style="text-align:justify;">Now you will notice that the same logic is followed in follow result set.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/percentrank1.jpg" alt="" width="501" height="284" /></p>
<p style="text-align:justify;">I have now quick question to you &#8211; how many of you know the logic/formula of PERCENT_RANK() before this blog post?</p>
<p style="text-align:justify;">Reference: <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>)</strong></p>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/postaday/'>PostADay</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-function/'>SQL Function</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/tech/sql-scripts/'>SQL Scripts</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/15944/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/15944/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/15944/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/15944/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/15944/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/15944/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/15944/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/15944/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/15944/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/15944/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/15944/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/15944/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/15944/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/15944/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=15944&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2011/11/18/sql-server-introduction-to-percent_rank-analytic-functions-introduced-in-sql-server-2012/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/percentrank.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/percentrank1.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Puzzle to Win Print Book &#8211; Write T-SQL Self Join Without Using LEAD and LAG</title>
		<link>http://blog.sqlauthority.com/2011/11/16/sql-server-puzzle-to-win-print-book-write-t-sql-self-join-without-using-first-_value-and-last_value/</link>
		<comments>http://blog.sqlauthority.com/2011/11/16/sql-server-puzzle-to-win-print-book-write-t-sql-self-join-without-using-first-_value-and-last_value/#comments</comments>
		<pubDate>Wed, 16 Nov 2011 01:30:23 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[PostADay]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Function]]></category>
		<category><![CDATA[SQL Puzzle]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=15845</guid>
		<description><![CDATA[Last week we asked a puzzle SQL SERVER – Puzzle to Win Print Book – Functions FIRST_VALUE and LAST_VALUE with OVER clause and ORDER BY . This puzzle got very interesting participation. The details of the winner is listed here. In this puzzle we received two very important feedback. This puzzle cleared the concepts of First_Value [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=15845&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">Last week we asked a puzzle <strong><a title="Permanent Link to SQL SERVER – Puzzle to Win Print Book – Functions FIRST_VALUE and LAST_VALUE with OVER clause and ORDER BY" href="http://blog.sqlauthority.com/2011/11/11/sql-server-puzzle-to-win-print-book-functions-first_value-and-last_value-with-over-clause-and-order-by/" rel="bookmark">SQL SERVER – Puzzle to Win Print Book – Functions FIRST_VALUE and LAST_VALUE with OVER clause and ORDER BY</a></strong> . This puzzle got very interesting participation. The details of the winner is listed <strong><a href="http://blog.sqlauthority.com/2011/11/11/sql-server-puzzle-to-win-print-book-functions-first_value-and-last_value-with-over-clause-and-order-by/#comment-196163" target="_blank">here</a></strong>.</p>
<p style="text-align:justify;">In this puzzle we received two very important feedback.</p>
<ol style="text-align:justify;">
<li>This puzzle cleared the concepts of First_Value and Last_Value to the participants.</li>
<li>As this was based on SQL Server 2012 many could not participate it as they have yet not installed SQL Server 2012.</li>
</ol>
<p style="text-align:justify;">I really appreciate the feedback of user and decided to come up something as fun and helps learn new feature of SQL Server 2012.</p>
<p style="text-align:justify;">Please read yesterday&#8217;s blog post <strong><a title="Permanent Link to SQL SERVER – Introduction to LEAD and LAG – Analytic Functions Introduced in SQL Server 2012" href="http://blog.sqlauthority.com/2011/11/15/sql-server-introduction-to-lead-and-lag-analytic-functions-introduced-in-sql-server-2012/" rel="bookmark">SQL SERVER – Introduction to LEAD and LAG – Analytic Functions Introduced in SQL Server 2012 </a></strong>before continuing this puzzle as it is based on yesterday&#8217;s post.</p>
<p style="text-align:justify;">Let us fun following query.<br />
<code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks<br />
GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">s.SalesOrderID</span><span style="color:gray;">,</span><span style="color:black;">s.SalesOrderDetailID</span><span style="color:gray;">,</span><span style="color:black;">s.OrderQty</span><span style="color:gray;">,<br />
</span><span style="color:black;">LEAD</span><span style="color:gray;">(</span><span style="color:black;">SalesOrderDetailID</span><span style="color:gray;">) </span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:blue;">ORDER BY </span><span style="color:black;">SalesOrderDetailID<br />
</span><span style="color:gray;">) </span><span style="color:black;">LeadValue</span><span style="color:gray;">,<br />
</span><span style="color:black;">LAG</span><span style="color:gray;">(</span><span style="color:black;">SalesOrderDetailID</span><span style="color:gray;">) </span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:blue;">ORDER BY </span><span style="color:black;">SalesOrderDetailID<br />
</span><span style="color:gray;">) </span><span style="color:black;">LagValue<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Sales.SalesOrderDetail s<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">SalesOrderID </span><span style="color:blue;">IN </span><span style="color:gray;">(</span><span style="color:black;">43670</span><span style="color:gray;">, </span><span style="color:black;">43669</span><span style="color:gray;">, </span><span style="color:black;">43667</span><span style="color:gray;">, </span><span style="color:black;">43663</span><span style="color:gray;">)<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">s.SalesOrderID</span><span style="color:gray;">,</span><span style="color:black;">s.SalesOrderDetailID</span><span style="color:gray;">,</span><span style="color:black;">s.OrderQty<br />
GO<br />
</span></code></p>
<p style="text-align:justify;">Above query will give us following result.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/leadlag1.jpg" alt="" width="500" /></p>
<h2 style="text-align:justify;"><strong>Puzzle: </strong></h2>
<p style="text-align:justify;">Now use T-SQL Self Join where same table is joined to itself and get the same result without using LEAD or LAG functions.</p>
<h2 style="text-align:justify;">Hint:</h2>
<ul style="text-align:justify;">
<li><strong><a href="http://blog.sqlauthority.com/2009/04/13/sql-server-introduction-to-joins-basic-of-joins/" target="_blank">Introduction to JOINs – Basic of JOINs</a></strong></li>
<li><strong><a href="http://blog.sqlauthority.com/2010/07/08/sql-server-the-self-join-inner-join-and-outer-join/" target="_blank">Self Join</a></strong></li>
<li><strong><a href="http://beyondrelational.com/justlearned/posts/431/a-new-analytic-functions-in-sql-server-denali-ctp3-lead-and-lag.aspx" target="_blank">A new analytic functions in SQL Server Denali CTP3 &#8211; LEAD() and LAG()</a></strong></li>
</ul>
<h2 style="text-align:justify;">Rules</h2>
<ul style="text-align:justify;">
<li>Leave a comment with your detailed answer by Nov 21′s blog post.</li>
<li>Open world-wide (where Amazon ships books)</li>
<li>If you blog about puzzle’s solution and if you win, you win additional surprise gift as well.</li>
</ul>
<h2 style="text-align:justify;">Prizes</h2>
<p style="text-align:justify;"><a href="http://blog.sqlauthority.com/sql-server-books/sql-server-interview-questions-and-answers-for-all-database-developers-and-developers-administrators/" target="_blank"><img src="http://www.pinaldave.com/bimg/sqlinterview_s" alt="" width="169" height="258" /></a></p>
<p style="text-align:justify;">Print copy of my new book SQL Server Interview Questions <strong><a href="http://amzn.to/qTOXoG" target="_blank">Amazon</a>|<a href="http://bit.ly/indiasqlinterview" target="_blank">Flipkart</a></strong></p>
<p style="text-align:justify;">If you already have this book, you can opt for any of my other books SQL Wait Stats [<strong><a href="http://amzn.to/p3tL1n" target="_blank">Amazon</a>|<a href="http://amzn.to/waitstatskindle" target="_blank">Flipkart</a>|<a href="http://amzn.to/waitstatskindle" target="_blank">Kindle</a></strong>] and SQL Programming [<strong><a href="http://amzn.to/rsC2La" target="_blank">Amazon</a>|<a href="http://bit.ly/pinalbook4" target="_blank">Flipkart</a>|<a href="http://amzn.to/sqlbook4onkindle" target="_blank">Kindle</a></strong>].</p>
<p style="text-align:justify;">Reference: <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>)</strong></p>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/postaday/'>PostADay</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-function/'>SQL Function</a>, <a href='http://blog.sqlauthority.com/category/sql-puzzle/'>SQL Puzzle</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/tech/sql-scripts/'>SQL Scripts</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/15845/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/15845/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/15845/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/15845/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/15845/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/15845/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/15845/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/15845/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/15845/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/15845/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/15845/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/15845/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/15845/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/15845/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=15845&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2011/11/16/sql-server-puzzle-to-win-print-book-write-t-sql-self-join-without-using-first-_value-and-last_value/feed/</wfw:commentRss>
		<slash:comments>19</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/leadlag1.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/sqlinterview_s" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Introduction to LEAD and LAG &#8211; Analytic Functions Introduced in SQL Server 2012</title>
		<link>http://blog.sqlauthority.com/2011/11/15/sql-server-introduction-to-lead-and-lag-analytic-functions-introduced-in-sql-server-2012/</link>
		<comments>http://blog.sqlauthority.com/2011/11/15/sql-server-introduction-to-lead-and-lag-analytic-functions-introduced-in-sql-server-2012/#comments</comments>
		<pubDate>Tue, 15 Nov 2011 01:30:03 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[PostADay]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Function]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=15772</guid>
		<description><![CDATA[SQL Server 2012 introduces new analytical function LEAD() and LAG(). This functions accesses data from a subsequent row (for lead) and previous row (for lag) in the same result set without the use of a self-join . It will be very difficult to explain this in words so I will attempt small example to explain [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=15772&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">SQL Server 2012 introduces new analytical function LEAD() and LAG(). This functions accesses data from a subsequent row (for lead) and previous row (for lag) in the same result set without the use of a self-join . It will be very difficult to explain this in words so I will attempt small example to explain you this function. Instead of creating new table, I will be using AdventureWorks sample database as most of the developer uses that for experiment.</p>
<p style="text-align:justify;">Let us fun following query.<br />
<code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks<br />
GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">s.SalesOrderID</span><span style="color:gray;">,</span><span style="color:black;">s.SalesOrderDetailID</span><span style="color:gray;">,</span><span style="color:black;">s.OrderQty</span><span style="color:gray;">,<br />
</span><span style="color:black;">LEAD</span><span style="color:gray;">(</span><span style="color:black;">SalesOrderDetailID</span><span style="color:gray;">) </span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:blue;">ORDER BY </span><span style="color:black;">SalesOrderDetailID<br />
</span><span style="color:gray;">) </span><span style="color:black;">LeadValue</span><span style="color:gray;">,<br />
</span><span style="color:black;">LAG</span><span style="color:gray;">(</span><span style="color:black;">SalesOrderDetailID</span><span style="color:gray;">) </span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:blue;">ORDER BY </span><span style="color:black;">SalesOrderDetailID<br />
</span><span style="color:gray;">) </span><span style="color:black;">LagValue<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Sales.SalesOrderDetail s<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">SalesOrderID </span><span style="color:blue;">IN </span><span style="color:gray;">(</span><span style="color:black;">43670</span><span style="color:gray;">, </span><span style="color:black;">43669</span><span style="color:gray;">, </span><span style="color:black;">43667</span><span style="color:gray;">, </span><span style="color:black;">43663</span><span style="color:gray;">)<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">s.SalesOrderID</span><span style="color:gray;">,</span><span style="color:black;">s.SalesOrderDetailID</span><span style="color:gray;">,</span><span style="color:black;">s.OrderQty<br />
GO<br />
</span></code></p>
<p style="text-align:justify;">Above query will give us following result.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/leadlag1.jpg" alt="" width="500" /></p>
<p style="text-align:justify;">When we look at above resultset it is very clear that LEAD function gives us value which is going to come in next line and LAG function gives us value which was encountered in previous line. If we have to generate the same result without using this function we will have to use self join. In future blog post we will see the same. Let us explore this function a bit more. This function not only provide previous or next line but it can also access any line before or after using offset.</p>
<p style="text-align:justify;">Let us fun following query, where LEAD and LAG function accesses the row with offset of 2.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks<br />
GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">s.SalesOrderID</span><span style="color:gray;">,</span><span style="color:black;">s.SalesOrderDetailID</span><span style="color:gray;">,</span><span style="color:black;">s.OrderQty</span><span style="color:gray;">,<br />
</span><span style="color:black;">LEAD</span><span style="color:gray;">(</span><span style="color:black;">SalesOrderDetailID</span><span style="color:gray;">,</span><span style="color:black;">2</span><span style="color:gray;">) </span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:blue;">ORDER BY </span><span style="color:black;">SalesOrderDetailID<br />
</span><span style="color:gray;">) </span><span style="color:black;">LeadValue</span><span style="color:gray;">,<br />
</span><span style="color:black;">LAG</span><span style="color:gray;">(</span><span style="color:black;">SalesOrderDetailID</span><span style="color:gray;">,</span><span style="color:black;">2</span><span style="color:gray;">) </span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:blue;">ORDER BY </span><span style="color:black;">SalesOrderDetailID<br />
</span><span style="color:gray;">) </span><span style="color:black;">LagValue<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Sales.SalesOrderDetail s<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">SalesOrderID </span><span style="color:blue;">IN </span><span style="color:gray;">(</span><span style="color:black;">43670</span><span style="color:gray;">, </span><span style="color:black;">43669</span><span style="color:gray;">, </span><span style="color:black;">43667</span><span style="color:gray;">, </span><span style="color:black;">43663</span><span style="color:gray;">)<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">s.SalesOrderID</span><span style="color:gray;">,</span><span style="color:black;">s.SalesOrderDetailID</span><span style="color:gray;">,</span><span style="color:black;">s.OrderQty<br />
GO</span></code></p>
<p style="text-align:justify;">Above query will give us following result.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/leadlag2.jpg" alt="" width="500" /></p>
<p style="text-align:justify;">You can see the LEAD and LAG functions  now have interval of  rows when they are returning results. As there is interval of two rows the first two rows in LEAD function and last two rows in LAG function will return NULL value. You can easily replace this NULL Value with any other default value by passing third parameter in LEAD and LAG function.</p>
<p style="text-align:justify;">Let us fun following query.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks<br />
GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">s.SalesOrderID</span><span style="color:gray;">,</span><span style="color:black;">s.SalesOrderDetailID</span><span style="color:gray;">,</span><span style="color:black;">s.OrderQty</span><span style="color:gray;">,<br />
</span><span style="color:black;">LEAD</span><span style="color:gray;">(</span><span style="color:black;">SalesOrderDetailID</span><span style="color:gray;">,</span><span style="color:black;">2</span><span style="color:gray;">,</span><span style="color:black;">0</span><span style="color:gray;">) </span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:blue;">ORDER BY </span><span style="color:black;">SalesOrderDetailID<br />
</span><span style="color:gray;">) </span><span style="color:black;">LeadValue</span><span style="color:gray;">,<br />
</span><span style="color:black;">LAG</span><span style="color:gray;">(</span><span style="color:black;">SalesOrderDetailID</span><span style="color:gray;">,</span><span style="color:black;">2</span><span style="color:gray;">,</span><span style="color:black;">0</span><span style="color:gray;">) </span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:blue;">ORDER BY </span><span style="color:black;">SalesOrderDetailID<br />
</span><span style="color:gray;">) </span><span style="color:black;">LagValue<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Sales.SalesOrderDetail s<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">SalesOrderID </span><span style="color:blue;">IN </span><span style="color:gray;">(</span><span style="color:black;">43670</span><span style="color:gray;">, </span><span style="color:black;">43669</span><span style="color:gray;">, </span><span style="color:black;">43667</span><span style="color:gray;">, </span><span style="color:black;">43663</span><span style="color:gray;">)<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">s.SalesOrderID</span><span style="color:gray;">,</span><span style="color:black;">s.SalesOrderDetailID</span><span style="color:gray;">,</span><span style="color:black;">s.OrderQty<br />
GO</span></code></p>
<p style="text-align:justify;">Above query will give us following result, where NULL are now replaced with value 0.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/leadlag3.jpg" alt="" width="500" /></p>
<p style="text-align:justify;">Just like any other analytic function we can easily partition this function as well. Let us see the use of PARTITION BY in this clause.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks<br />
GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">s.SalesOrderID</span><span style="color:gray;">,</span><span style="color:black;">s.SalesOrderDetailID</span><span style="color:gray;">,</span><span style="color:black;">s.OrderQty</span><span style="color:gray;">,<br />
</span><span style="color:black;">LEAD</span><span style="color:gray;">(</span><span style="color:black;">SalesOrderDetailID</span><span style="color:gray;">) </span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:black;">PARTITION </span><span style="color:blue;">BY </span><span style="color:black;">SalesOrderID<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">SalesOrderDetailID<br />
</span><span style="color:gray;">) </span><span style="color:black;">LeadValue</span><span style="color:gray;">,<br />
</span><span style="color:black;">LAG</span><span style="color:gray;">(</span><span style="color:black;">SalesOrderDetailID</span><span style="color:gray;">) </span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:black;">PARTITION </span><span style="color:blue;">BY </span><span style="color:black;">SalesOrderID<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">SalesOrderDetailID<br />
</span><span style="color:gray;">) </span><span style="color:black;">LagValue<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Sales.SalesOrderDetail s<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">SalesOrderID </span><span style="color:blue;">IN </span><span style="color:gray;">(</span><span style="color:black;">43670</span><span style="color:gray;">, </span><span style="color:black;">43669</span><span style="color:gray;">, </span><span style="color:black;">43667</span><span style="color:gray;">, </span><span style="color:black;">43663</span><span style="color:gray;">)<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">s.SalesOrderID</span><span style="color:gray;">,</span><span style="color:black;">s.SalesOrderDetailID</span><span style="color:gray;">,</span><span style="color:black;">s.OrderQty<br />
GO</span></code></p>
<p style="text-align:justify;">Above query will give us following result, where now the data is partitioned by SalesOrderID and LEAD and LAG functions are returning the appropriate result in that window. As now there are smaller partition in my query, you will see higher presence of NULL.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/leadlag4.jpg" alt="" width="500" /></p>
<p style="text-align:justify;">In future blog post we will see how this functions are compared to SELF JOIN.</p>
<p style="text-align:justify;">Reference: <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>)</strong></p>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/postaday/'>PostADay</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-function/'>SQL Function</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/tech/sql-scripts/'>SQL Scripts</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/15772/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/15772/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/15772/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/15772/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/15772/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/15772/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/15772/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/15772/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/15772/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/15772/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/15772/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/15772/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/15772/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/15772/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=15772&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2011/11/15/sql-server-introduction-to-lead-and-lag-analytic-functions-introduced-in-sql-server-2012/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/leadlag1.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/leadlag2.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/leadlag3.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/leadlag4.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Puzzle to Win Print Book &#8211; Functions FIRST_VALUE and LAST_VALUE with OVER clause and ORDER BY</title>
		<link>http://blog.sqlauthority.com/2011/11/11/sql-server-puzzle-to-win-print-book-functions-first_value-and-last_value-with-over-clause-and-order-by/</link>
		<comments>http://blog.sqlauthority.com/2011/11/11/sql-server-puzzle-to-win-print-book-functions-first_value-and-last_value-with-over-clause-and-order-by/#comments</comments>
		<pubDate>Fri, 11 Nov 2011 01:30:02 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[PostADay]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Function]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=15697</guid>
		<description><![CDATA[Some time an interesting feature and smart audience makes total difference at places. From last two days, I have been writing on SQL Server 2012 feature FIRST_VALUE and LAST_VALUE. Please read following post before I continue today as this question is based on the same. Introduction to FIRST_VALUE and LAST_VALUE Introduction to FIRST_VALUE and LAST_VALUE [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=15697&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">Some time an interesting feature and smart audience makes total difference at places. From last two days, I have been writing on SQL Server 2012 feature FIRST_VALUE and LAST_VALUE.</p>
<p style="text-align:justify;">Please read following post before I continue today as this question is based on the same.</p>
<ul style="text-align:justify;">
<li><strong><a href="http://blog.sqlauthority.com/2011/11/09/sql-server-introduction-to-first-_value-and-last_value-analytic-functions-introduced-in-sql-server-2012/" target="_blank">Introduction to FIRST_VALUE and LAST_VALUE</a></strong></li>
<li><strong><a href="http://blog.sqlauthority.com/2011/11/10/sql-server-over-clause-with-first-_value-and-last_value-analytic-functions-introduced-in-sql-server-2012-rows-between-unbounded-preceding-and-unbounded-following/" target="_blank">Introduction to FIRST_VALUE and LAST_VALUE with OVER clause</a></strong></li>
</ul>
<p style="text-align:justify;">As a comment of the second post I received excellent question from <a href="http://blog.sqlauthority.com/2011/11/10/sql-server-over-clause-with-first-_value-and-last_value-analytic-functions-introduced-in-sql-server-2012-rows-between-unbounded-preceding-and-unbounded-following/#comment-192018" target="_blank">Nilesh Molankar</a>. He asks what will happen if we change few things in the T-SQL. I really like this question as this kind of questions will make us sharp and help us perform in critical situation in need. We recently publish <strong><a href="http://blog.sqlauthority.com/sql-server-books/sql-server-interview-questions-and-answers-for-all-database-developers-and-developers-administrators/" target="_blank">SQL Server Interview Questions</a></strong> book. I promise that in future version of this book, we will for sure include this question. Instead of repeating his question, I am going to ask something very similar to his question.</p>
<p style="text-align:justify;">Let us first run following query (read <strong><a href="http://blog.sqlauthority.com/2011/11/10/sql-server-over-clause-with-first-_value-and-last_value-analytic-functions-introduced-in-sql-server-2012-rows-between-unbounded-preceding-and-unbounded-following/" target="_blank">yesterday&#8217;s blog post</a></strong> for more detail):</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks<br />
GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">s.SalesOrderID</span><span style="color:gray;">,</span><span style="color:black;">s.SalesOrderDetailID</span><span style="color:gray;">,</span><span style="color:black;">s.OrderQty</span><span style="color:gray;">,<br />
</span><span style="color:black;">FIRST_VALUE</span><span style="color:gray;">(</span><span style="color:black;">SalesOrderDetailID</span><span style="color:gray;">) </span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:black;">PARTITION </span><span style="color:blue;">BY </span><span style="color:black;">SalesOrderID<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">SalesOrderDetailID<br />
ROWS </span><span style="color:gray;">BETWEEN </span><span style="color:black;">UNBOUNDED PRECEDING </span><span style="color:gray;">AND </span><span style="color:black;">UNBOUNDED FOLLOWING</span><span style="color:gray;">) </span><span style="color:black;">FstValue</span><span style="color:gray;">,<br />
</span><span style="color:black;">LAST_VALUE</span><span style="color:gray;">(</span><span style="color:black;">SalesOrderDetailID</span><span style="color:gray;">) </span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:black;">PARTITION </span><span style="color:blue;">BY </span><span style="color:black;">SalesOrderID<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">SalesOrderDetailID<br />
ROWS </span><span style="color:gray;">BETWEEN </span><span style="color:black;">UNBOUNDED PRECEDING </span><span style="color:gray;">AND </span><span style="color:black;">UNBOUNDED FOLLOWING</span><span style="color:gray;">) </span><span style="color:black;">LstValue<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Sales.SalesOrderDetail s<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">SalesOrderID </span><span style="color:blue;">IN </span><span style="color:gray;">(</span><span style="color:black;">43670</span><span style="color:gray;">, </span><span style="color:black;">43669</span><span style="color:gray;">, </span><span style="color:black;">43667</span><span style="color:gray;">, </span><span style="color:black;">43663</span><span style="color:gray;">)<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">s.SalesOrderID</span><span style="color:gray;">,</span><span style="color:black;">s.SalesOrderDetailID</span><span style="color:gray;">,</span><span style="color:black;">s.OrderQty<br />
GO</span></code></p>
<p style="text-align:justify;">Here is the resultset of the above query.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/lastvalue1.jpg" alt="" width="491" height="264" /></p>
<p style="text-align:justify;">Now let us change the ORDER BY clause of OVER clause in above query and see what is the new result.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks<br />
GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">s.SalesOrderID</span><span style="color:gray;">,</span><span style="color:black;">s.SalesOrderDetailID</span><span style="color:gray;">,</span><span style="color:black;">s.OrderQty</span><span style="color:gray;">,<br />
</span><span style="color:black;">FIRST_VALUE</span><span style="color:gray;">(</span><span style="color:black;">SalesOrderDetailID</span><span style="color:gray;">) </span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:black;">PARTITION </span><span style="color:blue;">BY </span><span style="color:black;">SalesOrderID<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">OrderQty<br />
ROWS </span><span style="color:gray;">BETWEEN </span><span style="color:black;">UNBOUNDED PRECEDING </span><span style="color:gray;">AND </span><span style="color:black;">UNBOUNDED FOLLOWING</span><span style="color:gray;">) </span><span style="color:black;">FstValue</span><span style="color:gray;">,<br />
</span><span style="color:black;">LAST_VALUE</span><span style="color:gray;">(</span><span style="color:black;">SalesOrderDetailID</span><span style="color:gray;">) </span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:black;">PARTITION </span><span style="color:blue;">BY </span><span style="color:black;">SalesOrderID<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">OrderQty<br />
ROWS </span><span style="color:gray;">BETWEEN </span><span style="color:black;">UNBOUNDED PRECEDING </span><span style="color:gray;">AND </span><span style="color:black;">UNBOUNDED FOLLOWING</span><span style="color:gray;">) </span><span style="color:black;">LstValue<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Sales.SalesOrderDetail s<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">SalesOrderID </span><span style="color:blue;">IN </span><span style="color:gray;">(</span><span style="color:black;">43670</span><span style="color:gray;">, </span><span style="color:black;">43669</span><span style="color:gray;">, </span><span style="color:black;">43667</span><span style="color:gray;">, </span><span style="color:black;">43663</span><span style="color:gray;">)<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">s.SalesOrderID</span><span style="color:gray;">,</span><span style="color:black;">s.SalesOrderDetailID</span><span style="color:gray;">,</span><span style="color:black;">s.OrderQty<br />
GO</span></code></p>
<p style="text-align:justify;">Now let us see the result and ready for interesting question:</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/lastvalue4.jpg" alt="" width="498" height="259" /></p>
<h2 style="text-align:justify;">Puzzle</h2>
<p style="text-align:justify;">You can see that row number 2, 3, 4, and 5 has same SalesOrderID = 43667. The FIRST_VALUE is 78 and LAST_VALUE is 77. Now if these function was working on maximum and minimum value they should have given answer as 77 and 80 respectively instead of 78 and 77. Also the value of FIRST_VALUE is greater than LAST_VALUE 77. <strong>Why? Explain in detail.</strong></p>
<h2 style="text-align:justify;">Hint</h2>
<p style="text-align:justify;">Let me give you a simple hint. Just for simplicity I have changed the order of columns selected in the SELECT and ORDER BY (at the end). This will not change resultset but just order of the columns as well order of the rows. However, the data remains the same.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks<br />
GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">s.OrderQty</span><span style="color:gray;">,</span><span style="color:black;">s.SalesOrderID</span><span style="color:gray;">,</span><span style="color:black;">s.SalesOrderDetailID</span><span style="color:gray;">,<br />
</span><span style="color:black;">FIRST_VALUE</span><span style="color:gray;">(</span><span style="color:black;">SalesOrderDetailID</span><span style="color:gray;">) </span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:black;">PARTITION </span><span style="color:blue;">BY </span><span style="color:black;">SalesOrderID<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">OrderQty<br />
ROWS </span><span style="color:gray;">BETWEEN </span><span style="color:black;">UNBOUNDED PRECEDING </span><span style="color:gray;">AND </span><span style="color:black;">UNBOUNDED FOLLOWING</span><span style="color:gray;">) </span><span style="color:black;">FstValue</span><span style="color:gray;">,<br />
</span><span style="color:black;">LAST_VALUE</span><span style="color:gray;">(</span><span style="color:black;">SalesOrderDetailID</span><span style="color:gray;">) </span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:black;">PARTITION </span><span style="color:blue;">BY </span><span style="color:black;">SalesOrderID<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">OrderQty<br />
ROWS </span><span style="color:gray;">BETWEEN </span><span style="color:black;">UNBOUNDED PRECEDING </span><span style="color:gray;">AND </span><span style="color:black;">UNBOUNDED FOLLOWING</span><span style="color:gray;">) </span><span style="color:black;">LstValue<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Sales.SalesOrderDetail s<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">SalesOrderID </span><span style="color:blue;">IN </span><span style="color:gray;">(</span><span style="color:black;">43670</span><span style="color:gray;">, </span><span style="color:black;">43669</span><span style="color:gray;">, </span><span style="color:black;">43667</span><span style="color:gray;">, </span><span style="color:black;">43663</span><span style="color:gray;">)<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">s.OrderQty</span><span style="color:gray;">,</span><span style="color:black;">s.SalesOrderID</span><span style="color:gray;">,</span><span style="color:black;">s.SalesOrderDetailID<br />
GO<br />
</span></code></p>
<p style="text-align:justify;">Above query returns following result:</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/lastvalue3.jpg" alt="" width="502" height="251" /></p>
<p style="text-align:justify;">Now I am very sure all of you have figured out the solution. Here is the<strong> second hint</strong> &#8211; pay attention to row 2, 3, 4, and 10.</p>
<h2 style="text-align:justify;">Hint2</h2>
<ul style="text-align:justify;">
<li><a href="http://beyondrelational.com/blogs/martinschmidt/archive/2011/08/30/t-sql-enhancements-first-value-and-last-value.aspx" target="_blank">T-SQL Enhancements: FIRST_VALUE() and LAST_VALUE()</a></li>
<li>MSDN: <a href="http://bit.ly/sDyZc4" target="_blank">FIRST_VALUE</a> and <a href="http://bit.ly/tjqtrP" target="_blank">LAST_VALUE</a></li>
</ul>
<h2 style="text-align:justify;">Rules</h2>
<ul style="text-align:justify;">
<li>Leave a comment with your detailed answer by Nov 15&#8242;s blog post.</li>
<li>Open world-wide (where Amazon ships books)</li>
<li>If you blog about puzzle&#8217;s solution and if you win, you win additional surprise gift as well.</li>
</ul>
<h2 style="text-align:justify;">Prizes</h2>
<p style="text-align:justify;"><a href="http://blog.sqlauthority.com/sql-server-books/sql-server-interview-questions-and-answers-for-all-database-developers-and-developers-administrators/" target="_blank"><img class="alignnone" src="http://www.pinaldave.com/bimg/sqlinterview_s" alt="" width="169" height="258" /></a></p>
<p style="text-align:justify;">Print copy of my new book SQL Server Interview Questions <strong><a href="http://amzn.to/qTOXoG" target="_blank">Amazon</a>|<a href="http://bit.ly/indiasqlinterview" target="_blank">Flipkart</a></strong></p>
<p style="text-align:justify;">If you already have this book, you can opt for any of my other books SQL Wait Stats [<strong><a href="http://amzn.to/p3tL1n" target="_blank">Amazon</a>|<a href="http://amzn.to/waitstatskindle" target="_blank">Flipkart</a>|<a href="http://amzn.to/waitstatskindle" target="_blank">Kindle</a></strong>] and SQL Programming [<strong><a href="http://amzn.to/rsC2La" target="_blank">Amazon</a>|<a href="http://bit.ly/pinalbook4" target="_blank">Flipkart</a>|<a href="http://amzn.to/sqlbook4onkindle" target="_blank">Kindle</a></strong>].</p>
<p style="text-align:justify;">Reference: <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>)</strong></p>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/postaday/'>PostADay</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-function/'>SQL Function</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/tech/sql-scripts/'>SQL Scripts</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/15697/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/15697/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/15697/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/15697/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/15697/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/15697/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/15697/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/15697/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/15697/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/15697/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/15697/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/15697/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/15697/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/15697/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=15697&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2011/11/11/sql-server-puzzle-to-win-print-book-functions-first_value-and-last_value-with-over-clause-and-order-by/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/lastvalue1.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/lastvalue4.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/lastvalue3.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/sqlinterview_s" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; OVER clause with FIRST _VALUE and LAST_VALUE  &#8211; Analytic Functions Introduced in SQL Server 2012 &#8211; ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING</title>
		<link>http://blog.sqlauthority.com/2011/11/10/sql-server-over-clause-with-first-_value-and-last_value-analytic-functions-introduced-in-sql-server-2012-rows-between-unbounded-preceding-and-unbounded-following/</link>
		<comments>http://blog.sqlauthority.com/2011/11/10/sql-server-over-clause-with-first-_value-and-last_value-analytic-functions-introduced-in-sql-server-2012-rows-between-unbounded-preceding-and-unbounded-following/#comments</comments>
		<pubDate>Thu, 10 Nov 2011 01:30:28 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[PostADay]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Function]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=15683</guid>
		<description><![CDATA[Yesterday I had discussed two analytical functions FIRST_VALUE and LAST_VALUE. After reading the blog post I received very interesting question. &#8220;Don&#8217;t you think there is bug in your first example where FIRST_VALUE is remain same but the LAST_VALUE is changing every line. I think the LAST_VALUE should be the highest value in the windows or [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=15683&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">Yesterday I had discussed two analytical functions FIRST_VALUE and LAST_VALUE. After reading the blog post I received very interesting question.</p>
<p style="text-align:justify;">&#8220;Don&#8217;t you think there is bug in your first example where FIRST_VALUE is remain same but the LAST_VALUE is changing every line. I think the LAST_VALUE should be the highest value in the windows or set of result.&#8221;</p>
<p style="text-align:justify;">I find this question very interesting because this is very commonly made mistake. No there is no bug in the code. I think what we need is a bit more explanation. Let me attempt that first. Before you do that I suggest you <strong><a href="http://blog.sqlauthority.com/2011/11/09/sql-server-introduction-to-first-_value-and-last_value-analytic-functions-introduced-in-sql-server-2012/" target="_blank">read yesterday&#8217;s blog post</a></strong> as this question is related to that blog post.</p>
<p style="text-align:justify;">Now let’s have fun following query:</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks<br />
GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">s.SalesOrderID</span><span style="color:gray;">,</span><span style="color:black;">s.SalesOrderDetailID</span><span style="color:gray;">,</span><span style="color:black;">s.OrderQty</span><span style="color:gray;">,<br />
</span><span style="color:black;">FIRST_VALUE</span><span style="color:gray;">(</span><span style="color:black;">SalesOrderDetailID</span><span style="color:gray;">) </span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:blue;">ORDER BY </span><span style="color:black;">SalesOrderDetailID</span><span style="color:gray;">) </span><span style="color:black;">FstValue</span><span style="color:gray;">,<br />
</span><span style="color:black;">LAST_VALUE</span><span style="color:gray;">(</span><span style="color:black;">SalesOrderDetailID</span><span style="color:gray;">) </span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:blue;">ORDER BY </span><span style="color:black;">SalesOrderDetailID</span><span style="color:gray;">) </span><span style="color:black;">LstValue<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Sales.SalesOrderDetail s<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">SalesOrderID </span><span style="color:blue;">IN </span><span style="color:gray;">(</span><span style="color:black;">43670</span><span style="color:gray;">, </span><span style="color:black;">43669</span><span style="color:gray;">, </span><span style="color:black;">43667</span><span style="color:gray;">, </span><span style="color:black;">43663</span><span style="color:gray;">)<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">s.SalesOrderID</span><span style="color:gray;">,</span><span style="color:black;">s.SalesOrderDetailID</span><span style="color:gray;">,</span><span style="color:black;">s.OrderQty<br />
GO<br />
</span></code></p>
<p style="text-align:justify;">The above query will give us the following result:</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/firstvalue1.jpg" alt="" width="500" height="278" /></p>
<p style="text-align:justify;">As per the reader&#8217;s question the value of the LAST_VALUE function should be always 114 and not increasing as the rows are increased. Let me re-write the above code once again with bit extra T-SQL Syntax. Please pay special attention to the ROW clause which I have added in the above syntax.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks<br />
GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">s.SalesOrderID</span><span style="color:gray;">,</span><span style="color:black;">s.SalesOrderDetailID</span><span style="color:gray;">,</span><span style="color:black;">s.OrderQty</span><span style="color:gray;">,<br />
</span><span style="color:black;">FIRST_VALUE</span><span style="color:gray;">(</span><span style="color:black;">SalesOrderDetailID</span><span style="color:gray;">) </span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:blue;">ORDER BY </span><span style="color:black;">SalesOrderDetailID<br />
ROWS </span><span style="color:gray;">BETWEEN </span><span style="color:black;">UNBOUNDED PRECEDING </span><span style="color:gray;">AND </span><span style="color:blue;">CURRENT </span><span style="color:black;">ROW</span><span style="color:gray;">) </span><span style="color:black;">FstValue</span><span style="color:gray;">,<br />
</span><span style="color:black;">LAST_VALUE</span><span style="color:gray;">(</span><span style="color:black;">SalesOrderDetailID</span><span style="color:gray;">) </span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:blue;">ORDER BY </span><span style="color:black;">SalesOrderDetailID<br />
ROWS </span><span style="color:gray;">BETWEEN </span><span style="color:black;">UNBOUNDED PRECEDING </span><span style="color:gray;">AND </span><span style="color:blue;">CURRENT </span><span style="color:black;">ROW</span><span style="color:gray;">) </span><span style="color:black;">LstValue<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Sales.SalesOrderDetail s<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">SalesOrderID </span><span style="color:blue;">IN </span><span style="color:gray;">(</span><span style="color:black;">43670</span><span style="color:gray;">, </span><span style="color:black;">43669</span><span style="color:gray;">, </span><span style="color:black;">43667</span><span style="color:gray;">, </span><span style="color:black;">43663</span><span style="color:gray;">)<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">s.SalesOrderID</span><span style="color:gray;">,</span><span style="color:black;">s.SalesOrderDetailID</span><span style="color:gray;">,</span><span style="color:black;">s.OrderQty<br />
GO<br />
</span></code></p>
<p style="text-align:justify;">Now once again check the result of the above query.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/firstvalue1.jpg" alt="" width="500" height="278" /></p>
<p style="text-align:justify;">The result of both the query is same because in OVER clause the default ROWS selection is always UNBOUNDED PRECEDING AND CURRENT ROW.</p>
<p style="text-align:justify;">If you want the maximum value of the windows with OVER clause you need to change the syntax to UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING for ROW clause. Now run following query and pay special attention to ROW clause again.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks<br />
GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">s.SalesOrderID</span><span style="color:gray;">,</span><span style="color:black;">s.SalesOrderDetailID</span><span style="color:gray;">,</span><span style="color:black;">s.OrderQty</span><span style="color:gray;">,<br />
</span><span style="color:black;">FIRST_VALUE</span><span style="color:gray;">(</span><span style="color:black;">SalesOrderDetailID</span><span style="color:gray;">) </span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:black;">PARTITION </span><span style="color:blue;">BY </span><span style="color:black;">SalesOrderID<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">SalesOrderDetailID<br />
ROWS </span><span style="color:gray;">BETWEEN </span><span style="color:black;">UNBOUNDED PRECEDING </span><span style="color:gray;">AND </span><span style="color:black;">UNBOUNDED FOLLOWING</span><span style="color:gray;">) </span><span style="color:black;">FstValue</span><span style="color:gray;">,<br />
</span><span style="color:black;">LAST_VALUE</span><span style="color:gray;">(</span><span style="color:black;">SalesOrderDetailID</span><span style="color:gray;">) </span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:black;">PARTITION </span><span style="color:blue;">BY </span><span style="color:black;">SalesOrderID<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">SalesOrderDetailID<br />
ROWS </span><span style="color:gray;">BETWEEN </span><span style="color:black;">UNBOUNDED PRECEDING </span><span style="color:gray;">AND </span><span style="color:black;">UNBOUNDED FOLLOWING</span><span style="color:gray;">) </span><span style="color:black;">LstValue<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Sales.SalesOrderDetail s<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">SalesOrderID </span><span style="color:blue;">IN </span><span style="color:gray;">(</span><span style="color:black;">43670</span><span style="color:gray;">, </span><span style="color:black;">43669</span><span style="color:gray;">, </span><span style="color:black;">43667</span><span style="color:gray;">, </span><span style="color:black;">43663</span><span style="color:gray;">)<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">s.SalesOrderID</span><span style="color:gray;">,</span><span style="color:black;">s.SalesOrderDetailID</span><span style="color:gray;">,</span><span style="color:black;">s.OrderQty<br />
GO</span></code></p>
<p style="text-align:justify;">Here is the resultset of the above query which is what questioner was asking. So in simple word, there is no bug but there is additional syntax needed to add to get your desired answer.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/lastvalue1.jpg" alt="" width="491" height="264" /></p>
<p style="text-align:justify;">The same logic also applies to PARTITION BY clause when used. Here is quick example of how we can further partition the query by SalesOrderDetailID with this new functions.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks<br />
GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">s.SalesOrderID</span><span style="color:gray;">,</span><span style="color:black;">s.SalesOrderDetailID</span><span style="color:gray;">,</span><span style="color:black;">s.OrderQty</span><span style="color:gray;">,<br />
</span><span style="color:black;">FIRST_VALUE</span><span style="color:gray;">(</span><span style="color:black;">SalesOrderDetailID</span><span style="color:gray;">) </span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:black;">PARTITION </span><span style="color:blue;">BY </span><span style="color:black;">SalesOrderID<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">SalesOrderDetailID<br />
ROWS </span><span style="color:gray;">BETWEEN </span><span style="color:black;">UNBOUNDED PRECEDING </span><span style="color:gray;">AND </span><span style="color:black;">UNBOUNDED FOLLOWING</span><span style="color:gray;">) </span><span style="color:black;">FstValue</span><span style="color:gray;">,<br />
</span><span style="color:black;">LAST_VALUE</span><span style="color:gray;">(</span><span style="color:black;">SalesOrderDetailID</span><span style="color:gray;">) </span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:black;">PARTITION </span><span style="color:blue;">BY </span><span style="color:black;">SalesOrderID<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">SalesOrderDetailID<br />
ROWS </span><span style="color:gray;">BETWEEN </span><span style="color:black;">UNBOUNDED PRECEDING </span><span style="color:gray;">AND </span><span style="color:black;">UNBOUNDED FOLLOWING</span><span style="color:gray;">) </span><span style="color:black;">LstValue<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Sales.SalesOrderDetail s<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">SalesOrderID </span><span style="color:blue;">IN </span><span style="color:gray;">(</span><span style="color:black;">43670</span><span style="color:gray;">, </span><span style="color:black;">43669</span><span style="color:gray;">, </span><span style="color:black;">43667</span><span style="color:gray;">, </span><span style="color:black;">43663</span><span style="color:gray;">)<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">s.SalesOrderID</span><span style="color:gray;">,</span><span style="color:black;">s.SalesOrderDetailID</span><span style="color:gray;">,</span><span style="color:black;">s.OrderQty<br />
GO</span></code></p>
<p style="text-align:justify;">Above query will give us windowed resultset on SalesOrderDetailsID as well give us FIRST and LAST value for the windowed resultset.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/lastvalue2.jpg" alt="" width="500" height="271" /></p>
<p style="text-align:justify;">There are lots to discuss for this two functions and we have just explored tip of the iceberg. In future post I will discover it further deep.</p>
<p style="text-align:justify;">Reference: <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>)</strong></p>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/postaday/'>PostADay</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-function/'>SQL Function</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/tech/sql-scripts/'>SQL Scripts</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/15683/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/15683/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/15683/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/15683/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/15683/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/15683/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/15683/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/15683/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/15683/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/15683/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/15683/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/15683/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/15683/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/15683/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=15683&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2011/11/10/sql-server-over-clause-with-first-_value-and-last_value-analytic-functions-introduced-in-sql-server-2012-rows-between-unbounded-preceding-and-unbounded-following/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/firstvalue1.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/firstvalue1.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/lastvalue1.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/lastvalue2.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Introduction to FIRST _VALUE and LAST_VALUE  &#8211; Analytic Functions Introduced in SQL Server 2012</title>
		<link>http://blog.sqlauthority.com/2011/11/09/sql-server-introduction-to-first-_value-and-last_value-analytic-functions-introduced-in-sql-server-2012/</link>
		<comments>http://blog.sqlauthority.com/2011/11/09/sql-server-introduction-to-first-_value-and-last_value-analytic-functions-introduced-in-sql-server-2012/#comments</comments>
		<pubDate>Wed, 09 Nov 2011 01:30:37 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[PostADay]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Function]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=15669</guid>
		<description><![CDATA[SQL Server 2012 introduces new analytical functions FIRST_VALUE() and LAST_VALUE(). This function returns first and last value from the list. It will be very difficult to explain this in words so I’d like to attempt to explain its function through a brief example. Instead of creating a new table, I will be using the AdventureWorks [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=15669&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">SQL Server 2012 introduces new analytical functions FIRST_VALUE() and LAST_VALUE(). This function returns first and last value from the list. It will be very difficult to explain this in words so I’d like to attempt to explain its function through a brief example. Instead of creating a new table, I will be using the AdventureWorks sample database as most developers use that for experiment purposes.</p>
<p style="text-align:justify;">Now let’s have fun following query:</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks<br />
GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">s.SalesOrderID</span><span style="color:gray;">,</span><span style="color:black;">s.SalesOrderDetailID</span><span style="color:gray;">,</span><span style="color:black;">s.OrderQty</span><span style="color:gray;">,<br />
</span><span style="color:black;">FIRST_VALUE</span><span style="color:gray;">(</span><span style="color:black;">SalesOrderDetailID</span><span style="color:gray;">) </span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:blue;">ORDER BY </span><span style="color:black;">SalesOrderDetailID</span><span style="color:gray;">) </span><span style="color:black;">FstValue</span><span style="color:gray;">,<br />
</span><span style="color:black;">LAST_VALUE</span><span style="color:gray;">(</span><span style="color:black;">SalesOrderDetailID</span><span style="color:gray;">) </span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:blue;">ORDER BY </span><span style="color:black;">SalesOrderDetailID</span><span style="color:gray;">) </span><span style="color:black;">LstValue<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Sales.SalesOrderDetail s<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">SalesOrderID </span><span style="color:blue;">IN </span><span style="color:gray;">(</span><span style="color:black;">43670</span><span style="color:gray;">, </span><span style="color:black;">43669</span><span style="color:gray;">, </span><span style="color:black;">43667</span><span style="color:gray;">, </span><span style="color:black;">43663</span><span style="color:gray;">)<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">s.SalesOrderID</span><span style="color:gray;">,</span><span style="color:black;">s.SalesOrderDetailID</span><span style="color:gray;">,</span><span style="color:black;">s.OrderQty<br />
GO<br />
</span></code></p>
<p style="text-align:justify;">The above query will give us the following result:</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/firstvalue1.jpg" alt="" width="500" height="278" /></p>
<p style="text-align:justify;">What’s the most interesting thing here is that as we go from row 1 to row 10, the value of the FIRST_VALUE() remains the same but the value of the LAST_VALUE is increasing. The reason behind this is that as we progress in every line &#8211; considering that line and all the other lines before it, the last value will be of the row where we are currently looking at. To fully understand this statement, see the following figure:</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/firstvalue2.jpg" alt="" width="500" height="287" /></p>
<p style="text-align:justify;">This may be useful in some cases; but not always. However, when we use the same thing with PARTITION BY, the same query starts showing the result which can be easily used in analytical algorithms and needs.</p>
<p style="text-align:justify;">Let us have fun through the following query:</p>
<p style="text-align:justify;">Let us fun following query.<br />
<code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks<br />
GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">s.SalesOrderID</span><span style="color:gray;">,</span><span style="color:black;">s.SalesOrderDetailID</span><span style="color:gray;">,</span><span style="color:black;">s.OrderQty</span><span style="color:gray;">,<br />
</span><span style="color:black;">FIRST_VALUE</span><span style="color:gray;">(</span><span style="color:black;">SalesOrderDetailID</span><span style="color:gray;">) </span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:black;">PARTITION </span><span style="color:blue;">BY </span><span style="color:black;">SalesOrderID<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">SalesOrderDetailID</span><span style="color:gray;">) </span><span style="color:black;">FstValue</span><span style="color:gray;">,<br />
</span><span style="color:black;">LAST_VALUE</span><span style="color:gray;">(</span><span style="color:black;">SalesOrderDetailID</span><span style="color:gray;">) </span><span style="color:blue;">OVER </span><span style="color:gray;">(</span><span style="color:black;">PARTITION </span><span style="color:blue;">BY </span><span style="color:black;">SalesOrderID<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">SalesOrderDetailID</span><span style="color:gray;">) </span><span style="color:black;">LstValue<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Sales.SalesOrderDetail s<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">SalesOrderID </span><span style="color:blue;">IN </span><span style="color:gray;">(</span><span style="color:black;">43670</span><span style="color:gray;">, </span><span style="color:black;">43669</span><span style="color:gray;">, </span><span style="color:black;">43667</span><span style="color:gray;">, </span><span style="color:black;">43663</span><span style="color:gray;">)<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">s.SalesOrderID</span><span style="color:gray;">,</span><span style="color:black;">s.SalesOrderDetailID</span><span style="color:gray;">,</span><span style="color:black;">s.OrderQty<br />
GO</span></code></p>
<p style="text-align:justify;">The above query will give us the following result:</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/firstvalue3.jpg" alt="" width="496" height="249" /></p>
<p style="text-align:justify;">Let us understand how PARTITION BY windows the resultset.</p>
<p style="text-align:justify;">I have used PARTITION BY SalesOrderID in my query. This will create small windows of the resultset from the original resultset and will follow the logic or FIRST_VALUE and LAST_VALUE in this resultset.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/firstvalue4.jpg" alt="" width="496" height="249" /></p>
<p style="text-align:justify;">Well, this is just an introduction to these functions. In the future blog posts we will go deeper to discuss the usage of these two functions. By the way, these functions can be applied over VARCHAR fields as well and are not limited to the numeric field only.</p>
<p style="text-align:justify;">Reference: <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>)</strong></p>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/postaday/'>PostADay</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-function/'>SQL Function</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/tech/sql-scripts/'>SQL Scripts</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/15669/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/15669/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/15669/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/15669/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/15669/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/15669/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/15669/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/15669/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/15669/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/15669/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/15669/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/15669/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/15669/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/15669/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=15669&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2011/11/09/sql-server-introduction-to-first-_value-and-last_value-analytic-functions-introduced-in-sql-server-2012/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/firstvalue1.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/firstvalue2.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/firstvalue3.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/firstvalue4.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Introduction to CUME_DIST &#8211; Analytic Functions Introduced in SQL Server 2012</title>
		<link>http://blog.sqlauthority.com/2011/11/08/sql-server-introduction-to-cume_dist-analytic-functions-introduced-in-sql-server-2012/</link>
		<comments>http://blog.sqlauthority.com/2011/11/08/sql-server-introduction-to-cume_dist-analytic-functions-introduced-in-sql-server-2012/#comments</comments>
		<pubDate>Tue, 08 Nov 2011 01:30:07 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[PostADay]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Function]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=15651</guid>
		<description><![CDATA[This blog post is written in response to the T-SQL Tuesday post of Prox ‘n’ Funx. This is a very interesting subject. By the way Brad Schulz is my favorite guy when it is about blogging. I respect him as well learn a lot from him. Everybody is writing something new his subject, I decided [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=15651&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;"><a href="http://bradsruminations.blogspot.com/2011/10/invitation-for-t-sql-tuesday-024-prox-n.html" target="_blank"><img class="alignleft" src="http://www.pinaldave.com/bimg/TSQL2sDay.jpg" alt="" width="123" height="123" /></a>This blog post is written in response to the T-SQL Tuesday post of <a href="http://bradsruminations.blogspot.com/2011/10/invitation-for-t-sql-tuesday-024-prox-n.html" target="_blank">Prox ‘n’ Funx</a>. This is a very interesting subject. By the way Brad Schulz is my favorite guy when it is about blogging. I respect him as well learn a lot from him. Everybody is writing something new his subject, I decided to start SQL Server 2012 analytic functions series.</p>
<p style="text-align:justify;">SQL Server 2012 introduces new analytical function CUME_DIST(). This function provides cumulative distribution value. It will be very difficult to explain this in words so I will attempt small example to explain you this function. Instead of creating new table, I will be using AdventureWorks sample database as most of the developer uses that for experiment.</p>
<p style="text-align:justify;">Let us fun following query.<br />
<code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks<br />
GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">SalesOrderID</span><span style="color:gray;">, </span><span style="color:black;">OrderQty</span><span style="color:gray;">,<br />
</span><span style="color:black;">CUME_DIST</span><span style="color:gray;">() </span><span style="color:blue;">OVER</span><span style="color:gray;">(</span><span style="color:blue;">ORDER BY </span><span style="color:black;">SalesOrderID</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">CDist<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Sales.SalesOrderDetail<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">SalesOrderID </span><span style="color:blue;">IN </span><span style="color:gray;">(</span><span style="color:black;">43670</span><span style="color:gray;">, </span><span style="color:black;">43669</span><span style="color:gray;">, </span><span style="color:black;">43667</span><span style="color:gray;">, </span><span style="color:black;">43663</span><span style="color:gray;">)<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">CDist </span><span style="color:blue;">DESC<br />
</span><span style="color:black;">GO<br />
</span></code></p>
<p style="text-align:justify;">Above query will give us following result.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/cumedist1.jpg" alt="" width="280" height="258" /></p>
<p style="text-align:justify;">Now let us understand what is the formula behind CUME_DIST and why the values in SalesOrderID = 43670 are 1.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/cumedist3.jpg" alt="" width="501" height="289" /></p>
<p style="text-align:justify;">Let us take more example and be clear about why the values in SalesOrderID = 43667 are 0.5.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/cumedist4.jpg" alt="" width="501" height="289" /></p>
<p style="text-align:justify;">Now let us enhence the same example and use PARTITION BY into the OVER clause and see the results. Run following query in SQL Server 2012.</p>
<p><code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks<br />
GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">SalesOrderID</span><span style="color:gray;">, </span><span style="color:black;">OrderQty</span><span style="color:gray;">,</span><span style="color:black;"> ProductID</span><span style="color:gray;">,<br />
</span><span style="color:black;">CUME_DIST</span><span style="color:gray;">() </span><span style="color:blue;">OVER</span><span style="color:gray;">(</span><span style="color:black;">PARTITION </span><span style="color:blue;">BY </span><span style="color:black;">SalesOrderID<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">ProductID </span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">CDist<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Sales.SalesOrderDetail s<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">SalesOrderID </span><span style="color:blue;">IN </span><span style="color:gray;">(</span><span style="color:black;">43670</span><span style="color:gray;">, </span><span style="color:black;">43669</span><span style="color:gray;">, </span><span style="color:black;">43667</span><span style="color:gray;">, </span><span style="color:black;">43663</span><span style="color:gray;">)<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">s.SalesOrderID </span><span style="color:blue;">DESC</span><span style="color:gray;">, </span><span style="color:black;">CDist </span><span style="color:blue;">DESC<br />
</span><span style="color:black;">GO<br />
</span></code></p>
<p style="text-align:justify;">Now let us see the result of this query. We are have changed the ORDER BY clause as well partitioning by SalesOrderID.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/cumedist5.jpg" alt="" width="363" height="248" /></p>
<p style="text-align:justify;">You can see that CUME_DIST() function provides us different results. Additionally now we see value 1 multiple times. As we are using partitioning for each group of SalesOrderID we get the CUME_DIST() value.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/cumedist6.jpg" alt="" width="499" height="248" /></p>
<p style="text-align:justify;">CUME_DIST() was long awaited Analytical function and I am glad to see it in SQL Server 2012.</p>
<p style="text-align:justify;">Reference: <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>)</strong></p>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/postaday/'>PostADay</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-function/'>SQL Function</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/tech/sql-scripts/'>SQL Scripts</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/15651/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/15651/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/15651/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/15651/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/15651/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/15651/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/15651/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/15651/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/15651/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/15651/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/15651/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/15651/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/15651/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/15651/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=15651&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2011/11/08/sql-server-introduction-to-cume_dist-analytic-functions-introduced-in-sql-server-2012/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/TSQL2sDay.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/cumedist1.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/cumedist3.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/cumedist4.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/cumedist5.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/cumedist6.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Video &#8211; Performance Improvement in Columnstore Index</title>
		<link>http://blog.sqlauthority.com/2011/11/07/sql-server-video-performance-improvement-in-columnstore-index/</link>
		<comments>http://blog.sqlauthority.com/2011/11/07/sql-server-video-performance-improvement-in-columnstore-index/#comments</comments>
		<pubDate>Mon, 07 Nov 2011 01:30:48 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[PostADay]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Index]]></category>
		<category><![CDATA[SQL Performance]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Video]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=15635</guid>
		<description><![CDATA[I earlier wrote an article about SQL SERVER – Fundamentals of Columnstore Index and it got very well accepted in community. However, one of the suggestion I keep on receiving for that article is that many of the reader wanted to see columnstore index in the action but they were not able to do that. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=15635&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">I earlier wrote an article about <a href="http://blog.sqlauthority.com/2011/10/29/sql-server-fundamentals-of-columnstore-index/" target="_blank"><strong>SQL SERVER – Fundamentals of Columnstore Index</strong></a> and it got very well accepted in community. However, one of the suggestion I keep on receiving for that article is that many of the reader wanted to see columnstore index in the action but they were not able to do that. Some of the readers did not install SQL Server 2012 or some did not have good machine to recreate the big table involved in the demo.</p>
<p style="text-align:justify;">For the same reason, I have created small video for that.</p>
<p style="text-align:justify;"><span style="text-align:center; display: block;"><a href="http://blog.sqlauthority.com/2011/11/07/sql-server-video-performance-improvement-in-columnstore-index/"><img src="http://img.youtube.com/vi/C-Ay6UxMfMo/2.jpg" alt="" /></a></span></p>
<p style="text-align:justify;">I have written two more article on columstore index. Please read them as followup to the video:</p>
<p style="text-align:justify;"><a href="http://blog.sqlauthority.com/2011/10/30/sql-server-how-to-ignore-columnstore-index-usage-in-query/" target="_blank"><strong>SQL SERVER – How to Ignore Columnstore Index Usage in Query</strong></a></p>
<p style="text-align:justify;"><a href="http://blog.sqlauthority.com/2011/11/06/sql-server-updating-data-in-a-columnstore-index/" target="_blank"><strong>SQL SERVER – Updating Data in A Columnstore Index</strong></a></p>
<p style="text-align:justify;">Reference: <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>)</strong></p>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/postaday/'>PostADay</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-index/'>SQL Index</a>, <a href='http://blog.sqlauthority.com/category/sql-performance/'>SQL Performance</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/tech/sql-scripts/'>SQL Scripts</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a>, <a href='http://blog.sqlauthority.com/category/video/'>Video</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/15635/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/15635/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/15635/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/15635/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/15635/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/15635/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/15635/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/15635/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/15635/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/15635/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/15635/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/15635/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/15635/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/15635/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=15635&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2011/11/07/sql-server-video-performance-improvement-in-columnstore-index/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>
	</item>
	</channel>
</rss>
