0

現在の合計を持つmysqlテーブルがあります:

+---------------------+--------+
| Timestamp           | Total  |
+---------------------+--------+
| 2012-07-04 05:35:00 |  1.280 | 1.280-1.280 = 0
| 2012-07-04 09:25:00 |  2.173 | 2.173-1.280 = 0.893
| 2012-07-04 09:30:00 |  2.219 | 2.219-1.280 = 0.939
| 2012-07-04 15:00:00 |  7.778 | 7.778-1.280 = 6.498
| 2012-07-04 21:05:00 | 13.032 | 13.032-1.280 = 11.752
| 2012-07-04 22:00:00 | 13.033 | 13.033-1.280 = 11.753
| 2012-07-05 05:20:00 | 13.033 | 13.033-13.033 = 0
| 2012-07-05 07:10:00 | 13.140 | 13.140-13.033 = 0.107
| 2012-07-05 10:15:00 | 14.993 | 14.993-13.033 = 1.960
| 2012-07-05 11:35:00 | 16.870 | 16.870-13.033 = 3.837
+---------------------+--------+

私が探しているのは、間隔ごとに集計された毎日の増加を決定するクエリです。各行の背後にある計算だけでなく、望ましい結果も表示しようとしました。私は結合ですでにいくつかのことを試しましたが、どういうわけか、毎日の開始値が何であるかを判断できません。

ありがとう。

4

1 に答える 1

0

I can't vouch for the efficiency of this query, but it does get you the results you are looking for:

SELECT t1.`Timestamp`, t1.`Total`, 
      CASE WHEN t1.`timestamp` =
            (SELECT MIN(t2.`Timestamp`)
             FROM myTable t2 
             WHERE DATE(t2.`Timestamp`)=DATE(t1.`Timestamp`)) 
        THEN 0
        ELSE t1.`Total` - (SELECT MIN(t3.`Total`)
              FROM myTable t3
              WHERE DATE(t3.`Timestamp`)=DATE(t1.`Timestamp`))         
        END AS Diff
FROM myTable t1
ORDER BY `Timestamp`

Alternate Solution (more efficient I think)

SELECT  t1.`Timestamp`, t1.`Total`, (t1.`Total` - d1.MinVal) diff
FROM myTable t1 
  INNER JOIN 
     (SELECT DATE(`Timestamp`) ts_date,
             MIN(`Total`) AS MinVal
      FROM myTable
      GROUP BY ts_date) d1
  ON DATE(t1.`Timestamp`) = d1.ts_date
于 2012-08-06T22:46:58.657 に答える