Stack Exchange
MySQL ORDER BY issues
I'm trying to do too much maybe in one SQL statement. I'd like to get sub-totals and a grand-total all in the appropriate places in the output table. This is my SQL;
SELECT Date, DOW, Week, Year, logdate, Month, monum, netID, Logins, 
       creds, newb, netCnt, TOD, netCnt, activity
  FROM (SELECT logdate
              ,activity
              ,DATE( logdate )                      AS Date
              ,DAYOFWEEK( logdate )         AS DOW
              ,WEEK( logdate,0 )              AS Week
              ,YEAR( logdate )              AS Year
              ,DATE_FORMAT( logdate, '%M' )         AS Month
              ,DATE_FORMAT( logdate, '%m' )     AS monum
              ,CONVERT( netID,UNSIGNED INTEGER )    AS netID
              ,COUNT( callsign )                    AS Logins
              ,COUNT( IF(creds <> '',1,NULL) )  AS creds
              ,COUNT( IF(comments LIKE '%first log in%',1,NULL) ) AS newb
              ,count( DISTINCT netID )      AS netCnt
              ,SUM(  DISTINCT netID)            AS allCnt
              ,SEC_TO_TIME( SUM(timeonduty) )       AS TOD
         FROM NetLog
        WHERE netID <> 0 
          AND activity NOT LIKE '%TEST%'
          AND netcall LIKE '%W0KCN%'
          AND substr(logdate,1,4) = 2017
    GROUP BY Month, netID WITH ROLLUP ) AS t 
    ORDER BY t.logdate , logins

The (abbreviated) output looks like this;

You'll notice that each month is ordered correctly until you get to October. The grand total gets sorted before October instead of at end (after December). Is this something I can control in the SQL or should I fix this in PHP? How would I fix it in SQL?

Turns out I needed a CASE statement to get this working.
SELECT Date, DOW, Week, Year,  Month, monum, netID, Logins, 
       creds, newb, netCnt, TOD, activity, logdate,
       COALESCE (Month, 'GT') as MonthNM
  FROM (SELECT logdate
              ,activity
              ,DATE( logdate )                      AS Date
              ,DAYOFWEEK( logdate )                 AS DOW
              ,WEEK( logdate,0 )                    AS Week
              ,YEAR( logdate )                      AS Year
              ,MONTHNAME ( logdate )                AS Month
              ,MONTH( logdate )                     AS monum
              ,CONVERT( netID,UNSIGNED INTEGER )    AS netID
              ,COUNT( callsign )                    AS Logins
              ,COUNT( IF(creds <> '',1,NULL) )      AS creds
              ,COUNT( IF(comments LIKE '%first log in%',1,NULL) ) AS newb
              ,count( DISTINCT netID )              AS netCnt
              ,SUM(  DISTINCT netID)                AS allCnt
              ,SEC_TO_TIME( SUM(timeonduty) )       AS TOD
         FROM NetLog
        WHERE netID <> 0 
          AND activity NOT LIKE '%TEST%'
          AND netcall LIKE '%W0KCN%'
          AND substr(logdate,1,4) = 2017
    GROUP BY Month, netID WITH ROLLUP ) AS t 
    ORDER BY 
     CASE
        WHEN MonthNM = 'GT' THEN  1
        ELSE 0
     END
        ,monum
        ,logins


Your second field logins will be sorted  accordingly only within equal values of the firs sorted column. Otherwise, sorting priority on the first column. Try to swap logdate and logins and see what's happen. 

I don't think I understand what you mean when you say "swap logdate and logins". Where should I swap them?
