Jump to content
Server Maintenance This Week. ×

GROUP BY + SORT BY


This topic is 3178 days old. Please don't post here. Open a new topic instead.

Recommended Posts

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 )

    )

)

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This topic is 3178 days old. Please don't post here. Open a new topic instead.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

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