<?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 to include subset of 1 table in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/SQL-JOIN-to-include-subset-of-1-table/m-p/737713#M230000</link>
    <description>I am not looking for any values in particular, just the first three in lieu&lt;BR /&gt;of a random selection.  There is only 1 record per org ID in the org table&lt;BR /&gt;(same for the other ID in their respective tables).&lt;BR /&gt;&lt;BR /&gt;I have used a select query as a table in the joins in the past, so maybe I&lt;BR /&gt;could do some variation on that here?&lt;BR /&gt;</description>
    <pubDate>Wed, 28 Apr 2021 19:49:56 GMT</pubDate>
    <dc:creator>RandoDando</dc:creator>
    <dc:date>2021-04-28T19:49:56Z</dc:date>
    <item>
      <title>SQL JOIN to include subset of 1 table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-JOIN-to-include-subset-of-1-table/m-p/737699#M229996</link>
      <description>&lt;P&gt;I am trying to provide a subset of a table to someone using a PROC SQL query, but I am unsure of how to subset a table WITHIN a join.&amp;nbsp; For instance&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SQL;

SELECT REG.REGION, ORG.ORG, PLC.PLACE, SPT.SPOT

from REGION REG INNER JOIN

ORGANIZATION ORG ON REG.REG_ID = ORG.REG_ID

INNER JOIN

PLACE PLC ON PLC.ORG_ID = ORG.ORG_ID

INNER JOIN

SPOT SPT ON SPT.PLACE_ID = PLC.PLACE_ID&lt;BR /&gt;&lt;BR /&gt;where ORG.ORG_ID = 32158675;

quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If I want only the first 3 corresponding PLACE IDs in this example (using the order in the PLACE table), how do I modify this query to give me just that?&lt;/P&gt;
&lt;P&gt;I prefer not to specify them in a where statement because I want to run this for different circumstances and it would require me to look up the possible IDs each time.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;[Sorry for the boilerplate example, working from a different PC]&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 28 Apr 2021 18:58:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-JOIN-to-include-subset-of-1-table/m-p/737699#M229996</guid>
      <dc:creator>RandoDando</dc:creator>
      <dc:date>2021-04-28T18:58:34Z</dc:date>
    </item>
    <item>
      <title>Re: SQL JOIN to include subset of 1 table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-JOIN-to-include-subset-of-1-table/m-p/737710#M229998</link>
      <description>&lt;P&gt;"First 3" means that you may not want an SQL approach. SQL generally has no sense of "order".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Provide some examples of data and what you expect the result to be for those example data sets and you may get something.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Depending on how you define "first" you might be able to get them if 1) each ID only occurs one time in a data set and 2) the data set is sorted PRIOR to proc sql so that the first 3 (or other number) of desired values are in the first 3 records.&lt;/P&gt;
&lt;P&gt;You can use SAS data set options on data sets that contribute to a Proc SQL query so options like (obs=3) will select the first 3 records from a data set. Here is an example using a data set you should have available to test code with.&lt;/P&gt;
&lt;PRE&gt;proc sql;
   create table example as
   select *
   from sashelp.class (obs=3)
   ;
quit;&lt;/PRE&gt;
&lt;P&gt;Repeating, you would have to have the data set with the values you want sorted before Proc SQL to use this approach to get "first" anything.&lt;/P&gt;</description>
      <pubDate>Wed, 28 Apr 2021 19:44:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-JOIN-to-include-subset-of-1-table/m-p/737710#M229998</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-04-28T19:44:02Z</dc:date>
    </item>
    <item>
      <title>Re: SQL JOIN to include subset of 1 table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-JOIN-to-include-subset-of-1-table/m-p/737713#M230000</link>
      <description>I am not looking for any values in particular, just the first three in lieu&lt;BR /&gt;of a random selection.  There is only 1 record per org ID in the org table&lt;BR /&gt;(same for the other ID in their respective tables).&lt;BR /&gt;&lt;BR /&gt;I have used a select query as a table in the joins in the past, so maybe I&lt;BR /&gt;could do some variation on that here?&lt;BR /&gt;</description>
      <pubDate>Wed, 28 Apr 2021 19:49:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-JOIN-to-include-subset-of-1-table/m-p/737713#M230000</guid>
      <dc:creator>RandoDando</dc:creator>
      <dc:date>2021-04-28T19:49:56Z</dc:date>
    </item>
    <item>
      <title>Re: SQL JOIN to include subset of 1 table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-JOIN-to-include-subset-of-1-table/m-p/737722#M230002</link>
      <description>&lt;P&gt;You have not defined what actually constitutes "first 3" of anything.&lt;/P&gt;
