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.

GROUP BY + SORT BY

Featured Replies

I am using this FQL to generate data for a chart. The chart works fine when I have just the GROUP BY clause, but as soon as I add the SORT BY clause, it gives me an error "All non-aggregated column references in the SELECT list and HAVING clause must be in the GROUP BY clause."

Does FQL not let you use both clauses at the same time?

Let ( [ ~sql = "

    SELECT w.~month, SUM(w.~smh)

    FROM ~workorders w

    WHERE w.~resort = 1 AND w.~date = 1

       GROUP BY w.~month

       /* ORDER BY w.~created */ <------ Stops working if I remove the comments.

    

    ";

 

    $sqlQuery = Substitute ( ~sql ;

        [ "~workorders" ; SQLTableName ( WORKORDERS::WorkOrder ) ];

        [ "~resort" ; SQLFieldName ( WORKORDERS::cRework ) ];

        [ "~wo" ; SQLFieldName (WORKORDERS::WorkOrder ) ];

        [ "~smh" ; SQLFieldName ( WORKORDERS::sManHours ) ];

        [ "~created" ; SQLFieldName ( WORKORDERS::timestamp_create ) ];

        [ "~year" ; SQLFieldName ( WORKORDERS::YearCreated ) ];

        [ "~monthnumber" ; SQLFieldName ( WORKORDERS::MonthCreatedNumber ) ];

        [ "~date" ; SQLFieldName ( WORKORDERS::DateCreated11mo ) ];

        [ "~month" ; SQLFieldName ( WORKORDERS::MonthCreated ) ]

    

    );

 

    $$CALLED = List ( /*$$CALLED*/ "" ; Get (CurrentTimeUTCMilliseconds) );

    $sqlResult = ExecuteSQL ( $sqlQuery ; "" ; "" )

];

    //SQLDebugResult(ExecuteSQL ( $sqlQuery ; "" ; "" ))

    //$sqlQuery &\¶&

    If ( $sqlResult = "?" ;

        False;

        Let ( $$CHART.DATA = $sqlResult ; False )

    )

)

I think you need to include your sort-field in the GROUP BY clause, but then, without having the field in the SELECT, you'll have multiple entries per month with no specification. Maybe you just want to sort by month?

 

SELECT w.~month, SUM(w.~smh)

FROM ~workorders w

WHERE w.~resort = 1 AND w.~date = 1

GROUP BY w.~month, w.~created

ORDER BY w.~created

  • Author

Thanks Otmar for the response. It looks like the ORDER BY clause fields have to be in the SELECT statement, which then you have to include them in the GROUP BY clause. If you group by the first one you need, the second one doesn't really matter that much. 

Here's my final query. 

Let ( [ ~sql = "

 

SELECT w.~monthnumber, w.~month, SUM(w.~smh*30)

 

FROM ~workorders w    

 

WHERE w.~resort = 1 AND w.~date = 1 

 

       GROUP BY w.~monthnumber, w.~month 

 

       ORDER BY w.~monthnumber 

Untitled picture.png

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.