こんにちは基本的にこのクエリには3つのものが必要です:
- 日付のセット
- 同じテーブルでfct_salesを結合して、nextDateを取得します
- 日付のセットを作成し、fct_salesに参加しました。
5005の合計が2011-01-01の日付になるようにしたい場合は、不可能だと思います。リストに2つ以上のCumulative_sumがある場合、それらは互いにオーバーラップします...
日付の範囲が必要なため、例はきれいではありません。時々dbには、クエリコードを減らすのに役立つタイムテーブルがあります。これが役に立たない場合は、少なくとも少しは役に立ちます:)
SQLFIDDLE
だからここに私の例:
SELECT
a.Date
,b.Cumulative_sum
FROM
(SELECT DATE_FORMAT(curdate() - INTERVAL (a.a + (10 * b.a) + (100 * c.a)) DAY, '%Y-%m-%d') AS Date
FROM
(SELECT 0 AS a
UNION ALL SELECT 1
UNION ALL SELECT 2
UNION ALL SELECT 3
UNION ALL SELECT 4
UNION ALL SELECT 5
UNION ALL SELECT 6
UNION ALL SELECT 7
UNION ALL SELECT 8
UNION ALL SELECT 9) AS a CROSS
JOIN
(SELECT 0 AS a
UNION ALL SELECT 1
UNION ALL SELECT 2
UNION ALL SELECT 3
UNION ALL SELECT 4
UNION ALL SELECT 5
UNION ALL SELECT 6
UNION ALL SELECT 7
UNION ALL SELECT 8
UNION ALL SELECT 9) AS b CROSS
JOIN
(SELECT 0 AS a
UNION ALL SELECT 1
UNION ALL SELECT 2
UNION ALL SELECT 3
UNION ALL SELECT 4
UNION ALL SELECT 5
UNION ALL SELECT 6
UNION ALL SELECT 7
UNION ALL SELECT 8
UNION ALL SELECT 9) AS c) a,
(SELECT
fc1.datDate,
fc1.Cumulative_sum,
(SELECT MIN(fc2.datDate)
from fc fc2
WHERE fc1.datDate < fc2.datDate ) AS nextDate
from fc fc1) b
where (a.Date between '2011-01-01' and '2011-01-10')
AND a.Date>=b.datDate
AND a.Date < COALESCE(b.nextDate, '2011-01-11')
ORDER BY a.Date, b.Cumulative_sum
結果:
DATE CUMULATIVE_SUM
2011-01-02 5005
2011-01-03 5005
2011-01-04 7007
2011-01-05 7007
2011-01-06 7007
2011-01-07 7007
2011-01-08 7007
2011-01-09 7007
2011-01-10 7007
クエリデータを使用したクエリの例:
SET @cumulative_sum := 0;
SELECT
a.Date
,b.Cumulative_sum
FROM
(SELECT DATE_FORMAT(curdate() - INTERVAL (a.a + (10 * b.a) + (100 * c.a)) DAY, '%Y-%m-%d') AS Date
FROM
(SELECT 0 AS a
UNION ALL SELECT 1
UNION ALL SELECT 2
UNION ALL SELECT 3
UNION ALL SELECT 4
UNION ALL SELECT 5
UNION ALL SELECT 6
UNION ALL SELECT 7
UNION ALL SELECT 8
UNION ALL SELECT 9) AS a CROSS
JOIN
(SELECT 0 AS a
UNION ALL SELECT 1
UNION ALL SELECT 2
UNION ALL SELECT 3
UNION ALL SELECT 4
UNION ALL SELECT 5
UNION ALL SELECT 6
UNION ALL SELECT 7
UNION ALL SELECT 8
UNION ALL SELECT 9) AS b CROSS
JOIN
(SELECT 0 AS a
UNION ALL SELECT 1
UNION ALL SELECT 2
UNION ALL SELECT 3
UNION ALL SELECT 4
UNION ALL SELECT 5
UNION ALL SELECT 6
UNION ALL SELECT 7
UNION ALL SELECT 8
UNION ALL SELECT 9) AS c) a,
(SELECT
fc1.datDate,
fc1.Cumulative_sum,
(SELECT MIN(fc2.datDate) AS datDate
FROM (SELECT fct_sales.datDate, @cumulative_sum := @cumulative_sum + fct_sales.dblTotal AS
cumulative_sum
FROM fct_sales
where
fct_sales.intProductID=40
and fct_sales.datDate between '2011-01-01' and '2011-01-10') fc2
WHERE fc1.datDate < fc2.datDate ) AS nextDate
FROM (SELECT fct_sales.datDate, @cumulative_sum := @cumulative_sum + fct_sales.dblTotal AS
cumulative_sum
FROM fct_sales
where
fct_sales.intProductID=40
and fct_sales.datDate between '2011-01-01' and '2011-01-10' ) fc1) b
where (a.Date between '2011-01-01' and '2011-01-10')
AND a.Date>=b.datDate
AND a.Date < COALESCE(b.nextDate, '2011-01-11')
ORDER BY a.Date, b.Cumulative_sum