<?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 Where clause greater/less than multiple numeric columns in Technology Q&amp;A</title>
    <link>https://community.sap.com/t5/technology-q-a/sql-where-clause-greater-less-than-multiple-numeric-columns/qaa-p/13826966#M4857809</link>
    <description>&lt;P&gt;Thanks.  Great ideas from all!&lt;/P&gt;</description>
    <pubDate>Fri, 23 Apr 2021 12:27:06 GMT</pubDate>
    <dc:creator>dhkom</dc:creator>
    <dc:date>2021-04-23T12:27:06Z</dc:date>
    <item>
      <title>SQL Where clause greater/less than multiple numeric columns</title>
      <link>https://community.sap.com/t5/technology-q-a/sql-where-clause-greater-less-than-multiple-numeric-columns/qaq-p/13826956</link>
      <description>&lt;P&gt;I never thought I'd be asking an SQL question here.  However today is the day!  &lt;/P&gt;
&lt;P&gt;Say I have a table with numeric columns A, B populated as follows.  We can think of column A being a foreign key to a parent table, and column A and B together being the primary key of its child table:&lt;/P&gt;
&lt;PRE&gt;A  B
1  1
1  2
2  1
2  2
3  1
3  2
&lt;/PRE&gt;

&lt;P&gt;I would like a SQL Select to return all records after, for example, (2, 1).  I.e. (2, 2), (3, 1), and (3, 2). &lt;/P&gt;
&lt;P&gt;Obviously the following won't work (it won't return (3, 1)):&lt;/P&gt;
&lt;P&gt;&lt;CODE&gt;select A, B from mytable when A &amp;gt; 2 and B &amp;gt; 1&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;I wish there were a way to write "A &amp;gt; 2 and B &amp;gt; 1" in a way that indicates B is "is a breakdown" of A, for lack of a better way to express it.  &lt;/P&gt;
&lt;P&gt;Of course I could create a derived column with the two numbers concatenated together, padded with enough zeros to accomodate maximum number size.  Something like:&lt;/P&gt;
&lt;PRE&gt;A  B  AandB
1  1  0101
1  2  0102
2  1  0201
2  2  0202
3  1  0301
3  2  0302
&lt;/PRE&gt;

&lt;P&gt;... Then I could write the SQL I want as:&lt;/P&gt;
&lt;P&gt;&lt;CODE&gt;select A, B from mytable when AandB &amp;gt; '0201'&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;However it would be wonderful if I could write a Where clause operating on the original numbers.  &lt;/P&gt;
&lt;P&gt;Maybe it would have been best to avoid multiple numeric columns making up a child table's key, although I'm not sure avoiding such would always eliminate the need for what I'm asking about.&lt;/P&gt;
&lt;P&gt;This has been a tough one to Google search for solutions to.  Thoughts and ideas are welcome!&lt;/P&gt;</description>
      <pubDate>Thu, 22 Apr 2021 11:56:12 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/sql-where-clause-greater-less-than-multiple-numeric-columns/qaq-p/13826956</guid>
      <dc:creator>dhkom</dc:creator>
      <dc:date>2021-04-22T11:56:12Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Where clause greater/less than multiple numeric columns</title>
      <link>https://community.sap.com/t5/technology-q-a/sql-where-clause-greater-less-than-multiple-numeric-columns/qaa-p/13826957#M4857800</link>
      <description>&lt;P&gt;That would be a tuple comparison. The following would work:&lt;/P&gt;
&lt;P&gt;WHERE (A = 2 AND B &amp;gt; 1) OR A &amp;gt; 2&lt;/P&gt;</description>
      <pubDate>Thu, 22 Apr 2021 12:27:11 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/sql-where-clause-greater-less-than-multiple-numeric-columns/qaa-p/13826957#M4857800</guid>
      <dc:creator>johnsmirnios</dc:creator>
      <dc:date>2021-04-22T12:27:11Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Where clause greater/less than multiple numeric columns</title>
      <link>https://community.sap.com/t5/technology-q-a/sql-where-clause-greater-less-than-multiple-numeric-columns/qaa-p/13826958#M4857801</link>
      <description>&lt;P&gt;I'd guess the folowing should do:&lt;/P&gt;
