<?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>topic Re: sql join with filter in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/sql-join-with-filter/m-p/645383#M192916</link>
    <description>&lt;P&gt;I suggest that you run this code and look at the result:&lt;/P&gt;
&lt;PRE&gt;proc sql;
create table work.sum as
select a.x as ax, b.x as bx, b.y, sum(a.x) as atot, sum(b.x) as btot, (sum(a.x) - sum(b.x)) as diff
from a, b
where b.y &amp;gt; 1;
quit;&lt;/PRE&gt;
&lt;P&gt;Then look up what "cartesian join" means.&lt;/P&gt;
&lt;P&gt;I think you expected to get a row by row match of some sort but that is not what happens with&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;from a, b which generates a cartesian join.&lt;/P&gt;
&lt;P&gt;Your log should show something like this:&lt;/P&gt;
&lt;PRE&gt;NOTE: The execution of this query involves performing one or more Cartesian product joins that
      can not be optimized.
&lt;/PRE&gt;
&lt;P&gt;as an indication of what is going on.&lt;/P&gt;</description>
    <pubDate>Tue, 05 May 2020 19:08:59 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2020-05-05T19:08:59Z</dc:date>
    <item>
      <title>sql join with filter</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sql-join-with-filter/m-p/645372#M192913</link>
      <description>&lt;P&gt;Why are the totals from this query join too high?&amp;nbsp; The results should be 5, 3 and 2.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data a;&lt;BR /&gt;input x 1.;&lt;BR /&gt;datalines;&lt;BR /&gt;1&lt;BR /&gt;2&lt;BR /&gt;1&lt;BR /&gt;1&lt;BR /&gt;;&lt;/P&gt;&lt;P&gt;data b;&lt;BR /&gt;input x 1. y 1.;&lt;BR /&gt;datalines;&lt;BR /&gt;11&lt;BR /&gt;12&lt;BR /&gt;13&lt;BR /&gt;21&lt;BR /&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;/*sum values of x for both data sets and calculate the difference*/&lt;/P&gt;&lt;P&gt;proc sql;&lt;BR /&gt;select sum(a.x) as atot, sum(b.x) as btot, (sum(a.x) - sum(b.x)) as diff&lt;BR /&gt;from a, b&lt;BR /&gt;where b.y &amp;gt; 1;&lt;BR /&gt;quit;&lt;/P&gt;</description>
      <pubDate>Tue, 05 May 2020 18:42:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sql-join-with-filter/m-p/645372#M192913</guid>
      <dc:creator>Batman</dc:creator>
      <dc:date>2020-05-05T18:42:01Z</dc:date>
    </item>
    <item>
      <title>Re: sql join with filter</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sql-join-with-filter/m-p/645383#M192916</link>
      <description>&lt;P&gt;I suggest that you run this code and look at the result:&lt;/P&gt;
&lt;PRE&gt;proc sql;
create table work.sum as
select a.x as ax, b.x as bx, b.y, sum(a.x) as atot, sum(b.x) as btot, (sum(a.x) - sum(b.x)) as diff
from a, b
where b.y &amp;gt; 1;
quit;&lt;/PRE&gt;
&lt;P&gt;Then look up what "cartesian join" means.&lt;/P&gt;
&lt;P&gt;I think you expected to get a row by row match of some sort but that is not what happens with&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;from a, b which generates a cartesian join.&lt;/P&gt;
&lt;P&gt;Your log should show something like this:&lt;/P&gt;
&lt;PRE&gt;NOTE: The execution of this query involves performing one or more Cartesian product joins that
      can not be optimized.
&lt;/PRE&gt;
&lt;P&gt;as an indication of what is going on.&lt;/P&gt;</description>
      <pubDate>Tue, 05 May 2020 19:08:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sql-join-with-filter/m-p/645383#M192916</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-05-05T19:08:59Z</dc:date>
    </item>
    <item>
      <title>Re: sql join with filter</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sql-join-with-filter/m-p/645408#M192924</link>
      <description>Sorry, I should have been clearer. I just want to compare the totals for x for both datasets with the where filter for the values in data set b.</description>
      <pubDate>Tue, 05 May 2020 20:31:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sql-join-with-filter/m-p/645408#M192924</guid>
      <dc:creator>Batman</dc:creator>
      <dc:date>2020-05-05T20:31:37Z</dc:date>
    </item>
    <item>
      <title>Re: sql join with filter</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sql-join-with-filter/m-p/645417#M192927</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/9248"&gt;@Batman&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Sorry, I should have been clearer. I just want to compare the totals for x for both datasets with the where filter for the values in data set b.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;The Cartesian join is the reason why. That is the response to your question.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You need to create the SUM before joining the data and need to avoid creating a Cartesian join for your results.&lt;/P&gt;
&lt;P&gt;A one-to-one record merge is quite often the place to use a data step merge. In SQL you would need to provide either 1) exactly one record in each subset or 2) a field in common to join on&lt;/P&gt;
&lt;P&gt;One way:&lt;/P&gt;
&lt;PRE&gt;proc sql;
   create table want as
   select asum,bsum, asum-bsum as dif
   from (select sum(x) as asum from a),
        (select sum(x) as bsum from b
         where y&amp;gt;1)
   ;
quit;

&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 05 May 2020 20:57:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sql-join-with-filter/m-p/645417#M192927</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-05-05T20:57:06Z</dc:date>
    </item>
  </channel>
</rss>

