<?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: Efficient SQL in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Combine-multiple-SQL-queries-into-a-single-step-via-JOINs/m-p/498683#M132586</link>
    <description>&lt;P&gt;That makes sense. How would you join the following SQL along with the code which posted earlier/above. Since there are two SQLs, I'm not sure to combine the below query with the master SQL.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;create table incomm6 as
select x.policyinstanceid , x.customerID, y.value as client_segment from
(select b.identifier as policyinstanceid , b.customerID
from session.incomm a left join qis.rppolicyinstance b
on a.policyinstanceid=b.identifier) as x left join qis.rpcustomerfield y
on  x.customerid=y.customerid where y.externalidentifier='customerSubsegmentGE'
);&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 25 Sep 2018 09:28:37 GMT</pubDate>
    <dc:creator>Babloo</dc:creator>
    <dc:date>2018-09-25T09:28:37Z</dc:date>
    <item>
      <title>Combine multiple SQL queries into a single step via JOINs</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Combine-multiple-SQL-queries-into-a-single-step-via-JOINs/m-p/498659#M132573</link>
      <description />
      <pubDate>Wed, 26 Sep 2018 12:43:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Combine-multiple-SQL-queries-into-a-single-step-via-JOINs/m-p/498659#M132573</guid>
      <dc:creator>Babloo</dc:creator>
      <dc:date>2018-09-26T12:43:51Z</dc:date>
    </item>
    <item>
      <title>Re: Efficient SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Combine-multiple-SQL-queries-into-a-single-step-via-JOINs/m-p/498661#M132574</link>
      <description>&lt;P&gt;One create table can only create one dataset/table, so you can't put the 4 steps into one select.&lt;/P&gt;
&lt;P&gt;You might be able to do everything in one data step, if it is feasible to use formats created from the "right" datasets.&lt;/P&gt;</description>
      <pubDate>Tue, 25 Sep 2018 07:48:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Combine-multiple-SQL-queries-into-a-single-step-via-JOINs/m-p/498661#M132574</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-09-25T07:48:29Z</dc:date>
    </item>
    <item>
      <title>Re: Efficient SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Combine-multiple-SQL-queries-into-a-single-step-via-JOINs/m-p/498663#M132575</link>
      <description>&lt;P&gt;In what sense?&amp;nbsp; The four SQL statements create four datasets, one SQL statement cannot create more than one table.&amp;nbsp; A datastep might be able to.&amp;nbsp; Do you mean join all those tables into one big dataset?&amp;nbsp; That's possible, then its a matter of combining the selects so each variable is unique, and then adding the correct joins.&lt;/P&gt;</description>
      <pubDate>Tue, 25 Sep 2018 08:00:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Combine-multiple-SQL-queries-into-a-single-step-via-JOINs/m-p/498663#M132575</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-09-25T08:00:31Z</dc:date>
    </item>
    <item>
      <title>Re: Efficient SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Combine-multiple-SQL-queries-into-a-single-step-via-JOINs/m-p/498667#M132578</link>
      <description>Yes, I want to combine all the datasets into one big data set.&lt;BR /&gt;</description>
      <pubDate>Tue, 25 Sep 2018 08:40:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Combine-multiple-SQL-queries-into-a-single-step-via-JOINs/m-p/498667#M132578</guid>
      <dc:creator>Babloo</dc:creator>
      <dc:date>2018-09-25T08:40:31Z</dc:date>
    </item>
    <item>
      <title>Re: Efficient SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Combine-multiple-SQL-queries-into-a-single-step-via-JOINs/m-p/498675#M132583</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/8409"&gt;@Babloo&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Yes, I want to combine all the datasets into one big data set.&lt;BR /&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Create formats for the lookups, and use those in one data step.&lt;/P&gt;</description>
      <pubDate>Tue, 25 Sep 2018 09:06:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Combine-multiple-SQL-queries-into-a-single-step-via-JOINs/m-p/498675#M132583</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-09-25T09:06:04Z</dc:date>
    </item>
    <item>
      <title>Re: Efficient SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Combine-multiple-SQL-queries-into-a-single-step-via-JOINs/m-p/498676#M132584</link>
      <description>&lt;P&gt;Ok, then its a matter of looking at each select statement - make sure you have the variables you want in the output dataset, and they are unique.&amp;nbsp; Then look at each join and see how it relates to each other - this is why most would have some sort of plan of joins.&lt;/P&gt;
&lt;P&gt;From:&lt;/P&gt;
&lt;PRE&gt;from session.incomm a left join qis.rpcommunicationdescription b
on a.communicationdescriptionid=b.identifier
from session.incomm a  left join qis.rpcommunicationtype b
on a.communicationtypeid=b.identifier
from session.incomm a left join qis.rpinboxforcommunication b
on a.inboxforcommunicationID=b.identifier
where b.identifier in ('00000000001','10000000000','10000000001','10000000003')