&lt;P&gt;Or even which table you expect to get 3 from.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I cannot even tell if you are saying that selecting the first 3 from a specific contributing table is not sufficient. I have shown a way to do that. If you need something else you need to provide example data from all of your tables, with more than 3 so we can see where 3 are selected from, and the final result.&lt;/P&gt;</description>
      <pubDate>Wed, 28 Apr 2021 20:10:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-JOIN-to-include-subset-of-1-table/m-p/737722#M230002</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-04-28T20:10:54Z</dc:date>
    </item>
    <item>
      <title>Re: SQL JOIN to include subset of 1 table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-JOIN-to-include-subset-of-1-table/m-p/737950#M230108</link>
      <description>&lt;P&gt;If each PLACE_ID is only supposed to fulfill the join condition once, then you can use this:&lt;/P&gt;&lt;PRE&gt;PROC SQL &lt;FONT color="#FF6600"&gt;outobs=3&lt;/FONT&gt;;

SELECT REG.REGION, ORG.ORG, PLC.PLACE, SPT.SPOT

from REGION REG INNER JOIN

ORGANIZATION ORG ON REG.REG_ID = ORG.REG_ID

INNER JOIN

PLACE PLC ON PLC.ORG_ID = ORG.ORG_ID

INNER JOIN

SPOT SPT ON SPT.PLACE_ID = PLC.PLACE_ID where ORG.ORG_ID = 32158675;

quit;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Otherwise, use a temporary table:&lt;/P&gt;&lt;PRE&gt;&lt;FONT color="#FF0000"&gt;proc sql noprint outobs=3;
create table PLACE_TEMP as select place_id, place
from PLACE;
quit;&lt;/FONT&gt;

PROC SQL;
SELECT REG.REGION, ORG.ORG, PLC.PLACE, SPT.SPOT
from REGION REG INNER JOIN
ORGANIZATION ORG ON REG.REG_ID = ORG.REG_ID
INNER JOIN
&lt;FONT color="#FF0000"&gt;PLACE_TEMP&lt;/FONT&gt; PLC ON PLC.ORG_ID = ORG.ORG_ID
INNER JOIN
SPOT SPT ON SPT.PLACE_ID = PLC.PLACE_ID where ORG.ORG_ID = 32158675;
quit;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 29 Apr 2021 15:35:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-JOIN-to-include-subset-of-1-table/m-p/737950#M230108</guid>
      <dc:creator>SAS-Nutzer</dc:creator>
      <dc:date>2021-04-29T15:35:29Z</dc:date>
    </item>
    <item>
      <title>Re: SQL JOIN to include subset of 1 table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-JOIN-to-include-subset-of-1-table/m-p/738046#M230156</link>
      <description>&lt;P&gt;To avoid warnings, you can also use&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;FONT color="#000000"&gt;proc sql noprint;
  create table PLACE_TEMP as select place_id, place
  from PLACE(ob=3);
quit;&lt;/FONT&gt;&lt;/PRE&gt;
&lt;P&gt;instead of&lt;/P&gt;
&lt;PRE&gt;&lt;FONT color="#000000"&gt;proc sql noprint outobs=3;
  create table PLACE_TEMP as select place_id, place
  from PLACE;
quit;&lt;/FONT&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 29 Apr 2021 22:42:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-JOIN-to-include-subset-of-1-table/m-p/738046#M230156</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2021-04-29T22:42:55Z</dc:date>
    </item>
  </channel>
</rss>

