Solved

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

Posted on 2014-02-01
5
181 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
  • 2
  • 2
5 Comments
 
LVL 48

Expert Comment

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

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Show hidden user account 7 27
Copying and pasting pictures from Excel 2 19
cannot get subtotal to work 8 17
Gantt chart 2 14
Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
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…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…

705 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now