<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/atom10full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><feed xmlns="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:thr="http://purl.org/syndication/thread/1.0">
    <title>Oracle DB Development</title>
    
    
    <link rel="alternate" type="text/html" href="http://oradbdev.mathiasmagnusson.com/" />
    <id>tag:typepad.com,2003:weblog-1872631</id>
    <updated>2011-10-11T14:00:00+02:00</updated>
    
    <generator uri="http://www.typepad.com/">TypePad</generator>
    <atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/atom+xml" href="http://feeds.feedburner.com/OracleDbDevelopment" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="oracledbdevelopment" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://hubbub.api.typepad.com/" /><feedburner:emailServiceId xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">OracleDbDevelopment</feedburner:emailServiceId><feedburner:feedburnerHostname xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">http://feedburner.google.com</feedburner:feedburnerHostname><entry>
        <title>Suppressing repeating values in SQL</title>
        <link rel="alternate" type="text/html" href="http://oradbdev.mathiasmagnusson.com/2011/10/suppressing-repeating-values-in-sql.html" />
        <link rel="replies" type="text/html" href="http://oradbdev.mathiasmagnusson.com/2011/10/suppressing-repeating-values-in-sql.html" thr:count="1" thr:updated="2012-01-07T14:30:24+01:00" />
        <id>tag:typepad.com,2003:post-6a01156f98ed10970c014e8a507c94970d</id>
        <published>2011-10-11T14:00:00+02:00</published>
        <updated>2011-10-11T07:27:33+02:00</updated>
        <summary>Sometimes you my find a need to suppress repeating values in SQL. On case is when your reporting tool does not have such a feture or you just cannot find it fast enough. That happened to me with a report that was to be converted to APEX from Oracle Reports the other week. I could not find an option in APEX to suppress repeating values, and I did not want to make them all control breaks as that would chop up the report too much. Let's begin with a real simple SQL that shows the departments different employees work in....</summary>
        <author>
            <name>Mathias Magnusson</name>
        </author>
        
        
