2

私は現在の合計列を達成するのに近づいていますが、私は何か単純なものが欠けていると思います。どんな助けでも大歓迎です。

SET @runtot := 0;
SELECT
  COUNT(adjustment_id) AS Adjustments,
  DATE(FROM_UNIXTIME(shifts.outtime)) AS 'Month',
  (@runtot := @runtot + COUNT(adjustment_id)) AS RT
FROM adjustments
  INNER JOIN shifts
    ON (shifts.shiftID = adjustments.shiftID)
  INNER JOIN employees
    ON (shifts.idnum = employees.idnum)
WHERE YEAR(FROM_UNIXTIME(shifts.outtime)) = '2012'
GROUP BY MONTH(FROM_UNIXTIME(shifts.outtime))
ORDER BY MONTH(FROM_UNIXTIME(shifts.outtime))ASC

上記のコードは次のように出力します。

Adjustments | Month      | RT
34          | 2012-08-29 | 34
161         | 2012-09-01 | 161

RTは調整と一致し、期待どおりに現在の合計が表示されません。

よろしくお願いします!

4

1 に答える 1

2

集計クエリの結果に対して現在の合計を実行する必要があります。

SET @runtot := 0;

SELECT 
    Adjustments,
    Month,
    (@runtot := @runtot + Adjustments) AS RT
FROM ( SELECT
COUNT(adjustment_id) AS Adjustments,
DATE(FROM_UNIXTIME(shifts.outtime)) AS 'Month'
FROM
adjustments
INNER JOIN shifts ON (
shifts.shiftID = adjustments.shiftID
)
INNER JOIN employees ON (shifts.idnum = employees.idnum)
WHERE
YEAR (FROM_UNIXTIME(shifts.outtime)) = '2012'
GROUP BY MONTH(FROM_UNIXTIME(shifts.outtime))
ORDER BY MONTH(FROM_UNIXTIME(shifts.outtime)) ASC
) x
于 2013-01-29T08:49:50.573 に答える