<?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: Self-Join Proc SQL in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Self-Join-Proc-SQL/m-p/108517#M30257</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;When you use pass through you need to use SQL that is compliant with your system, so you need to provide the system information, in this case SYBASE. &lt;BR /&gt;Unfortunately I'm not familiar with SYBASE SQL, so I don't know the solution off the top of my head.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;My next suggestion would be to use a subquery and join on the table itself.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 09 Oct 2013 19:16:53 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2013-10-09T19:16:53Z</dc:date>
    <item>
      <title>Self-Join Proc SQL</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Self-Join-Proc-SQL/m-p/108509#M30249</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I'm looking for some assistance with a self-join using proc sql.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have a table with the following variables: client_policy_number, transaction_date, plan_type, plan_description. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I want to select the minimum plan_type for each transaction_date and group by the policy_number and transaction_date.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The data looks like this:&lt;/P&gt;&lt;P&gt;policy_number&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; tx_date&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; plan_typ&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; plan_desc&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 04JUL2011&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 15&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Agro&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 04JUL2011&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 12&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; GDP&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 04JUL2011&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 7&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; VFFC&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; line-height: 1.5em;"&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; line-height: 1.5em;"&gt;I'd like to return something like this for each policy number: &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; line-height: 1.5em;"&gt;policy_number&amp;nbsp;&amp;nbsp;&amp;nbsp; tx_date&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; plan_typ&amp;nbsp;&amp;nbsp;&amp;nbsp; plan_desc&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; line-height: 1.5em;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 04JUL2011&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 7&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; VFFC &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I'm wondering if anyone has suggestions or can provide me guidance.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thank you!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 09 Oct 2013 15:52:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Self-Join-Proc-SQL/m-p/108509#M30249</guid>
      <dc:creator>Shara</dc:creator>
      <dc:date>2013-10-09T15:52:03Z</dc:date>
    </item>
    <item>
      <title>Re: Self-Join Proc SQL</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Self-Join-Proc-SQL/m-p/108510#M30250</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Does the following work (untested):&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; create table want as&lt;/P&gt;&lt;P&gt;select policy_number, tx_date, min(plan_type) as min_plan_type, plan_desc&lt;/P&gt;&lt;P&gt;group by policy_number, tx_date&lt;/P&gt;&lt;P&gt;having plan_typ=calculated min_plan_type;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This is also a very easy data step problem using first logic. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc sort data=have; by policy_number tx_date plan_type plan_desc;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data want;&lt;/P&gt;&lt;P&gt;set have;&lt;/P&gt;&lt;P&gt;by policy_number tx_date;&lt;/P&gt;&lt;P&gt;if first.tx_date;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 09 Oct 2013 16:07:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Self-Join-Proc-SQL/m-p/108510#M30250</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2013-10-09T16:07:14Z</dc:date>
    </item>
    <item>
      <title>Re: Self-Join Proc SQL</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Self-Join-Proc-SQL/m-p/108511#M30251</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Question about the above:&amp;nbsp; having plan_typ =calculated min_plan_type&lt;/P&gt;&lt;P&gt;This is giving me an error but I don't understand why. It doesn't like something about it.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 09 Oct 2013 16:13:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Self-Join-Proc-SQL/m-p/108511#M30251</guid>
      <dc:creator>Shara</dc:creator>
      <dc:date>2013-10-09T16:13:06Z</dc:date>
    </item>
    <item>
      <title>Re: Self-Join Proc SQL</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Self-Join-Proc-SQL/m-p/108512#M30252</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;The data step solution you provided does work.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Unfortunately, I must do this one in proc sql and can't use a data step. &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 09 Oct 2013 16:21:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Self-Join-Proc-SQL/m-p/108512#M30252</guid>
      <dc:creator>Shara</dc:creator>
      <dc:date>2013-10-09T16:21:24Z</dc:date>
    </item>
    <item>
      <title>Re: Self-Join Proc SQL</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Self-Join-Proc-SQL/m-p/108513#M30253</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Can you post the log?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 09 Oct 2013 16:22:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Self-Join-Proc-SQL/m-p/108513#M30253</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2013-10-09T16:22:36Z</dc:date>
    </item>
    <item>
      <title>Re: Self-Join Proc SQL</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Self-Join-Proc-SQL/m-p/108514#M30254</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I don't know if the log will be useful. I'm using SAS as a pass through. I've tried running your solution separate of the connection to our database and it works in a 2 step process where I extract just the 4 variables and then separately run the proc sql solution you gave above.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Here's my code, based on your help. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc sql; &lt;/P&gt;&lt;P&gt;connect to XXXX as IQ (user = XXX password = XXX host='XXX' database='XXX' server='XXX' port='XXX'); &lt;/P&gt;&lt;P&gt;create table Plan_home_details as&lt;/P&gt;&lt;P&gt;select * from connection to IQ&lt;/P&gt;&lt;P&gt;(&lt;/P&gt;&lt;P&gt;select&lt;/P&gt;&lt;P&gt;p.pol_no,&lt;/P&gt;&lt;P&gt;p.tx_pol_ver_dt,&lt;/P&gt;&lt;P&gt;min(p.plan_typ) as min_plan_type,&lt;/P&gt;&lt;P&gt;p.plan_desc&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;from FDSS_ANA2.pla_pol&amp;nbsp;&amp;nbsp;&amp;nbsp; p&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;where p.pol_no = 123456789&amp;nbsp;&amp;nbsp; /*I'm testing on a single policy number*/&lt;/P&gt;&lt;P&gt;and p.pol_tx_typ in (3,4,5)&lt;/P&gt;&lt;P&gt;group by p.pol_no, p.tx_pol_ver_dt&lt;/P&gt;&lt;P&gt;having p.plan_typ=calculated min_plan_type&lt;/P&gt;&lt;P&gt;);&lt;/P&gt;&lt;P&gt;disconnect from IQ;&lt;/P&gt;&lt;P&gt;quit; &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The log says the following:&lt;/P&gt;&lt;P&gt;ERROR: CLI prepare error: [Sybase][ODBC Driver][Sybase IQ]Syntax error near 'min_plan_type' on line 1&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 09 Oct 2013 16:34:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Self-Join-Proc-SQL/m-p/108514#M30254</guid>
      <dc:creator>Shara</dc:creator>
      <dc:date>2013-10-09T16:34:08Z</dc:date>
    </item>
    <item>
      <title>Re: Self-Join Proc SQL</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Self-Join-Proc-SQL/m-p/108515#M30255</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I'm fairly positive it is because CALCULATED is for select and where statement references and is not required for HAVING statement. Simply using the column alias should be sufficient. The where statement normally applies before aggregate functions whereas having applies naturally after.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;TABLE border="1" cellpadding="6" cellspacing="0"&gt;&lt;TBODY&gt;&lt;TR valign="bottom"&gt;&lt;TH align="left" valign="top" width="49%"&gt;HAVING clause attributes&lt;/TH&gt;&lt;TH align="left" valign="top" width="49%"&gt;WHERE clause attributes&lt;/TH&gt;&lt;/TR&gt;&lt;TR valign="top"&gt;&lt;TD align="left" valign="top" width="49%"&gt;is typically used to specify conditions &lt;BR /&gt;for including or excluding groups of rows from a table.&lt;/TD&gt;&lt;TD align="left" valign="top" width="49%"&gt;is used to specify conditions for &lt;BR /&gt;including or excluding individual rows from a table.&lt;/TD&gt;&lt;/TR&gt;&lt;TR valign="top"&gt;&lt;TD align="left" valign="top" width="49%"&gt;must follow the GROUP BY clause in a &lt;BR /&gt;query, if used with a GROUP BY clause.&lt;/TD&gt;&lt;TD align="left" valign="top" width="49%"&gt;must precede the GROUP BY clause in a &lt;BR /&gt;query, if used with a GROUP BY clause.&lt;/TD&gt;&lt;/TR&gt;&lt;TR valign="top"&gt;&lt;TD align="left" valign="top" width="49%"&gt;is affected by a GROUP BY clause; when &lt;BR /&gt;there is no GROUP BY clause, the HAVING clause is treated like a WHERE &lt;BR /&gt;clause.&lt;/TD&gt;&lt;TD align="left" valign="top" width="49%"&gt;is not affected by a GROUP BY &lt;BR /&gt;clause.&lt;/TD&gt;&lt;/TR&gt;&lt;TR valign="top"&gt;&lt;TD align="left" valign="top" width="49%"&gt;is processed after the GROUP BY clause and &lt;BR /&gt;any aggregate functions.&lt;/TD&gt;&lt;TD align="left" valign="top" width="49%"&gt;&lt;P&gt;is processed before a GROUP BY clause, if &lt;BR /&gt;there is one, and before any aggregate functions.&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;One of the following codes should fix the issue&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; create table want as&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; select *&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; from have&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; group by policy_number, tx_date&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; having plan_typ=min(plan_typ)&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;*edit* killed the where approach - does not work. However, calculated statement is not necessary in the having clause.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;It's possible that simply removing the CALCULATED statement in Reeza's code will work as well since having is processed after aggregate functions.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Vince&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 09 Oct 2013 16:54:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Self-Join-Proc-SQL/m-p/108515#M30255</guid>
      <dc:creator>Vince28_Statcan</dc:creator>
      <dc:date>2013-10-09T16:54:30Z</dc:date>
    </item>
    <item>
      <title>Re: Self-Join Proc SQL</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Self-Join-Proc-SQL/m-p/108516#M30256</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Please try this, its is working.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;DATA POLICY_DATASET;&lt;BR /&gt;INPUT POLICY_NUMBER $ TX_DATE : DATE9. PLAN_TYP PLAN_DESC $;&lt;BR /&gt;FORMAT TX_DATE DATE9.;&lt;BR /&gt;CARDS;&lt;BR /&gt;1 04JUL2011 15 Agro&lt;BR /&gt;1 04JUL2011 12 GDP&lt;BR /&gt;1 04JUL2011 7 VFFC&lt;BR /&gt;2 05JUL2011 10 Agro&lt;BR /&gt;2 05JUL2011 11 GDP&lt;BR /&gt;2 05JUL2011 20 VFFC&lt;BR /&gt;3 06JUL2011 2 Agro&lt;BR /&gt;3 06JUL2011 10 GDP&lt;BR /&gt;3 06JUL2011 15 VFFC&lt;BR /&gt;;&lt;BR /&gt;RUN;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;PROC SQL;&lt;BR /&gt;CREATE TABLE POLICY_NUMBERS AS&lt;BR /&gt;SELECT POLICY_NUMBER, TX_DATE, PLAN_TYP, PLAN_DESC, MIN(PLAN_TYP) AS MIN_PLAN&lt;BR /&gt;FROM POLICY_DATASET&lt;/P&gt;&lt;P&gt;GROUP BY POLICY_NUMBER, TX_DATE&lt;BR /&gt;HAVING PLAN_TYP=CALCULATED MIN_PLAN&lt;BR /&gt;;&lt;BR /&gt;QUIT;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 09 Oct 2013 17:06:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Self-Join-Proc-SQL/m-p/108516#M30256</guid>
      <dc:creator>UPENDRAKUMARB</dc:creator>
      <dc:date>2013-10-09T17:06:53Z</dc:date>
    </item>
    <item>
      <title>Re: Self-Join Proc SQL</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Self-Join-Proc-SQL/m-p/108517#M30257</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;When you use pass through you need to use SQL that is compliant with your system, so you need to provide the system information, in this case SYBASE. &lt;BR /&gt;Unfortunately I'm not familiar with SYBASE SQL, so I don't know the solution off the top of my head.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;My next suggestion would be to use a subquery and join on the table itself.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 09 Oct 2013 19:16:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Self-Join-Proc-SQL/m-p/108517#M30257</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2013-10-09T19:16:53Z</dc:date>
    </item>
    <item>
      <title>Re: Self-Join Proc SQL</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Self-Join-Proc-SQL/m-p/108518#M30258</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Agree with Vince: "Calculated" is a SAS SQL flavour thing and won't work in Sybase (and other DBMS). Using "having p.plan_typ=min(p.plan_typ)" will work.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;You also need to have "min(p.plan_typ) as min_plan_type" in your select statement. A "select *" won't do. Not sure if Sybase would allow a "select * " syntactically (Oracle wouldn't) but even if it doesn't throw an error (SAS SQL wouldn't) you would end up with one row per distinct value of plan_type per group. Something you don't want. You're only after one row with the minimum value for plan_type.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;select&lt;BR /&gt;&amp;nbsp; p.pol_no,&lt;BR /&gt;&amp;nbsp; p.tx_pol_ver_dt,&lt;BR /&gt;&amp;nbsp; min(p.plan_typ) as min_plan_type,&lt;BR /&gt;&amp;nbsp; p.plan_desc&lt;BR /&gt;&amp;nbsp; from FDSS_ANA2.pla_pol p&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;where &lt;BR /&gt;&amp;nbsp; p.pol_no = 123456789&amp;nbsp;&amp;nbsp; /*I'm testing on a single policy number*/&lt;BR /&gt;&amp;nbsp; and p.pol_tx_typ in (3,4,5)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;group by p.pol_no, p.tx_pol_ver_dt&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;having p.plan_typ=min(p.plan_typ)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If you're familiar with SAS SQL but not with the DB flavour SQL then one "trick" which sometimes works is to write the SQL first in SAS but also using options&lt;/P&gt;&lt;P&gt;OPTIONS SASTRACE=',,,d' SASTRACELOC=SASLOG &lt;EM&gt;NOSTSUFFIX&lt;/EM&gt;;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The SAS/Access engine will try to send as much of your SAS SQL to the database for execution (so translates it to DB flavour SQL). Using above options will show you in the log the SQL SAS was able to send to the DB - and if it was the full SQL then you can copy/paste this SQL from the log into a pass-through SQL block (or at least use it as a starting point).&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 09 Oct 2013 22:08:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Self-Join-Proc-SQL/m-p/108518#M30258</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2013-10-09T22:08:59Z</dc:date>
    </item>
    <item>
      <title>Re: Self-Join Proc SQL</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Self-Join-Proc-SQL/m-p/108519#M30259</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Doesn't SQL usually complain about selecting something that's not in the group by clause or summarized on as well, ie plan_desc?&lt;/P&gt;&lt;P&gt;That's why you usually need a subquery.&amp;nbsp; &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 09 Oct 2013 22:35:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Self-Join-Proc-SQL/m-p/108519#M30259</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2013-10-09T22:35:43Z</dc:date>
    </item>
    <item>
      <title>Re: Self-Join Proc SQL</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Self-Join-Proc-SQL/m-p/108520#M30260</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Yes, some DB SQL flavours do. If "plan_desc" is constant per group then a way around this is to use a max or min function there as well:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;select&lt;BR /&gt;&amp;nbsp; p.pol_no,&lt;BR /&gt;&amp;nbsp; p.tx_pol_ver_dt,&lt;BR /&gt;&amp;nbsp; min(p.plan_typ) as min_plan_type,&lt;BR /&gt;&amp;nbsp; min(p.plan_desc) as plan_desc&lt;BR /&gt;&amp;nbsp; from FDSS_ANA2.pla_pol p&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;where &lt;BR /&gt;&amp;nbsp; p.pol_no = 123456789&amp;nbsp;&amp;nbsp; /*I'm testing on a single policy number*/&lt;BR /&gt;&amp;nbsp; and p.pol_tx_typ in (3,4,5)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;group by p.pol_no, p.tx_pol_ver_dt&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;having p.plan_typ=min(p.plan_typ)&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 09 Oct 2013 23:03:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Self-Join-Proc-SQL/m-p/108520#M30260</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2013-10-09T23:03:28Z</dc:date>
    </item>
    <item>
      <title>Re: Self-Join Proc SQL</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Self-Join-Proc-SQL/m-p/108521#M30261</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt; &lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 12pt; font-family: Courier New;"&gt;Try the following code&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt; &lt;/P&gt;&lt;P&gt;Proc sort data=have ;&lt;BR /&gt; by policy_number tx_date plan_typ;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;data want ;&lt;BR /&gt;set have;&lt;BR /&gt; by policy_number tx_date plan_typ;&lt;/P&gt;&lt;P&gt; if first.policy_number then output;&lt;BR /&gt;run;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 10 Oct 2013 05:26:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Self-Join-Proc-SQL/m-p/108521#M30261</guid>
      <dc:creator>Mit</dc:creator>
      <dc:date>2013-10-10T05:26:42Z</dc:date>
    </item>
    <item>
      <title>Re: Self-Join Proc SQL</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Self-Join-Proc-SQL/m-p/108522#M30262</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I don't think it's necessary to calculate the minimum in the select statement. The value of the minimum is stored for some row(s) of a group and thus the having statement will select the row(s) with value equal to minimum. This causes a remerging before having statement applies but realistically, plan_desc would cause the remerging anyway so there is no gain in calculating a minimum on plan_typ in the select statement unless you also edit the way you are computing plan_desc to not cause the remerging.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I also think this is what Reeza refers to. Including plan_desc in the query causes a warning (or a note) saying that the query caused a remerging. Which is not necessarily a bad thing - it would be just as long to do subquery + merge or self join.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;You did point out that if somehow you can use another way to derive plan_desc either through an aggregate function or possibly a case over the calculated min(plan_typ), then there could be a real gain but if not, then there is no need to create a column for min(plan_typ) in the select statement as the having will only retain the corresponding appropriate row(s).&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 10 Oct 2013 11:59:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Self-Join-Proc-SQL/m-p/108522#M30262</guid>
      <dc:creator>Vince28_Statcan</dc:creator>
      <dc:date>2013-10-10T11:59:40Z</dc:date>
    </item>
    <item>
      <title>Re: Self-Join Proc SQL</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Self-Join-Proc-SQL/m-p/108523#M30263</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;"The value of the minimum is stored for some row(s) of a group and thus the having statement will select the row(s) with value equal to minimum"&lt;/P&gt;&lt;P&gt;That will work with SAS SQL but I'm pretty sure that with Oracle SQL it wouldn't and you have to use some function for all columns not being part of the group by clause.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I'm not sure if this behaviour is Oracle specific or an "ANSI standard" but using a function will always work - and as I don't have experience with the Sybase SQL flavour that's what I proposed to be on the safe side. Else it's the sub-query as Reeza proposed.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 10 Oct 2013 12:40:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Self-Join-Proc-SQL/m-p/108523#M30263</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2013-10-10T12:40:14Z</dc:date>
    </item>
  </channel>
</rss>

