最好在MySQL中使用WITH ROLLUP

时间:2016-12-20 14:23:09

标签: mysql

我在MySQL中有以下查询;

SELECT *
   FROM
     (SELECT
     DATE( logdate )                     as Date
        ,WEEK( logdate,0 )               as Week
        ,DATE_FORMAT(logdate, '%M')      as Month
        ,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
        ,SEC_TO_TIME(SUM(timeonduty))    as TOD
        ,activity
     FROM NetLog
    WHERE netID <> 0 
          AND netcall = 'W0KCN' 
          AND logdate >= NOW() - INTERVAL 2 MONTH 
          AND activity NOT LIKE '%TEST%'
  GROUP BY netID WITH ROLLUP) AS t
ORDER BY netID DESC

它产生以下内容;

enter image description here

请注意底部的汇总行。我想在Week.Each之后添加汇总,在最后一周之后出现。

我遇到的问题包括;

1)每次我重新运行查询汇总行和其他行时,移动。它们确实出现在不同的行号而不是它们所属的行号上。值是正确的但行是错误的。例如,底部的汇总可能会显示距其当前位置两行,或四个或一个。

2)我希望能够按ASC或DESC进行排序,并且仍然会将汇总行显示在正确的位置,但经过几天(是几天)的反复试验,我仍然无法弄清楚如何那样做。

0 个答案:

没有答案