from session.incomm a  left join qis.rplogonuser b
on a.createdbylogonuserid=b.identifier&lt;/PRE&gt;
&lt;P&gt;What can we tell from these, well session.incomm appears to be the base table for all the joins, so this should appear in the from first, then all other datasets will left join to that.&amp;nbsp; There are then four other datasets in the list, one with a restriction, so a shell of our from would look something like:&lt;/P&gt;
&lt;PRE&gt;from       session.incomm a
left join  qis.rpcommunicationdescription b
on         a.communicationdescriptionid=b.identifier
left join  qis.rpcommunicationtype c
on         a.communicationtypeid=c.identifier
left join  (select distinct identifier as inboxforcommunicationid, name as afdeling from qis.rpinboxforcommunication where b.identifier in ('00000000001','10000000000','10000000001','10000000003')) d
on         a.inboxforcommunicationID=d.identifier
left join  qis.rplogonuser e
on         a.createdbylogonuserid=e.identifier&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;Select should be a case of selecting the variables from each alias dependant on what you want in the output.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;That should get you started, as I do not have any test data in the form of a datastep or what you want out, I can only provide untested guidance.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 25 Sep 2018 09:07:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Combine-multiple-SQL-queries-into-a-single-step-via-JOINs/m-p/498676#M132584</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-09-25T09:07:50Z</dc:date>
    </item>
    <item>
      <title>Re: Efficient SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Combine-multiple-SQL-queries-into-a-single-step-via-JOINs/m-p/498683#M132586</link>
      <description>&lt;P&gt;That makes sense. How would you join the following SQL along with the code which posted earlier/above. Since there are two SQLs, I'm not sure to combine the below query with the master SQL.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;create table incomm6 as
select x.policyinstanceid , x.customerID, y.value as client_segment from
(select b.identifier as policyinstanceid , b.customerID
from session.incomm a left join qis.rppolicyinstance b
on a.policyinstanceid=b.identifier) as x left join qis.rpcustomerfield y
on  x.customerid=y.customerid where y.externalidentifier='customerSubsegmentGE'
);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 25 Sep 2018 09:28:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Combine-multiple-SQL-queries-into-a-single-step-via-JOINs/m-p/498683#M132586</guid>
      <dc:creator>Babloo</dc:creator>
      <dc:date>2018-09-25T09:28:37Z</dc:date>
    </item>
    <item>
      <title>Re: Efficient SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Combine-multiple-SQL-queries-into-a-single-step-via-JOINs/m-p/498686#M132589</link>
      <description>&lt;P&gt;How does that relate to&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;session.incomm&lt;/PRE&gt;
&lt;P&gt;If its not related, then how is it related to one of the other tables?&amp;nbsp; This is why you really need some sort of SQL plan to describe each table and how they link to other tables, there are lots of examples out there:&lt;BR /&gt;&lt;A href="https://www.google.ch/search?q=example+visual+sql+plan&amp;amp;rlz=1C1GGRV_enGB751GB751&amp;amp;tbm=isch&amp;amp;source=iu&amp;amp;ictx=1&amp;amp;fir=0fYJil5fFueRxM%253A%252CgsV8SSOXM-_v9M%252C_&amp;amp;usg=AI4_-kQv8lH_bYEN0BgNe371Cir8cT9J2g&amp;amp;sa=X&amp;amp;ved=2ahUKEwjQiY3i8dXdAhWPblAKHZeXDWsQ9QEwAXoECAMQBg#imgrc=ekzxoSo1Diuu8M" target="_blank"&gt;https://www.google.ch/search?q=example+visual+sql+plan&amp;amp;rlz=1C1GGRV_enGB751GB751&amp;amp;tbm=isch&amp;amp;source=iu&amp;amp;ictx=1&amp;amp;fir=0fYJil5fFueRxM%253A%252CgsV8SSOXM-_v9M%252C_&amp;amp;usg=AI4_-kQv8lH_bYEN0BgNe371Cir8cT9J2g&amp;amp;sa=X&amp;amp;ved=2ahUKEwjQiY3i8dXdAhWPblAKHZeXDWsQ9QEwAXoECAMQBg#imgrc=ekzxoSo1Diuu8M&lt;/A&gt;:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Just draw out each of your tables, highlighting key variables which link each table.&amp;nbsp; Once you have that the coding is very simple, just deciding on filters, and type of join.&lt;/P&gt;</description>
      <pubDate>Tue, 25 Sep 2018 09:57:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Combine-multiple-SQL-queries-into-a-single-step-via-JOINs/m-p/498686#M132589</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-09-25T09:57:16Z</dc:date>
    </item>
    <item>
      <title>Re: Efficient SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Combine-multiple-SQL-queries-into-a-single-step-via-JOINs/m-p/498698#M132594</link>
      <description>&lt;P&gt;session.incomm is created from seperate SQL and it is the table where it is common for all the tables per my previous post. Now could you please guide me how to incorporate the following SQL with your code?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;create table incomm5 as
