<?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: proc sql joins in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-joins/m-p/731732#M227957</link>
    <description>&lt;P&gt;Would this work better?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; left join (select POLY_CMPNY,POLY_BRCH,POLY_POLCY,POLY_TYP,STATUS_DATE,NOTICE_TO,DATE_ORIGL_INCEPN,VALID_FLAG
            from DBADM.POTPOLH01 
            group by POLY_CMPNY,POLY_BRCH,POLY_POLCY,POLY_TYP 
            having STATUS_DATE = max(STATUS_DATE) ) as C&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 06 Apr 2021 22:03:21 GMT</pubDate>
    <dc:creator>ChrisNZ</dc:creator>
    <dc:date>2021-04-06T22:03:21Z</dc:date>
    <item>
      <title>proc sql joins</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-joins/m-p/731712#M227944</link>
      <description>&lt;P&gt;&amp;nbsp; Hi Team,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I need help in a query optimization.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The code is attached.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is there any other way can we replace the table c code to get the maximum status date, to avoid the duplicates.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;because of the sub query ,the query is taking long time to&amp;nbsp; run.&lt;/P&gt;
&lt;P&gt;Is there a way to optimise.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
proc sql;
connect to db2 as pscon(user=&amp;amp;db2_id
password=&amp;amp;db2_ps
database = &amp;amp;db2_db
readbuff=1);
%put &amp;amp;sqlxmsg;
create table &amp;amp;output as

select * from connection to pscon (

select a.cmpny||a.brch||a.polcy||a.typ as pol_key1, a.DATE_INCEPN,
a.DATE_EXPIRES, a.REAS_TERM, c.status_date, a.polcy_status, a.POLCY_PAY_PLAN,
c.NOTICE_TO, c.DATE_ORIGL_INCEPN,b.*,e.POLCY_EXTRA_CHRG1 as cancel_fee_prem,
e.POLCY_EXTRA_CHRG2 as cancel_fee_gst

from dbadm.potpoly as a

left join  (select * from ( select  row_number() over (partition by poly_cmpny||poly_brch||poly_polcy||poly_typ  order by status_date desc) as rownum,
poly_cmpny,poly_brch,poly_polcy,poly_typ,status_date,NOTICE_TO,DATE_ORIGL_INCEPN,valid_flag
from 
dbadm.potpolh01 ) cc where rownum=1) AS C
on c.poly_cmpny=a.cmpny and c.poly_brch=a.brch
and c.poly_polcy=a.polcy and c.poly_typ=a.typ and c.valid_flag='1'

left join dbadm.potstrni1 as b
on b.poly_cmpny=a.cmpny and b.poly_brch=a.brch
and b.poly_polcy=a.polcy and b.poly_typ=a.typ

left join dbadm.potbtrn01 as e
on e.poly_cmpny=a.cmpny and e.poly_brch=a.brch
and e.poly_polcy=a.polcy and e.poly_typ=a.typ and b.tran_no=e.tran_no
and b.BATC_BATCH_TYPe=e.BATC_BATCH_TYP
and e.POLCY_CHRG_TYP1='CF'


where a.CMPNY in ('1','6') and
a.DATE_INCEPN &amp;gt;=20120000 and ((substr(a.date_expires,1,4)=substr(a.DATE_INCEPN,1,4) and
substr(a.date_expires,5,2)-substr(a.DATE_INCEPN,5,2) in (1,0))
or (substr(a.date_expires,5,2)='01' and substr(a.DATE_INCEPN,5,2)='12' and
substr(a.date_expires,1,4)-substr(a.DATE_INCEPN,1,4)=1))

and a.typ not in ('0TP','1TP','2TP','3TP','4TP','5TP','6TP','7TP','8TP','9TP','STP','QTP')
and a.VALID_FLAG='1'
and b.DATE_EFFCT &amp;gt;=a.DATE_INCEPN and a.polcy_status='09'
);
quit;
%put $sqlxrc &amp;amp;sqlxmsg
disconnect from db2;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 06 Apr 2021 20:32:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-joins/m-p/731712#M227944</guid>
      <dc:creator>sfffdg</dc:creator>
      <dc:date>2021-04-06T20:32:50Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql joins</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-joins/m-p/731732#M227957</link>
      <description>&lt;P&gt;Would this work better?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; left join (select POLY_CMPNY,POLY_BRCH,POLY_POLCY,POLY_TYP,STATUS_DATE,NOTICE_TO,DATE_ORIGL_INCEPN,VALID_FLAG
            from DBADM.POTPOLH01 
            group by POLY_CMPNY,POLY_BRCH,POLY_POLCY,POLY_TYP 
            having STATUS_DATE = max(STATUS_DATE) ) as C&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 06 Apr 2021 22:03:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-joins/m-p/731732#M227957</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2021-04-06T22:03:21Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql joins</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-joins/m-p/731737#M227961</link>
      <description>Thanks Chris,&lt;BR /&gt;&lt;BR /&gt;But unfortunately,I get the below error.&lt;BR /&gt;&lt;BR /&gt;ERROR: CLI describe error: [IBM][CLI Driver][DB2] SQL0122N  A SELECT statement with no GROUP BY&lt;BR /&gt;clause contains a column name or expression and a column function in the SELECT clause, or a&lt;BR /&gt;column name or expression is contained in the SELECT clause but&lt;BR /&gt;       not in the GROUP BY clause.  SQLSTATE=42803&lt;BR /&gt;</description>
      <pubDate>Tue, 06 Apr 2021 22:38:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-joins/m-p/731737#M227961</guid>
      <dc:creator>sfffdg</dc:creator>
      <dc:date>2021-04-06T22:38:12Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql joins</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-joins/m-p/731759#M227972</link>
      <description>&lt;P&gt;This makes sense I suppose. Can you add all columns in the GROUP BY clause?&lt;/P&gt;</description>
      <pubDate>Wed, 07 Apr 2021 00:36:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-joins/m-p/731759#M227972</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2021-04-07T00:36:56Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql joins</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-joins/m-p/731798#M227986</link>
      <description>&lt;P&gt;I tried it Chris.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It gives the result based on the&amp;nbsp; whole set ,which is not my requirement.I need the group to be considered as mentioned before only 4 columns ,not all columns.&lt;/P&gt;</description>
      <pubDate>Wed, 07 Apr 2021 04:05:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-joins/m-p/731798#M227986</guid>
      <dc:creator>sfffdg</dc:creator>
      <dc:date>2021-04-07T04:05:31Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql joins</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-joins/m-p/731800#M227987</link>
      <description>&lt;P&gt;Well it looks like DB2 cannot accept a partial list of variables in the GROUP BY list. I don't have another idea.&lt;/P&gt;
