<?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 Tip &#38; Insight</title>
	<atom:link href="http://sqltip.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://sqltip.wordpress.com</link>
	<description>make sql life easy and joyable~</description>
	<lastBuildDate>Thu, 22 Oct 2009 14:57:05 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='sqltip.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>SQL Tip &#38; Insight</title>
		<link>http://sqltip.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://sqltip.wordpress.com/osd.xml" title="SQL Tip &#38; Insight" />
	<atom:link rel='hub' href='http://sqltip.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Drop failed for User</title>
		<link>http://sqltip.wordpress.com/2009/10/22/drop-failed-for-user/</link>
		<comments>http://sqltip.wordpress.com/2009/10/22/drop-failed-for-user/#comments</comments>
		<pubDate>Thu, 22 Oct 2009 14:52:17 +0000</pubDate>
		<dc:creator>junchaozhang</dc:creator>
				<category><![CDATA[Error Handle]]></category>

		<guid isPermaLink="false">http://sqltip.wordpress.com/?p=10</guid>
		<description><![CDATA[You may encounter an error while trying to drop a user in the database. &#8220;The database principal owns a database role and cannot be dropped. (Microsoft SQL Server, 15421)&#8221; So you may want to find which role the user owns and how to fix this error. Run the script below to find the roles user [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqltip.wordpress.com&amp;blog=10047682&amp;post=10&amp;subd=sqltip&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>You may encounter an error while trying to drop a user in the database.</p>
<p><span style="color:#ff0000;"><em>&#8220;The database principal owns a database role and cannot be dropped. (Microsoft SQL Server, 15421)&#8221;</em></span></p>
<p><span style="color:#000000;">So you may want to find which role the user owns and how to fix this error.</span></p>
<p><span style="color:#000000;">Run the script below to find the roles user owns:</span></p>
<p><span style="color:#666699;">SELECT dp.[name] AS [Owner], dp2.[name] AS [Role]<br />
FROM sys.database_principals dp JOIN sys.database_principals dp2 ON dp.principal_id = dp2.owning_principal_id<br />
WHERE dp.[name] = &#8216;UserToDelete&#8217;</span></p>
<p>In the above query replace &lt;&#8217;UserToDelete&#8217;&gt; <span style="font-size:x-small;"><span style="font-family:Arial;">with the user that you wish to delete. </span></span></p>
<p><span style="font-size:x-small;"><span style="font-family:Arial;">For each role in the query result, you can modify its owner in the Property window to [dbo].</span></span><span style="font-size:x-small;"><span style="font-family:Arial;">Or you can run the script below:</span></span></p>
<p><span style="font-size:x-small;"><span style="font-family:Arial;"><span style="color:#666699;">ALTER AUTHORIZATION ON ROLE::[ROLE_InResult] TO [dbo]</span></span></span></p>
<p><span style="font-size:x-small;"><span style="font-family:Arial;">Now you&#8217;re ready to delete the user.</span></span><span style="color:#000000;"> </span></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqltip.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqltip.wordpress.com/10/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqltip.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqltip.wordpress.com/10/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqltip.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqltip.wordpress.com/10/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqltip.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqltip.wordpress.com/10/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqltip.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqltip.wordpress.com/10/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqltip.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqltip.wordpress.com/10/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqltip.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqltip.wordpress.com/10/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqltip.wordpress.com&amp;blog=10047682&amp;post=10&amp;subd=sqltip&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://sqltip.wordpress.com/2009/10/22/drop-failed-for-user/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/ce5d1d643a035cc4d7ee96db5cc97cc9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">junchaozhang</media:title>
		</media:content>
	</item>
		<item>
		<title>Using non-equi Self-Join to remove redundants</title>
		<link>http://sqltip.wordpress.com/2009/10/22/using-non-equi-self-join-to-remove-redundants/</link>
		<comments>http://sqltip.wordpress.com/2009/10/22/using-non-equi-self-join-to-remove-redundants/#comments</comments>
		<pubDate>Thu, 22 Oct 2009 04:04:37 +0000</pubDate>
		<dc:creator>junchaozhang</dc:creator>
				<category><![CDATA[Data Clean]]></category>
		<category><![CDATA[Self-Join]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://sqltip.wordpress.com/?p=3</guid>
		<description><![CDATA[Somehow, you may mess up your data in the table like inputing duplicate values. For example, developers run the insert script multiple times. Or like this table Product   Some one inserted Product 1, 2, 3 later again and made the first 4 rows reduntant. You will find these by quering: /*&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;  SELECT p.ProductName, COUNT(*) [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqltip.wordpress.com&amp;blog=10047682&amp;post=3&amp;subd=sqltip&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Somehow, you may mess up your data in the table like inputing duplicate values. For example, developers run the insert script multiple times.</p>
<p>Or like this table Product</p>
<p><img class="alignnone size-full wp-image-5" title="Product Table" src="http://sqltip.files.wordpress.com/2009/10/10-21-2009-11-19-18-pm.png?w=449&#038;h=173" alt="Product Table" width="449" height="173" /> </p>
<p>Some one inserted Product 1, 2, 3 later again and made the first 4 rows reduntant. You will find these by quering:</p>
<p>/*&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
 SELECT p.ProductName, COUNT(*) AS Num<br />
 FROM Product p<br />
 GROUP BY p.ProductName<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;*/<br />
ProductName                                        Num<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211; &#8212;&#8212;&#8212;&#8211;<br />
Product1                                           3<br />
Product2                                           2<br />
Product3                                           2<br />
Product4                                           1</p>
<p>So when cleaning comes to play, we&#8217;d like to keep the last 4  rows only. How could you find and keep the lastest item only in each group? Or how could you delete the redundant ones?</p>
<p>Tip: use non-equi self-join</p>
<p>/*&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
SELECT DISTINCT p.ProductId, p.ProductName<br />
  FROM Product p JOIN Product p2 ON p.ProductId &lt; p2.ProductId AND p.ProductName = p2.ProductName<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;*/<br />
ProductId   ProductName<br />
&#8212;&#8212;&#8212;&#8211; &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br />
1           Product1<br />
2           Product2<br />
3           Product3<br />
4           Product1</p>
<p>So only task left is to remove them:</p>
<p>/*&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
DELETE<br />
FROM   Product<br />
WHERE  ProductId IN (SELECT DISTINCT p.ProductId<br />
                     FROM   Product p<br />
                            JOIN Product p2<br />
                                 ON  p.ProductId &gt; p2.ProductId<br />
                                 AND p.ProductName = p2.ProductName)<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;*/</p>
<p>(4 row(s) affected)</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqltip.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqltip.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqltip.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqltip.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqltip.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqltip.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqltip.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqltip.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqltip.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqltip.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqltip.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqltip.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqltip.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqltip.wordpress.com/3/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqltip.wordpress.com&amp;blog=10047682&amp;post=3&amp;subd=sqltip&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://sqltip.wordpress.com/2009/10/22/using-non-equi-self-join-to-remove-redundants/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/ce5d1d643a035cc4d7ee96db5cc97cc9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">junchaozhang</media:title>
		</media:content>

		<media:content url="http://sqltip.files.wordpress.com/2009/10/10-21-2009-11-19-18-pm.png" medium="image">
			<media:title type="html">Product Table</media:title>
		</media:content>
	</item>
	</channel>
</rss>
