<?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/"
	>

<channel>
	<title>kwatog &#38; co. &#187; Random</title>
	<atom:link href="http://kwatog.com/tag/random/feed/" rel="self" type="application/rss+xml" />
	<link>http://kwatog.com</link>
	<description>tech notes and general nonsense</description>
	<lastBuildDate>Wed, 25 Aug 2010 05:46:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Select Random Records in Oracle</title>
		<link>http://kwatog.com/blog/oracle/select-random-records-in-oracle/</link>
		<comments>http://kwatog.com/blog/oracle/select-random-records-in-oracle/#comments</comments>
		<pubDate>Mon, 23 Feb 2009 06:32:09 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Oracle]]></category>
		<category><![CDATA[Oracle SQL]]></category>
		<category><![CDATA[Random]]></category>
		<category><![CDATA[Random Query]]></category>

		<guid isPermaLink="false">http://kwatog.com/?p=47</guid>
		<description><![CDATA[Recently, I needed to sort the result of a query in random and show a limited number of records. Honestly, I didn&#8217;t know how to do it and had to search various online forums to get the correct answer. So for my own sanity, I&#8217;ll be posting the solution here. Step 1 SELECT field_name1, field_name2 [...]]]></description>
			<content:encoded><![CDATA[<p>Recently, I needed to sort the result of a query in random and show a limited number of records. Honestly, I didn&#8217;t know how to do it and had to search various online forums to get the correct answer. So for my own sanity, I&#8217;ll be posting the solution here.</p>
<p><strong>Step 1</strong><br />
<code>SELECT field_name1, field_name2<br />
FROM   table_name<br />
WHERE  field_query1 = 'ARGUMENT VAL'<br />
ORDER  BY dbms_random.VALUE;</code></p>
<p>The key here is the function dmbs_random.VALUE that generates, you guess it, random number. Its value is then used for ordering. The second step is to limit the output by using ROWNUM as below.</p>
<p><strong>Step 2</strong><br />
<code>SELECT field_name1, field_name2<br />
FROM  (SELECT field_name1, field_name2<br />
FROM   table_name<br />
WHERE  field_query1 = 'ARGUMENT VAL'<br />
ORDER  BY dbms_random.VALUE)<br />
WHERE ROWNUM &lt; 11;</code></p>
<p>If you&#8217;ll notice, I used a subquery here. Why didn&#8217;t I just include the rownum inside the subquery? It is because I wanted the subquery to finish the random ordering first before limiting the output to 10.</p>
<p><strong>Real World Example</strong><br />
<code><br />
SELECT order_num, pr_postn_id<br />
FROM (SELECT order_num, pr_postn_id<br />
      FROM siebel.s_order<br />
      WHERE order_num LIKE 'V-%'<br />
      ORDER BY dbms_random.VALUE)<br />
WHERE ROWNUM < 11;<br />
</code></p>
<p>I'll also need this similar solution for MySQL and will be discussed in my next post.</p>
]]></content:encoded>
			<wfw:commentRss>http://kwatog.com/blog/oracle/select-random-records-in-oracle/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