&lt;P&gt;Was it faster even?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So duplicate dates for the same four ID variables is fine?&lt;/P&gt;</description>
      <pubDate>Wed, 07 Apr 2021 04:40:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-joins/m-p/731800#M227987</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2021-04-07T04:40:40Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql joins</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-joins/m-p/731806#M227990</link>
      <description>I don't want any duplicate dates for 4 variables,need the maximum date.&lt;BR /&gt;the row_num() function  where rownum=1 (sorted date)takes care here in the original query as below.&lt;BR /&gt;&lt;BR /&gt;select * from ( select  row_number() over (partition by poly_cmpny||poly_brch||poly_polcy||poly_typ  order by status_date desc) as rownum,&lt;BR /&gt;poly_cmpny,poly_brch,poly_polcy,poly_typ,status_date,NOTICE_TO,DATE_ORIGL_INCEPN,valid_flag&lt;BR /&gt;from &lt;BR /&gt;dbadm.potpolh01 ) cc where rownum=1</description>
      <pubDate>Wed, 07 Apr 2021 04:48:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-joins/m-p/731806#M227990</guid>
      <dc:creator>sfffdg</dc:creator>
      <dc:date>2021-04-07T04:48:00Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql joins</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-joins/m-p/731808#M227992</link>
      <description>&lt;P&gt;My window function skills are rusty. Can you show me an example of data that would differ between the 2 syntaxes?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, did you look at a speed difference?&lt;/P&gt;</description>
      <pubDate>Wed, 07 Apr 2021 04:52:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-joins/m-p/731808#M227992</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2021-04-07T04:52:57Z</dc:date>
    </item>
  </channel>
</rss>

