<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>Question Re: Sql help on creating ranges in Technology Q&amp;A</title>
    <link>https://community.sap.com/t5/technology-q-a/sql-help-on-creating-ranges/qaa-p/13847874#M4878717</link>
    <description>&lt;P&gt;Just looked at the FAQ referenced by Volker. I bow to its elegance. Regarding my own suggestion, "Never mind!"&lt;/P&gt;</description>
    <pubDate>Thu, 08 Sep 2011 10:50:31 GMT</pubDate>
    <dc:creator>Former Member</dc:creator>
    <dc:date>2011-09-08T10:50:31Z</dc:date>
    <item>
      <title>Sql help on creating ranges</title>
      <link>https://community.sap.com/t5/technology-q-a/sql-help-on-creating-ranges/qaq-p/13847870</link>
      <description>&lt;P&gt;I have a table with 2 columns(name and value). The values can range from -5000 to +5000. How do I write a query to get the count of values in ranges like 
Range    Count
&lt;STRONG&gt;&lt;EM&gt;___ &lt;BR /&gt;
&lt;/EM&gt;&lt;/STRONG&gt;___
0-10       4
10-20      8
20-30      400&lt;/P&gt;</description>
      <pubDate>Wed, 07 Sep 2011 20:26:08 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/sql-help-on-creating-ranges/qaq-p/13847870</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2011-09-07T20:26:08Z</dc:date>
    </item>
    <item>
      <title>Re: Sql help on creating ranges</title>
      <link>https://community.sap.com/t5/technology-q-a/sql-help-on-creating-ranges/qaa-p/13847871#M4878714</link>
      <description>&lt;P&gt;You need to come up with a mathematical function that provides a mapping between the values in the table to a different value that you can use as a grouping attribute in a GROUP BY clause. The simpler the function, the better - if you have to resort to a SQL user-defined function, it can be done and will work but the performance penalty for invoking the function so many times may be more than you desire. The mechanics of the function will depend a great deal on the possible ranges of values.&lt;/P&gt;
