Jump to content
View in the app

A better way to browse. Learn more.

FMForums.com

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

ExecuteSQL SUM of Sales Year1 & Year2

Featured Replies

I have a table that contains items sold over the last 20 years, I would like to run a query where I can see a comparison of sales by year. 

 

Customer#   Name   Year1Sum   Year2Sum

1      ABC Company   5000.00   1000.00

2     BAC Company   2500.00   7500.00

 

I can get the first column SUM to work ok - how do I add the second column in the same results from different records.?

I would use the Virtual List technique, so I can loop by company and ExecuteSQL twice to get the two different years' data.

Why would you want to use ExecuteSQL() here, rather than summary fields?

  • Author

Ok so here is my formula.  Works as a single SELECT statement - how can I get results to be output together Sum of dollars shipped from 2015 and 2016 on the same line.  Records are individually created from invoicing.

 

      Let ( [

           $FiscalYrA = globalFiscalYear;
           $FiscalYrB = $FiscalYrA - 1;
           xTab = "    ";

           $SQLResult = ExecuteSQL ( "SELECT \"Customer Number\", \"Name\", SUM (\"Dollars Shipped\"
                                         FROM Invoices
                                            LEFT JOIN Customer_File ON \"Customer Number\" = \"CustomerNo\"
                                               WHERE FiscalYear = ? AND \"Invoice Number\" <> 000000
                                                  GROUP BY \"Customer Number\", \"Name\"
                                     UNION

                                      SELECT \"Customer Number\", \"Name\", SUM (\"Det Dol Shipped\"
                                         FROM Invoices
                                            LEFT JOIN Customer_File ON \"Customer Number\" = \"CustomerNo\"
                                               WHERE FiscalYear = ? AND \"Invoice Number\" <> 000000
                                                  GROUP BY \"Customer Number\", \"Name\"

                                                      ORDER BY \"Customer Number\"" ; xTab; "¶" ;

                                     /*Misc Items*/ $FiscalYrA; $FiscalYrB );

           $xCount = ValueCount ( $SQLResult )

             ] ;
                   $Var )

 

This all just seems so simple and efficient compared to a sub-summary export to Excel that takes hours.

 

Edited by Mike E

  • 2 weeks later...

1. UNION implies different lines. but there are tricks to have the "records" not use the return for each result row

2. I don't know if nested SELECTS here would work. SELECT field, SELECT(), SELECT()...

3. if you can get values (even two different select statements), you may be able to combine them using FM's text functions. Try changing the column and row separators with something easy to "parse".

beverly

 

 

Create an account or sign in to comment

Important Information

By using this site, you agree to our Terms of Use.

Account

Navigation

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.