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.

How/Where to insert ORDER BY command

Featured Replies

Greets, all:

I have a calculation that works as is:

ExecuteSQL ( "  

SELECT Department__lxt, COUNT ( * )  

FROM ESU7_STAFF  

WHERE Status__lxt = 'Active'  

GROUP BY Department__lxt

UNION ALL

SELECT '¶Total' Department__lxt,

COUNT (Department__lxt)

FROM ESU7_STAFF

WHERE Status__lxt = 'Active' " ; Char (9) ; ""

)

 

...but the output of departments is unsorted. (See below.) I'd like to have the departments in ascending order, but I just can't get the syntax right using ORDER BY; how/where should it go?

As always, TIA for your help!

 

 

Screenshot 2024-04-30 at 10.21.27 AM.png

Before starting:

Do you really have a field with the department's name in the staff table? I would expect to see a parent table of departments, with the staff holding only a DepartmentID value.

 

  • Author

Indeed, there is a separate Department table, so in the Staff table there's a field that holds the foreign key and a separate field that holds the name (Department__lxt) as well. (Redundant, I know.)

Edited by Rich S

The ORDER BY clause should come at the very end. But there is a problem here:

The sort will be applied to the entire result - including the "Total" line produced by the 2nd SELECT statement. If it starts with a ¶, it will be sorted to the top. If it starts with a "T", it will come before "Vision".

You could try and prepend a control character to force it to be the last - for example, this seems to work in my test:

ExecuteSQL ( 
"SELECT Department, COUNT (*)  
FROM Staff  
WHERE Status = 'Active'  
GROUP BY Department

UNION

SELECT CHR(127) + '¶Total', COUNT (*)
FROM Staff  
WHERE Status = 'Active' 

ORDER BY 1"
; 
Char (9) ; ""
)

However, I would rather make 2 separate calls to the ExecuteSQL() function and combine the result using the FMP calculation engine.

And of course, if you can produce the same result using a FMP native report, that's always preferable.

 

 

Edited by comment

  • Author

Beauty! Thanks for the tips, too.

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.