&lt;P&gt;select A, B from mytable where A = 2 and B &amp;gt; 1 or A &amp;gt; 2;&lt;/P&gt;</description>
      <pubDate>Thu, 22 Apr 2021 12:29:10 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/sql-where-clause-greater-less-than-multiple-numeric-columns/qaa-p/13826958#M4857801</guid>
      <dc:creator>VolkerBarth</dc:creator>
      <dc:date>2021-04-22T12:29:10Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Where clause greater/less than multiple numeric columns</title>
      <link>https://community.sap.com/t5/technology-q-a/sql-where-clause-greater-less-than-multiple-numeric-columns/qaa-p/13826960#M4857803</link>
      <description>&lt;P&gt;I'm a little fuzzy on arrays but the following might also work and is easily extended to more columns:&lt;/P&gt;
&lt;P&gt;WHERE ARRAY( A, B ) &amp;gt; ARRAY( 2, 1 )&lt;/P&gt;</description>
      <pubDate>Thu, 22 Apr 2021 12:32:51 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/sql-where-clause-greater-less-than-multiple-numeric-columns/qaa-p/13826960#M4857803</guid>
      <dc:creator>johnsmirnios</dc:creator>
      <dc:date>2021-04-22T12:32:51Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Where clause greater/less than multiple numeric columns</title>
      <link>https://community.sap.com/t5/technology-q-a/sql-where-clause-greater-less-than-multiple-numeric-columns/qaa-p/13826961#M4857804</link>
      <description>&lt;P&gt;Thanks, John.  I probably should have figured that out too - sometimes we're just blocked.  I've not used this ARRAY syntax and will definitely investigate.&lt;/P&gt;</description>
      <pubDate>Thu, 22 Apr 2021 12:38:41 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/sql-where-clause-greater-less-than-multiple-numeric-columns/qaa-p/13826961#M4857804</guid>
      <dc:creator>dhkom</dc:creator>
      <dc:date>2021-04-22T12:38:41Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Where clause greater/less than multiple numeric columns</title>
      <link>https://community.sap.com/t5/technology-q-a/sql-where-clause-greater-less-than-multiple-numeric-columns/qaa-p/13826962#M4857805</link>
      <description>&lt;P&gt;Thanks, Volker.  I probably should have figured that out too - sometimes we're just blocked.  Thinking more about this, multi-level numeric primary keys should probably be avoided in favor of surrogate keys.  This may have eliminated my need here.  However the database in question is well established as it is.&lt;/P&gt;</description>
      <pubDate>Thu, 22 Apr 2021 12:40:57 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/sql-where-clause-greater-less-than-multiple-numeric-columns/qaa-p/13826962#M4857805</guid>
      <dc:creator>dhkom</dc:creator>
      <dc:date>2021-04-22T12:40:57Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Where clause greater/less than multiple numeric columns</title>
      <link>https://community.sap.com/t5/technology-q-a/sql-where-clause-greater-less-than-multiple-numeric-columns/qaa-p/13826963#M4857806</link>
      <description>&lt;P&gt;The somewhat shorter answer for those aware of the higher precedende of AND vs. OR...:)&lt;/P&gt;</description>
      <pubDate>Thu, 22 Apr 2021 12:41:49 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/sql-where-clause-greater-less-than-multiple-numeric-columns/qaa-p/13826963#M4857806</guid>
      <dc:creator>VolkerBarth</dc:creator>
      <dc:date>2021-04-22T12:41:49Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Where clause greater/less than multiple numeric columns</title>
      <link>https://community.sap.com/t5/technology-q-a/sql-where-clause-greater-less-than-multiple-numeric-columns/qaa-p/13826964#M4857807</link>
      <description>&lt;P&gt;It's fine to rely on precedence but I have had too many first hand experiences in multiple languages where the author clearly assumed the wrong precedence &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; There's no mistaking the order of operations when the parentheses are given.&lt;/P&gt;
