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.

Need help with a specific ExecuteSQL statement

Featured Replies

Howdy all,

 

I could ask this in Stack Overflow or similar but it seems FM SQL has certain limitations when creating more complex requests (joins, group by, etc).

 

I have a table of sales figures (location_id, date, amount).

 

Assuming there's only one location, and I want to graph the sales over time...

 

x-axis:

ExecuteSQL("SELECT date FROM sales_figures ORDER by date ASC") - I'd get a list of all the dates

 

y-axis:

ExecuteSQL("SELECT amount FROM sales_figures ORDER by date ASC") - I'd get a list of all the amounts

 

The problem is multiple locations are in the list and I want the graph to show sales data for one or more locations that I select. For the x-axis (dates) I can SELECT distinct... easy.

 

 

But what about the sales figures on the y-axis? I only want ONE amount for each date... So I want to start with the first date, find all the locations for that date, sum up their sales amounts, and then move onto the next date.

 

Any ideas on how to form an ExecuteSQL statement to achieve that?

Perhaps something like this...

ExecuteSQL ( "SELECT "date", SUM(amount) FROM sales_figures GROUP BY "date" ORDER BY "date"", '|', char (13) )

Tim

  • Author
Ah... thanks... Your suggestion was good for all locations, but what if I just want one selected location, or 2, or all?
 
I ended up doing this:
 
Let (
 
location_list = Substitute ( dashboard::location_ids ; "¶" ; " OR location_id =" );
 
ExecuteSQL ( "SELECT SUM(amount) FROM sales_figures WHERE (location_id = " & location_list & ") GROUP BY date_start ORDER BY date_start"; ""; "" )
 
)

what if I just want one selected location, or 2, or all?

I ended up doing this:

 

Try IN:

Let (
  locationIDs = Substitute ( dashboard::location_ids ; ¶ ; "," ) ;
  ExecuteSQL ( "
    SELECT SUM ( amount ) 
    FROM sales_figures 
    WHERE location_id IN (" & locationIDs & ")  
    GROUP BY date_start 
    ORDER BY date_start 
    "; "" ; "" 
  )
)
  • Author

Well what I implemented with Tom's help has worked.

 

I guess "IN" is another method that will work too, though I haven't tested.

 

Thanks guys!

Create an account or sign in to comment

Important Information

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

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.