&lt;P&gt;If, for example, you want to group the rows by ranges of 10, then you can do something like this:&lt;/P&gt;
&lt;DIV class="codehilite"&gt;&lt;PRE&gt;&lt;SPAN class="n"&gt;create&lt;/SPAN&gt; &lt;SPAN class="n"&gt;table&lt;/SPAN&gt; &lt;SPAN class="n"&gt;bar&lt;/SPAN&gt; &lt;SPAN class="p"&gt;(&lt;/SPAN&gt;&lt;SPAN class="n"&gt;x&lt;/SPAN&gt; &lt;SPAN class="n"&gt;integer&lt;/SPAN&gt;&lt;SPAN class="p"&gt;);&lt;/SPAN&gt;
&lt;SPAN class="n"&gt;insert&lt;/SPAN&gt; &lt;SPAN class="n"&gt;into&lt;/SPAN&gt; &lt;SPAN class="n"&gt;bar&lt;/SPAN&gt; &lt;SPAN class="p"&gt;(&lt;/SPAN&gt;&lt;SPAN class="n"&gt;x&lt;/SPAN&gt;&lt;SPAN class="p"&gt;)&lt;/SPAN&gt; &lt;SPAN class="nb"&gt;select&lt;/SPAN&gt; &lt;SPAN class="n"&gt;row_num&lt;/SPAN&gt; &lt;SPAN class="n"&gt;from&lt;/SPAN&gt; &lt;SPAN class="n"&gt;sa_rowgenerator&lt;/SPAN&gt;&lt;SPAN class="p"&gt;(&lt;/SPAN&gt; &lt;SPAN class="mi"&gt;1&lt;/SPAN&gt;&lt;SPAN class="p"&gt;,&lt;/SPAN&gt; &lt;SPAN class="mi"&gt;200&lt;/SPAN&gt;&lt;SPAN class="p"&gt;,&lt;/SPAN&gt; &lt;SPAN class="mi"&gt;1&lt;/SPAN&gt;&lt;SPAN class="p"&gt;);&lt;/SPAN&gt;
&lt;SPAN class="n"&gt;commit&lt;/SPAN&gt;&lt;SPAN class="p"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="nb"&gt;select&lt;/SPAN&gt; &lt;SPAN class="n"&gt;floor&lt;/SPAN&gt;&lt;SPAN class="p"&gt;(&lt;/SPAN&gt;&lt;SPAN class="n"&gt;x&lt;/SPAN&gt;&lt;SPAN class="o"&gt;/&lt;/SPAN&gt;&lt;SPAN class="mi"&gt;10&lt;/SPAN&gt;&lt;SPAN class="p"&gt;)&lt;/SPAN&gt; &lt;SPAN class="n"&gt;as&lt;/SPAN&gt; &lt;SPAN class="n"&gt;fn&lt;/SPAN&gt;&lt;SPAN class="p"&gt;,&lt;/SPAN&gt; &lt;SPAN class="n"&gt;count&lt;/SPAN&gt;&lt;SPAN class="p"&gt;(&lt;/SPAN&gt;&lt;SPAN class="o"&gt;*&lt;/SPAN&gt;&lt;SPAN class="p"&gt;)&lt;/SPAN&gt;
&lt;SPAN class="n"&gt;from&lt;/SPAN&gt; &lt;SPAN class="n"&gt;bar&lt;/SPAN&gt;
&lt;SPAN class="n"&gt;group&lt;/SPAN&gt; &lt;SPAN class="n"&gt;by&lt;/SPAN&gt; &lt;SPAN class="n"&gt;fn&lt;/SPAN&gt;
&lt;SPAN class="n"&gt;order&lt;/SPAN&gt; &lt;SPAN class="n"&gt;by&lt;/SPAN&gt; &lt;SPAN class="n"&gt;fn&lt;/SPAN&gt;
&lt;/PRE&gt;&lt;/DIV&gt;</description>
      <pubDate>Wed, 07 Sep 2011 21:47:47 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/sql-help-on-creating-ranges/qaa-p/13847871#M4878714</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2011-09-07T21:47:47Z</dc:date>
    </item>
    <item>
      <title>Re: Sql help on creating ranges</title>
      <link>https://community.sap.com/t5/technology-q-a/sql-help-on-creating-ranges/qaa-p/13847872#M4878715</link>
      <description>&lt;P&gt;&lt;A href="http://sqlanywhere-forum.sap.com/questions/5804"&gt;This FAQ&lt;/A&gt; contains a similar question - take a look:)&lt;/P&gt;</description>
      <pubDate>Thu, 08 Sep 2011 03:14:15 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/sql-help-on-creating-ranges/qaa-p/13847872#M4878715</guid>
      <dc:creator>VolkerBarth</dc:creator>
      <dc:date>2011-09-08T03:14:15Z</dc:date>
    </item>
    <item>
      <title>Re: Sql help on creating ranges</title>
      <link>https://community.sap.com/t5/technology-q-a/sql-help-on-creating-ranges/qaa-p/13847873#M4878716</link>
      <description>&lt;P&gt;How about just doing a query for each range and UNIONing the result sets?&lt;/P&gt;
&lt;P&gt;SELECT '0 - 10', COUNT(&lt;EM&gt;) 
FROM mytable
WHERE mytable.value BETWEEN 0 AND 10
UNION
SELECT '11 - 20', COUNT(&lt;/EM&gt;) 
FROM mytable
WHERE mytable.value BETWEEN 11 AND 20
UNION
(and so on...)&lt;/P&gt;
&lt;P&gt;Of course, this simple approach will only be realistic if you have a finite, and relatively small, number of ranges you want to report.&lt;/P&gt;
&lt;P&gt;Another thought would be to write a procedure that loops through ranges that increment each pass, essentially just automating my simple-minded UNION idea.&lt;/P&gt;</description>
      <pubDate>Thu, 08 Sep 2011 10:44:40 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/sql-help-on-creating-ranges/qaa-p/13847873#M4878716</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2011-09-08T10:44:40Z</dc:date>
    </item>
    <item>
      <title>Re: Sql help on creating ranges</title>
      <link>https://community.sap.com/t5/technology-q-a/sql-help-on-creating-ranges/qaa-p/13847874#M4878717</link>
      <description>&lt;P&gt;Just looked at the FAQ referenced by Volker. I bow to its elegance. Regarding my own suggestion, "Never mind!"&lt;/P&gt;</description>
      <pubDate>Thu, 08 Sep 2011 10:50:31 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/sql-help-on-creating-ranges/qaa-p/13847874#M4878717</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2011-09-08T10:50:31Z</dc:date>
    </item>
  </channel>
</rss>

