<?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: SAP HANA SQL Error: [304]: division by zero ... in Technology Q&amp;A</title>
    <link>https://community.sap.com/t5/technology-q-a/sap-hana-sql-error-304-division-by-zero/qaa-p/495699#M90554</link>
    <description>&lt;P&gt;Hi Lars, thank you for update.&lt;/P&gt;
  &lt;PRE&gt;&lt;CODE&gt;NULLIF(col1, 0)&lt;/CODE&gt;&lt;/PRE&gt;
  &lt;P&gt;is the short representation of&lt;/P&gt;
  &lt;PRE&gt;&lt;CODE&gt;CASE WHEN col1 = 0 THEN null ELSE col1 END&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 26 Oct 2017 06:45:14 GMT</pubDate>
    <dc:creator>eralper_yilmaz</dc:creator>
    <dc:date>2017-10-26T06:45:14Z</dc:date>
    <item>
      <title>SAP HANA SQL Error: [304]: division by zero ...</title>
      <link>https://community.sap.com/t5/technology-q-a/sap-hana-sql-error-304-division-by-zero/qaq-p/495693</link>
      <description>&lt;P&gt;In SAP HANA I had the following query (simplified): &lt;/P&gt; 
  &lt;PRE&gt;&lt;CODE&gt;select col1, col2 from TBL
where col1 &amp;gt; 0 and col2/col1 &amp;gt; 3&lt;/CODE&gt;&lt;/PRE&gt;
  &lt;P&gt;This results in the error: &lt;/P&gt;
  &lt;P&gt;[304]: division by zero undefined: search table error: [6859] AttributeEngine: divide by zero &lt;/P&gt;
  &lt;P&gt;Even when I try&lt;/P&gt; 
  &lt;PRE&gt;&lt;CODE&gt;select * from (
  select col1, col2 from TBL
  where col1 &amp;gt; 0 
) where col2/col1 &amp;gt; 3&lt;/CODE&gt;&lt;/PRE&gt;
  &lt;P&gt;results in the same error.&lt;/P&gt;
  &lt;P&gt;NOTE for simplification I changed the SQL. &lt;/P&gt;
  &lt;P&gt;TBL is acually a Graphical Calculation view and there are more attributes.&lt;/P&gt;
  &lt;P&gt;But executing the inner SQL works OK&lt;/P&gt;
  &lt;P&gt;When adding the outer where condition the error occurs.&lt;/P&gt;</description>
      <pubDate>Wed, 25 Oct 2017 09:53:01 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/sap-hana-sql-error-304-division-by-zero/qaq-p/495693</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2017-10-25T09:53:01Z</dc:date>
    </item>
    <item>
      <title>Re: SAP HANA SQL Error: [304]: division by zero ...</title>
      <link>https://community.sap.com/t5/technology-q-a/sap-hana-sql-error-304-division-by-zero/qaa-p/495694#M90549</link>
      <description>&lt;P&gt;There might the some condition when col1 is 0. You need to handle these condition specifically with the help of &lt;STRONG&gt;CASE&lt;/STRONG&gt; statement or there are also other way you can do these.&lt;/P&gt;</description>
      <pubDate>Wed, 25 Oct 2017 11:30:39 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/sap-hana-sql-error-304-division-by-zero/qaa-p/495694#M90549</guid>
      <dc:creator>pratik_doshi2</dc:creator>
      <dc:date>2017-10-25T11:30:39Z</dc:date>
    </item>
    <item>
      <title>Re: SAP HANA SQL Error: [304]: division by zero ...</title>
      <link>https://community.sap.com/t5/technology-q-a/sap-hana-sql-error-304-division-by-zero/qaa-p/495695#M90550</link>
      <description>&lt;P&gt;The divisors should be checked for 0 values &lt;/P&gt;
  &lt;P&gt;Following code will help you eliminate the rows from the resultset where col1 value is equal to 0&lt;/P&gt;
  &lt;PRE&gt;&lt;CODE&gt;select 
    col1, col2 
from TBL
where 
    col1 &amp;gt; 0 and 
    col2 / (CASE WHEN col1 = 0 then null else col1 end) &amp;gt; 3&lt;/CODE&gt;&lt;/PRE&gt;
  &lt;P&gt;What is important with your sample, using sub-select statement, we all assume that rows with col1 value equal to 0 are excluded by using where clause&lt;/P&gt;
  &lt;PRE&gt;&lt;CODE&gt;col1 &amp;gt; 0&lt;/CODE&gt;&lt;/PRE&gt;
  &lt;P&gt;So we are sure that this criteria will exclude all rows with col1=0 so we are safe to use it as divisor&lt;/P&gt;
  &lt;P&gt;I guess the SQL engine which parses this SQL query and creates the SQL execution path applies all WHERE clause criteria on a single step.&lt;/P&gt;
  &lt;P&gt;Unfortunately, this smart act causes this error&lt;/P&gt;
  &lt;P&gt;But surprisingly following code does not work also and causes division by zero error&lt;/P&gt;
  &lt;PRE&gt;&lt;CODE&gt;do 
begin

tbl_tmp2 = 
select 
    col1, col2 
