Solved

Multiple FROM Clauses in VBA code for a single block of code

Posted on 2014-02-01
5
192 Views
Last Modified: 2014-02-03
Why are three FROM clauses needed below..I assume the second FROM clause is needed to build the first ?  If that is the case does the order of the FROM clause matter ?  

Note the below works..I am just trying to understand it....

Select G.Client, G.Date

FROM(Select GROCERY.Client,GROCERY.DATE
FROM (Select LEFT([FullClientName,40) AS Client,Date
FROM strExcelWSName
WHERE [Product] = 'Pizza') AS GROCERY

INNER JOIN (Select LEFT([FullClientName,40) AS Client,Date
FROM strWSName
WHERE [Product] = 'Soybeans' AS AG

ON GROCERY.CLIENT = AG.CLIENT

GROUP BY GROCERY.CLIENT,GROCERY.DATE AS
0
Comment
Question by:upobDaPlaya
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 49

Expert Comment

by:PortletPaul
ID: 39827092
That code would not work "as is", there are syntax errors
0
 
LVL 49

Accepted Solution

by:
PortletPaul earned 350 total points
ID: 39827099
This is a corrected version of the code, and the indentation should help you understand the structure.
SELECT
      G.Client
    , G.DATE
FROM (
            SELECT
                  GROCERY.Client
                , GROCERY.DATE
            FROM (
                        SELECT
                              LEFT([FullClientName], 40) AS Client
                            , [Date]
                        FROM strExcelWSName
                        WHERE [Product] = 'Pizza'
                  ) AS GROCERY
                  INNER JOIN (
                        SELECT
                              LEFT([FullClientName], 40) AS Client
                            , [Date]
                        FROM strWSName
                        WHERE [Product] = 'Soybeans'
                  ) AS AG
                        ON GROCERY.CLIENT = AG.CLIENT
      ) AS G
GROUP BY
      GROCERY.CLIENT
    , GROCERY.[DATE]

Open in new window

I assume the second FROM clause is needed to build the first ?  
YES, they are a series of subqueries.
 If that is the case does the order of the FROM clause matter ?  
Does the order matter? YES
0
 

Author Comment

by:upobDaPlaya
ID: 39827125
OMG ! The indentation makes a huge difference.  So the hierarchy of the sub-queries then is the one that always appears at the top is the one that gets built last..
0
 
LVL 32

Assisted Solution

by:Robberbaron (robr)
Robberbaron (robr) earned 150 total points
ID: 39827298
yes to heiracry..

but i think the last 3 lines should be
GROUP BY
      G.CLIENT
    , G.[DATE]

Open in new window


due to the ) AS G  immediately above.

its possible the outer clause is superfluous as well; the group by could be on the second select, in which case the Group By names would be corrent

eg
      SELECT
                  GROCERY.Client
                , GROCERY.DATE
        FROM (
                        SELECT
                              LEFT([FullClientName], 40) AS Client
                            , [Date]
                        FROM strExcelWSName
                        WHERE [Product] = 'Pizza'
                  ) AS GROCERY
                  INNER JOIN (
                        SELECT
                              LEFT([FullClientName], 40) AS Client
                            , [Date]
                        FROM strWSName
                        WHERE [Product] = 'Soybeans'
                  ) AS AG
                        ON GROCERY.CLIENT = AG.CLIENT
         GROUP BY
              GROCERY.CLIENT
              , GROCERY.[DATE]

Open in new window

0
 

Author Closing Comment

by:upobDaPlaya
ID: 39831483
Thanks all...makes sense
0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question