&lt;P&gt;My favourite one from C/C++ is&lt;/P&gt;
&lt;P&gt;bool flag_is_set = flags&amp;amp;SOME_FLAG_MASK != 0;&lt;/P&gt;
&lt;P&gt;It does not do what the author intended. It does the following:&lt;/P&gt;
&lt;P&gt;bool flag_is_set = flags &amp;amp; (SOME_FLAG_MASK != 0);&lt;/P&gt;
&lt;P&gt;which will always return zero if SOME_FLAG_MASK is zero or the low bit of flags otherwise.&lt;/P&gt;
&lt;P&gt;I've also seen&lt;/P&gt;
&lt;P&gt;if( a      &amp;amp;&amp;amp;&lt;/P&gt;
&lt;DIV&gt;&lt;PRE&gt;&lt;SPAN&gt;&lt;/SPAN&gt;b || c &amp;amp;&amp;amp;

d )
&lt;/PRE&gt;&lt;/DIV&gt;


&lt;P&gt;{
...
}&lt;/P&gt;
&lt;P&gt;and they clearly meant (b || c)&lt;/P&gt;</description>
      <pubDate>Thu, 22 Apr 2021 12:57:49 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/sql-where-clause-greater-less-than-multiple-numeric-columns/qaa-p/13826964#M4857807</guid>
      <dc:creator>johnsmirnios</dc:creator>
      <dc:date>2021-04-22T12:57:49Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Where clause greater/less than multiple numeric columns</title>
      <link>https://community.sap.com/t5/technology-q-a/sql-where-clause-greater-less-than-multiple-numeric-columns/qaa-p/13826965#M4857808</link>
      <description>&lt;P&gt;Well, I do get the point. On the other hand, I certainly dislike it when each and every condition is put into parantheses, as some tools do... It just makes it harder to read, and usually there is common knowledge of precedence rules. (Bitwise operators are less common, for sure...)&lt;/P&gt;</description>
      <pubDate>Thu, 22 Apr 2021 15:03:49 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/sql-where-clause-greater-less-than-multiple-numeric-columns/qaa-p/13826965#M4857808</guid>
      <dc:creator>VolkerBarth</dc:creator>
      <dc:date>2021-04-22T15:03:49Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Where clause greater/less than multiple numeric columns</title>
      <link>https://community.sap.com/t5/technology-q-a/sql-where-clause-greater-less-than-multiple-numeric-columns/qaa-p/13826959#M4857802</link>
      <description>&lt;P&gt;If your know the number range of your columns, then perhaps just multiply the first one by for example 10000 and then filter n that one.&lt;/P&gt;
&lt;P&gt;Something like:&lt;/P&gt;
&lt;DIV&gt;&lt;PRE&gt;&lt;SPAN&gt;&lt;/SPAN&gt;select A, B from mytable where A*10000+B  &amp;gt; 20001
&lt;/PRE&gt;&lt;/DIV&gt;


&lt;P&gt;Of course only works if you don't run into a numeric overflow, and the array solution already mentioned is elegant.
I'm just wondering how both solutions compare on the performance level for large tables&lt;/P&gt;</description>
      <pubDate>Fri, 23 Apr 2021 05:10:47 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/sql-where-clause-greater-less-than-multiple-numeric-columns/qaa-p/13826959#M4857802</guid>
      <dc:creator>André_Schild</dc:creator>
      <dc:date>2021-04-23T05:10:47Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Where clause greater/less than multiple numeric columns</title>
      <link>https://community.sap.com/t5/technology-q-a/sql-where-clause-greater-less-than-multiple-numeric-columns/qaa-p/13826966#M4857809</link>
      <description>&lt;P&gt;Thanks.  Great ideas from all!&lt;/P&gt;</description>
      <pubDate>Fri, 23 Apr 2021 12:27:06 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/sql-where-clause-greater-less-than-multiple-numeric-columns/qaa-p/13826966#M4857809</guid>
      <dc:creator>dhkom</dc:creator>
      <dc:date>2021-04-23T12:27:06Z</dc:date>
    </item>
  </channel>
</rss>