select distinct createdbylogonuserid, left(trim(lastname))||' '||left(trim(firstname)) as creator, creator_duser
from(
select 	b.identifier as createdbylogonuserid,
		b.externalidentifier as creator_duser,	
		b.lastname,b.firstname
from session.incomm a  left join qis.rplogonuser b
on a.createdbylogonuserid=b.identifier);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 25 Sep 2018 11:03:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Combine-multiple-SQL-queries-into-a-single-step-via-JOINs/m-p/498698#M132594</guid>
      <dc:creator>Babloo</dc:creator>
      <dc:date>2018-09-25T11:03:51Z</dc:date>
    </item>
    <item>
      <title>Re: Efficient SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Combine-multiple-SQL-queries-into-a-single-step-via-JOINs/m-p/498700#M132596</link>
      <description>&lt;P&gt;This is something &lt;U&gt;&lt;STRONG&gt;you&lt;/STRONG&gt;&lt;/U&gt;, the person who has the data, knows the data, know the plan, to do.&amp;nbsp; Either that or hire a contractor to come in and do it for you.&amp;nbsp; I cannot code things with no information or data or knowledge about how things link together.&lt;/P&gt;</description>
      <pubDate>Tue, 25 Sep 2018 11:07:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Combine-multiple-SQL-queries-into-a-single-step-via-JOINs/m-p/498700#M132596</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-09-25T11:07:16Z</dc:date>
    </item>
    <item>
      <title>Re: Efficient SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Combine-multiple-SQL-queries-into-a-single-step-via-JOINs/m-p/498710#M132601</link>
      <description>&lt;P&gt;make INDEX variables is able to get you faster.&lt;/P&gt;</description>
      <pubDate>Tue, 25 Sep 2018 12:49:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Combine-multiple-SQL-queries-into-a-single-step-via-JOINs/m-p/498710#M132601</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2018-09-25T12:49:51Z</dc:date>
    </item>
    <item>
      <title>Combine SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Combine-multiple-SQL-queries-into-a-single-step-via-JOINs/m-p/498737#M132616</link>
      <description>&lt;P&gt;Could you please help me understand to combine the following SQL with the master SQL?&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;select x.policyinstanceid , x.customerID, y.value as client_segment from
(select f.identifier as policyinstanceid , f.customerID
from session.incomm a left join qis.rppolicyinstance f
on a.policyinstanceid=f.identifier) as x left join qis.rpcustomerfield y
on  x.customerid=y.customerid where y.externalidentifier='customerSubsegmentGE'
);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Master SQL,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&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 noprint;
	connect to &amp;amp;glb_db2.(datasrc=&amp;amp;glb_db2_src. Authdomain=DB2AuthODS connection=global);
	create table incomm2 as
		select * from connection to db2(

with temp1 as (select  y.ExternalIdentifier as commnr,
		y.communicationtypeid,
		y.communicationdescriptionid,
		y.inboxforcommunicationID,
		y.isincoming as incoming,
		y.policyid,
   		y.policyinstanceid,
   		y.tscommunicationcreated,
   		y.datetobecompleted,
   		y.createdbylogonuserid,
		z.externalidentifier as polisnr
from qis.rpcommunication y, qis.rppolicy z
where y.policyid=z.identifier) 

		select b.name as documenttype, b.identifier as communicationdescriptionid,c.identifier as communicationtypeid ,c.externalidentifier,c.Name as commtype,
			e.identifier as createdbylogonuserid,
			e.externalidentifier as creator_duser,	
			e.lastname,e.firstname 
		from temp1 as a
			left join  qis.rpcommunicationdescription as b
				on         a.communicationdescriptionid=b.identifier
			left join  qis.rpcommunicationtype as c
				on         a.communicationtypeid=c.identifier
		left join  
(select distinct identifier as inboxforcommunicationid,name as afdeling from qis.rpinboxforcommunication where identifier in ('00000000001','10000000000','10000000001','10000000003')) as d
				on         a.inboxforcommunicationID=d.inboxforcommunicationid
			left join  qis.rplogonuser as e
				on         a.createdbylogonuserid=e.identifier


);
	disconnect from db2;  
quit;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 25 Sep 2018 14:04:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Combine-multiple-SQL-queries-into-a-single-step-via-JOINs/m-p/498737#M132616</guid>
      <dc:creator>Babloo</dc:creator>
      <dc:date>2018-09-25T14:04:44Z</dc:date>
    </item>
  </channel>
</rss>