from TBL
where 
    col1 &amp;lt;&amp;gt; 0 ;

select 
    col1, col2 
from :tbl_tmp2
where (col2 / col1) &amp;gt; 3;

end;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 25 Oct 2017 13:04:16 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/sap-hana-sql-error-304-division-by-zero/qaa-p/495695#M90550</guid>
      <dc:creator>eralper_yilmaz</dc:creator>
      <dc:date>2017-10-25T13:04:16Z</dc:date>
    </item>
    <item>
      <title>Re: SAP HANA SQL Error: [304]: division by zero ...</title>
      <link>https://community.sap.com/t5/technology-q-a/sap-hana-sql-error-304-division-by-zero/qaa-p/495696#M90551</link>
      <description>&lt;P&gt;The "check for 0" in your query silently assumes that the col1 &amp;gt;0 is executed before the rest of the query.&lt;/P&gt;
  &lt;P&gt;That's a false assumption for SQL - all predicates in your statement have to be true at the same time.&lt;/P&gt;
  &lt;P&gt;With HANA2 there is a comfortable way around this: the &lt;A href="https://help.sap.com/viewer/4fe29514fd584807ac9f2a04f6754767/2.0.02/en-US/bcbb7ba0f4f2495ca954132382226929.html"&gt; NDIV0 &lt;/A&gt;function.&lt;/P&gt;
  &lt;P&gt;Another workaround, in case you're on an older version that doesn't support NDIV0 is to use NULLIF in the division:&lt;/P&gt;
  &lt;PRE&gt;&lt;CODE&gt;select a,b&lt;BR /&gt;from TBL&lt;BR /&gt;where b &amp;gt;0 
and a/NULLIF(b, 0) &amp;gt;3&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 26 Oct 2017 01:30:13 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/sap-hana-sql-error-304-division-by-zero/qaa-p/495696#M90551</guid>
      <dc:creator>lbreddemann</dc:creator>
      <dc:date>2017-10-26T01:30:13Z</dc:date>
    </item>
    <item>
      <title>Re: SAP HANA SQL Error: [304]: division by zero ...</title>
      <link>https://community.sap.com/t5/technology-q-a/sap-hana-sql-error-304-division-by-zero/qaa-p/495697#M90552</link>
      <description>&lt;P&gt;Thanks for your detailed explanation. This is very helpful.&lt;/P&gt;</description>
      <pubDate>Thu, 26 Oct 2017 06:36:39 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/sap-hana-sql-error-304-division-by-zero/qaa-p/495697#M90552</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2017-10-26T06:36:39Z</dc:date>
    </item>
    <item>
      <title>Re: SAP HANA SQL Error: [304]: division by zero ...</title>
      <link>https://community.sap.com/t5/technology-q-a/sap-hana-sql-error-304-division-by-zero/qaa-p/495698#M90553</link>
      <description>&lt;P&gt;Thx for improving the answer of Eralper and even pointing out the possibility in HANA2&lt;/P&gt;</description>
      <pubDate>Thu, 26 Oct 2017 06:37:49 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/sap-hana-sql-error-304-division-by-zero/qaa-p/495698#M90553</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2017-10-26T06:37:49Z</dc:date>
    </item>
    <item>
      <title>Re: SAP HANA SQL Error: [304]: division by zero ...</title>
      <link>https://community.sap.com/t5/technology-q-a/sap-hana-sql-error-304-division-by-zero/qaa-p/495699#M90554</link>
      <description>&lt;P&gt;Hi Lars, thank you for update.&lt;/P&gt;
  &lt;PRE&gt;&lt;CODE&gt;NULLIF(col1, 0)&lt;/CODE&gt;&lt;/PRE&gt;
  &lt;P&gt;is the short representation of&lt;/P&gt;
  &lt;PRE&gt;&lt;CODE&gt;CASE WHEN col1 = 0 THEN null ELSE col1 END&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 26 Oct 2017 06:45:14 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/sap-hana-sql-error-304-division-by-zero/qaa-p/495699#M90554</guid>
      <dc:creator>eralper_yilmaz</dc:creator>
      <dc:date>2017-10-26T06:45:14Z</dc:date>
    </item>
    <item>
      <title>Re: SAP HANA SQL Error: [304]: division by zero ...</title>
      <link>https://community.sap.com/t5/technology-q-a/sap-hana-sql-error-304-division-by-zero/qaa-p/495700#M90555</link>
      <description>&lt;P&gt;You're right, semantically both expressions lead to the same outcome.&lt;/P&gt;
  &lt;P&gt;From an implementation point of view, CASE adds an additional type-coercion to the NULL return value, but that does not have a measurable impact on runtime or memory consumption. &lt;/P&gt;
  &lt;P&gt;More important, from my point of view, is that NULLIF makes it easier to see what the intention behind that bit of code is and it's shorter to write, as well.&lt;/P&gt;</description>
      <pubDate>Fri, 27 Oct 2017 00:31:48 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/sap-hana-sql-error-304-division-by-zero/qaa-p/495700#M90555</guid>
      <dc:creator>lbreddemann</dc:creator>
      <dc:date>2017-10-27T00:31:48Z</dc:date>
    </item>
  </channel>
</rss>

