<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Write query to get custom results</title>
	<atom:link href="http://interview.msdotnetheaven.com/database-question-answer/write-query-to-get-custom-results.html/feed" rel="self" type="application/rss+xml" />
	<link>http://interview.msdotnetheaven.com/database-question-answer/write-query-to-get-custom-results.html</link>
	<description>Best place for interview preparation</description>
	<lastBuildDate>Mon, 08 Feb 2010 11:07:03 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
	<item>
		<title>By: gaurav.arora</title>
		<link>http://interview.msdotnetheaven.com/database-question-answer/write-query-to-get-custom-results.html/comment-page-1#comment-6</link>
		<dc:creator>gaurav.arora</dc:creator>
		<pubDate>Mon, 08 Feb 2010 11:07:03 +0000</pubDate>
		<guid isPermaLink="false">http://interview.msdotnetheaven.com/?p=659#comment-6</guid>
		<description>Great one!

I have requirement where I need to update an existing table, here is the query :

&lt;code&gt;
declare @tbl table(id int identity, university varchar(100),Utotals int, status varchar(100),statuscnt int)
insert into @tbl(university,Utotals,status,statuscnt)
select &#039;u1&#039;,100,&#039;pending&#039;,20
union all
select &#039;u1&#039;,100,&#039;Active&#039;,20
union all
select &#039;u1&#039;,100,&#039;Deferred&#039;,60
union all
select &#039;u2&#039;,60,&#039;pending&#039;,20
union all
select &#039;u2&#039;,60,&#039;Activ&#039;,20
union all
select &#039;u2&#039;,60,&#039;Deferred&#039;,20

select * from @tbl

update tt
set   university=null,
      utotals=null
from @tbl tt inner join @tbl t2 on tt.id  = t2.id + 1
and tt.university = t2.university

select * from @tbl
&lt;/code&gt;&lt;div class=&quot;comment-remix-meta&quot;&gt;&lt;a href=&quot;#&quot; class=&quot;replyto&quot; onclick=&quot;replyto(&#039;6&#039;,&#039;gaurav.arora&#039;); return false;&quot;&gt;Reply&lt;/a&gt;  - &lt;a href=&quot;#&quot; class=&quot;quote&quot; onclick=&quot;quote(&#039;6&#039;,&#039;gaurav.arora&#039;,&#039;Great one!\r\n\r\nI have requirement where I need to update an existing table, here is the query :\r\n\r\n&lt;code&gt;\r\ndeclare @tbl table(id int identity, university varchar(100),Utotals int, status varchar(100),statuscnt int)\r\ninsert into @tbl(university,Utotals,status,statuscnt)\r\nselect \&#039;u1\&#039;,100,\&#039;pending\&#039;,20\r\nunion all\r\nselect \&#039;u1\&#039;,100,\&#039;Active\&#039;,20\r\nunion all\r\nselect \&#039;u1\&#039;,100,\&#039;Deferred\&#039;,60\r\nunion all\r\nselect \&#039;u2\&#039;,60,\&#039;pending\&#039;,20\r\nunion all\r\nselect \&#039;u2\&#039;,60,\&#039;Activ\&#039;,20\r\nunion all\r\nselect \&#039;u2\&#039;,60,\&#039;Deferred\&#039;,20\r\n\r\nselect * from @tbl\r\n\r\nupdate tt\r\nset   university=null,\r\n      utotals=null\r\nfrom @tbl tt inner join @tbl t2 on tt.id  = t2.id + 1\r\nand tt.university = t2.university\r\n\r\nselect * from @tbl\r\n&lt;\/code&gt;&#039;); return false;&quot;&gt;Quote&lt;/a&gt;&lt;/div&gt;</description>
		<content:encoded><![CDATA[<p>Great one!</p>
<p>I have requirement where I need to update an existing table, here is the query :</p>
<p><code><br />
declare @tbl table(id int identity, university varchar(100),Utotals int, status varchar(100),statuscnt int)<br />
insert into @tbl(university,Utotals,status,statuscnt)<br />
select 'u1',100,'pending',20<br />
union all<br />
select 'u1',100,'Active',20<br />
union all<br />
select 'u1',100,'Deferred',60<br />
union all<br />
select 'u2',60,'pending',20<br />
union all<br />
select 'u2',60,'Activ',20<br />
union all<br />
select 'u2',60,'Deferred',20</p>
<p>select * from @tbl</p>
<p>update tt<br />
set   university=null,<br />
      utotals=null<br />
from @tbl tt inner join @tbl t2 on tt.id  = t2.id + 1<br />
and tt.university = t2.university</p>
<p>select * from @tbl<br />
</code>
<div class="comment-remix-meta"><a href="#" class="replyto" onclick="replyto('6','gaurav.arora'); return false;">Reply</a>  &#8211; <a href="#" class="quote" onclick="quote('6','gaurav.arora','Great one!\r\n\r\nI have requirement where I need to update an existing table, here is the query :\r\n\r\n&lt;code&gt;\r\ndeclare @tbl table(id int identity, university varchar(100),Utotals int, status varchar(100),statuscnt int)\r\ninsert into @tbl(university,Utotals,status,statuscnt)\r\nselect \'u1\',100,\'pending\',20\r\nunion all\r\nselect \'u1\',100,\'Active\',20\r\nunion all\r\nselect \'u1\',100,\'Deferred\',60\r\nunion all\r\nselect \'u2\',60,\'pending\',20\r\nunion all\r\nselect \'u2\',60,\'Activ\',20\r\nunion all\r\nselect \'u2\',60,\'Deferred\',20\r\n\r\nselect * from @tbl\r\n\r\nupdate tt\r\nset   university=null,\r\n      utotals=null\r\nfrom @tbl tt inner join @tbl t2 on tt.id  = t2.id + 1\r\nand tt.university = t2.university\r\n\r\nselect * from @tbl\r\n&lt;\/code&gt;'); return false;">Quote</a></div>
 <a href="http://interview.msdotnetheaven.com/wp-content/plugins/cubepoints/cp_about.php?u=6&width=500&height=200" title="CubePoints - About" class="thickbox">?</a>]]></content:encoded>
	</item>
</channel>
</rss>
