<?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: SAS sql nested query in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/SAS-sql-nested-query/m-p/240208#M55582</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;select
	location,
	sum(sum(rev1, rev2, rev3, costs)) as branch_profit,
	total_profit
from 
	financials,
	(select
		sum(sum(rev1, rev2, rev3, costs)) as total_profit
	 from financials
	 where location ne ' ' )
where location ne ' '
group by location;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;BR /&gt;The main query joins table &lt;STRONG&gt;financials&lt;/STRONG&gt; with the result from the subquery without a join condition. The result from the subquery (a single observation) is thus crossed with every observation from table &lt;STRONG&gt;financials&lt;/STRONG&gt;. The resulting table, call it &lt;STRONG&gt;A&lt;/STRONG&gt; = (location, rev1, rev2, rev3, costs, total_profit), is then summarized by location to yield another table, call it &lt;STRONG&gt;B&lt;/STRONG&gt; = (location, branch_profit). Tables &lt;STRONG&gt;A&lt;/STRONG&gt; and &lt;STRONG&gt;B&lt;/STRONG&gt; are then joined (&lt;EM&gt;remerged&lt;/EM&gt;) by location to yield the final result. The SQL optimizer might use some tricks to speed things up, but formally, that's how it's done.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You would get the same result (perhaps more efficiently) with the query:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;select
	*
from 
	(select
		location,
		sum(sum(rev1, rev2, rev3, costs)) as branch_profit
	  from financials
	  where location ne ' '
	  group by location),
	 (select
		sum(sum(rev1, rev2, rev3, costs)) as total_profit
	  from financials
	  where location ne ' ' )
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 21 Dec 2015 04:25:55 GMT</pubDate>
    <dc:creator>PGStats</dc:creator>
    <dc:date>2015-12-21T04:25:55Z</dc:date>
    <item>
      <title>SAS sql nested query</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/SAS-sql-nested-query/m-p/240158#M55578</link>
      <description>&lt;P&gt;I have a date set shown below&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;data new;&lt;BR /&gt;input&lt;BR /&gt;location$ &amp;amp;20. date:mmddyy. rev1:dollar. rev2:dollar. rev3:dollar. costs:dollar.;&lt;BR /&gt;format date date9.;&lt;BR /&gt;cards;&lt;BR /&gt;Boston 12/15/2006 $500 $150 $300 -$200&lt;BR /&gt;Boston 12/22/2006 $300 $500 $200 -$200&lt;BR /&gt;Boston 12/29/2006 $100 $200 $50 -$200&lt;BR /&gt;Boston 1/6/2007 $100 $100 $150 -$200&lt;BR /&gt;New York 12/15/2006 . $600 $400 -$400&lt;BR /&gt;New York 12/22/2006 $700 $600 $250 -$400&lt;BR /&gt;New York 12/29/2006 $200 $300 $100 -$400&lt;BR /&gt;New York 1/6/2007 $50 $100 $50 -$400&lt;BR /&gt;;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;need to find -How much profit did each branch earn during the holiday period, in dollars and as a percent of&lt;BR /&gt;total profits?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And got one solution -select&lt;BR /&gt;location,&lt;BR /&gt;sum(sum(rev1,rev2,rev3,costs)) as branch_profit,&lt;BR /&gt;total_profit&lt;BR /&gt;from financials,&lt;BR /&gt;(select&lt;BR /&gt;sum(sum(rev1,rev2,rev3,costs)) as total_profit&lt;BR /&gt;from financials&lt;BR /&gt;where location ne ' ')&lt;BR /&gt;where location ne ' '&lt;BR /&gt;group by location;&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;can someone explain how this query is working and why "total_profit" is there in the main query?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 20 Dec 2015 05:24:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/SAS-sql-nested-query/m-p/240158#M55578</guid>
      <dc:creator>pawandh</dc:creator>
      <dc:date>2015-12-20T05:24:18Z</dc:date>
    </item>
    <item>
      <title>Re: SAS sql nested query</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/SAS-sql-nested-query/m-p/240163#M55579</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
select distinct
location,
sum(sum(rev1,rev2,rev3,costs)) as branch_profit
, (CALCULATED branch_profit/ total_profit *100) as percent
from  financials, (select sum(sum(rev1,rev2,rev3,costs)) as total_profit from  financials where location ne ' ')
where location ne ' '
group by location;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 20 Dec 2015 11:41:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/SAS-sql-nested-query/m-p/240163#M55579</guid>
      <dc:creator>mohamed_zaki</dc:creator>
      <dc:date>2015-12-20T11:41:00Z</dc:date>
    </item>
    <item>
      <title>Re: SAS sql nested query</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/SAS-sql-nested-query/m-p/240208#M55582</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;select
	location,
	sum(sum(rev1, rev2, rev3, costs)) as branch_profit,
	total_profit
from 
	financials,
	(select
		sum(sum(rev1, rev2, rev3, costs)) as total_profit
	 from financials
	 where location ne ' ' )
where location ne ' '
group by location;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;BR /&gt;The main query joins table &lt;STRONG&gt;financials&lt;/STRONG&gt; with the result from the subquery without a join condition. The result from the subquery (a single observation) is thus crossed with every observation from table &lt;STRONG&gt;financials&lt;/STRONG&gt;. The resulting table, call it &lt;STRONG&gt;A&lt;/STRONG&gt; = (location, rev1, rev2, rev3, costs, total_profit), is then summarized by location to yield another table, call it &lt;STRONG&gt;B&lt;/STRONG&gt; = (location, branch_profit). Tables &lt;STRONG&gt;A&lt;/STRONG&gt; and &lt;STRONG&gt;B&lt;/STRONG&gt; are then joined (&lt;EM&gt;remerged&lt;/EM&gt;) by location to yield the final result. The SQL optimizer might use some tricks to speed things up, but formally, that's how it's done.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You would get the same result (perhaps more efficiently) with the query:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;select
	*
from 
	(select
		location,
		sum(sum(rev1, rev2, rev3, costs)) as branch_profit
	  from financials
	  where location ne ' '
	  group by location),
	 (select
		sum(sum(rev1, rev2, rev3, costs)) as total_profit
	  from financials
	  where location ne ' ' )
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 21 Dec 2015 04:25:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/SAS-sql-nested-query/m-p/240208#M55582</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2015-12-21T04:25:55Z</dc:date>
    </item>
  </channel>
</rss>