<content type="xhtml" xml:lang="sv-SE" xml:base="http://oradbdev.mathiasmagnusson.com/">
<div xmlns="http://www.w3.org/1999/xhtml"><p>Sometimes you my find a need to suppress repeating values in SQL. On case is when your reporting tool does not have such a feture or you just cannot find it fast enough. That happened to me with a report that was to be converted to APEX from Oracle Reports the other week.</p>
<p>I could not find an option in APEX to suppress repeating values, and I did not want to make them all control breaks as that would chop up the report too much.</p>
<p>Let's begin with a real simple SQL that shows the departments different employees work in.</p>
<blockquote>
<p><span style="font-family: 'courier new', courier;">select a.dname</span><br /><span style="font-family: 'courier new', courier;">      ,b.ename</span><br /><span style="font-family: 'courier new', courier;">  from      dept a</span><br /><span style="font-family: 'courier new', courier;"> inner join emp  b</span><br /><span style="font-family: 'courier new', courier;">    on a.deptno = b.deptno</span><br /><span style="font-family: 'courier new', courier;"> order by 1,2</span></p>
<p>This of course gives a list like this:</p>
</blockquote>
<table border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td>
<blockquote><span style="font-family: 'courier new', courier;">ACCOUNTING</span></blockquote>
</td>
<td>
<blockquote><span style="font-family: 'courier new', courier;">CLARK</span></blockquote>
</td>
</tr>
<tr>
<td>
<blockquote><span style="font-family: 'courier new', courier;">ACCOUNTING    </span></blockquote>
</td>
<td>
<blockquote><span style="font-family: 'courier new', courier;">KING</span></blockquote>
</td>
</tr>
<tr>
<td>
<blockquote><span style="font-family: 'courier new', courier;">ACCOUNTING</span></blockquote>
</td>
<td>
<blockquote><span style="font-family: 'courier new', courier;">MILLER</span></blockquote>
</td>
</tr>
<tr>
<td>
<blockquote><span style="font-family: 'courier new', courier;">RESEARCH</span></blockquote>
</td>
<td>
<blockquote><span style="font-family: 'courier new', courier;">ADAMS</span></blockquote>
</td>
</tr>
<tr>
<td>
<blockquote><span style="font-family: 'courier new', courier;">RESEARCH</span></blockquote>
</td>
<td>
<blockquote><span style="font-family: 'courier new', courier;">FORD</span></blockquote>
</td>
</tr>
<tr>
<td>
<blockquote><span style="font-family: 'courier new', courier;">RESEARCH</span></blockquote>
</td>
<td>
<blockquote><span style="font-family: 'courier new', courier;">JONES</span></blockquote>
</td>
</tr>
<tr>
<td>
<blockquote><span style="font-family: 'courier new', courier;">RESEARCH</span></blockquote>
</td>
<td>
<blockquote><span style="font-family: 'courier new', courier;">SCOTT</span></blockquote>
</td>
</tr>
<tr>
<td>
<blockquote><span style="font-family: 'courier new', courier;">RESEARCH</span></blockquote>
</td>
<td>
<blockquote><span style="font-family: 'courier new', courier;">SMITH</span></blockquote>
</td>
</tr>
<tr>
<td>
<blockquote><span style="font-family: 'courier new', courier;">SALES</span></blockquote>
</td>
<td>
<blockquote><span style="font-family: 'courier new', courier;">ALLEN</span></blockquote>
</td>
</tr>
<tr>
<td>
<blockquote><span style="font-family: 'courier new', courier;">SALES</span></blockquote>
</td>
<td>
<blockquote><span style="font-family: 'courier new', courier;">BLAKE</span></blockquote>
</td>
</tr>
<tr>
<td>
<blockquote><span style="font-family: 'courier new', courier;">SALES</span></blockquote>
</td>
<td>
<blockquote><span style="font-family: 'courier new', courier;">JAMES</span></blockquote>
</td>
</tr>
<tr>
<td>
<blockquote><span style="font-family: 'courier new', courier;">SALES</span></blockquote>
</td>
<td>
<blockquote><span style="font-family: 'courier new', courier;">MARTIN</span></blockquote>
</td>
</tr>
<tr>
<td>
<blockquote><span style="font-family: 'courier new', courier;">SALES</span></blockquote>
</td>
<td>
<blockquote><span style="font-family: 'courier new', courier;">TURNER</span></blockquote>
</td>
</tr>
<tr>
<td>
<blockquote><span style="font-family: 'courier new', courier;">SALES</span></blockquote>
</td>
<td>
<blockquote><span style="font-family: 'courier new', courier;">WARD</span></blockquote>
</td>
</tr>
</tbody>
</table>
<p>Suppose I want to remove the department name when it is the same as on the previous row. SQL does not give a way to just mention a keyword to have it done and APEX does not allow for it as far as I can tell.</p>
<p>The first step is to add a rownumber to the result table for the above SQL.</p>
<blockquote>
<p><span style="font-family: 'courier new', courier;">select c.*       </span><br /><span style="font-family: 'courier new', courier;">      ,rownum rn</span><br /><span style="font-family: 'courier new', courier;">  from (select a.dname</span><br /><span style="font-family: 'courier new', courier;">              ,b.ename</span><br /><span style="font-family: 'courier new', courier;">          from      dept a</span><br /><span style="font-family: 'courier new', courier;">         inner join emp  b</span><br /><span style="font-family: 'courier new', courier;">            on a.deptno = b.deptno</span><br /><span style="font-family: 'courier new', courier;">         order by 1,2) c</span></p>
</blockquote>
<p>Now each row has an number starting from 1 and ending with 14. We wrap this in a prefactor construct (i.e. wrap it in a with so the select can use the data).</p>
<blockquote>
<p><span style="font-family: 'courier new', courier;">with rpt as(select c.*       <br /></span><span style="font-family: 'courier new', courier;">                  ,rownum rn<br /></span><span style="font-family: 'courier new', courier;">              from (select a.dname<br /></span><span style="font-family: 'courier new', courier;">                          ,b.ename<br />                      from      dept a<br />                     inner join emp  b<br />                        on a.deptno = b.deptno<br />                     order by 1,2) c)<br />select case<br />         when a.dname = b.dname then ' '<br />         else                        a.dname<br />       end dname<br />      ,a.ename<br />  from rpt a left outer join rpt b    <br />    on a.rn = b.rn + 1<br /> order by a.rn;</span></p>
</blockquote>
<p>Here we have the select creating the list with the rownumber in a with that the select that follows can reference. We have now also added a selfjoin so we join each row with its predecessor in rownumber order.</p>
<p>This allows us to show data from the prefactored SQl using a to display data and b to check data on the previous row.</p>
<p>In this case we check the department and when it is the same as it was on the previous row we display a single space. if it is not the same, we display the name of the department. Thus, the case performs the supressing of repåeating department names.</p>
<p>The result of this SQL is the following report.</p>
<blockquote>
<table border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td>
<blockquote>ACCOUNTING</blockquote>
</td>
<td>
<blockquote>CLARK</blockquote>
</td>
</tr>
<tr>
<td />
<td>
<blockquote> KING</blockquote>
</td>
</tr>
<tr>
<td>
<blockquote> </blockquote>
</td>
<td>
<blockquote>MILLER</blockquote>
</td>
</tr>
<tr>
<td>
<blockquote>RESEARCH</blockquote>
</td>
<td>
<blockquote>ADAMS</blockquote>
</td>
</tr>
<tr>
<td>
<blockquote> </blockquote>
</td>
<td>
<blockquote>FORD</blockquote>
</td>
</tr>
<tr>
<td>
<blockquote> </blockquote>
</td>
<td>
<blockquote>JONES</blockquote>
</td>
</tr>
<tr>
<td>
<blockquote>  </blockquote>
</td>
<td>
<blockquote>SCOTT</blockquote>
</td>
</tr>
<tr>
<td>
<blockquote> </blockquote>
</td>
<td>
<blockquote>SMITH</blockquote>
</td>
</tr>
<tr>
<td>
<blockquote>SALES</blockquote>
</td>
<td>
<blockquote>ALLEN</blockquote>
</td>
</tr>
<tr>
<td>
<blockquote> </blockquote>
</td>
<td>
<blockquote>BLAKE</blockquote>
</td>
</tr>
<tr>
<td>
<blockquote> </blockquote>
</td>
<td>
<blockquote>JAMES</blockquote>
</td>
</tr>
<tr>
<td>
<blockquote> </blockquote>
</td>
<td>
<blockquote>MARTIN</blockquote>
</td>
</tr>
<tr>
<td>
<blockquote> </blockquote>
</td>
<td>
<blockquote>TURNER</blockquote>
</td>
</tr>
<tr>
<td>
<blockquote> </blockquote>
</td>
<td>
<blockquote>WARD</blockquote>
</td>
</tr>
</tbody>
</table>
</blockquote>
<p>That is it. Suppressing isn't too hard to do, but it requires a little bit of setup in the SQL.</p></div>
</content>



    </entry>
 
</feed><!-- ph=1 -->

