我有sql查询
SET @cumulative_sum := 0;
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'
ORDER BY fct_sales.datDate ASC
哪个很好,并给我结果表
_______________________________
| datDate| Cumulative_sum|
|____________|________________|
| 2011-01-02 | 5005 |
|____________|________________|
|2011-01-04 | 7007 |
|____________|________________|
然而,我要求的是:
_____________________________
| datDate| Cumulative_sum|
|____________|________________|
| 2011-01-01| 5005 |
|____________|________________|
| 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 |
|____________|________________|
我该如何做到这一点?
答案 0 :(得分:0)
由于您的数据不适合解决您的问题.. 但无论你提供什么,我认为这会对你有帮助..
SELECT datDate, Cumulative_sum from fct_sales where Cumulative_sum > 5005 AND Cumulative_sum < 7007 AND intProductID = '40' ORDER BY datDate ASC
答案 1 :(得分:0)
试试这个
SELECT datDate, Cumulative_sum FROM fct_sales WHERE intProductID='40' AND datDate BETWEEN '2011-01-01' AND '2011-01-10' GROUP BY datDate
答案 2 :(得分:0)
您好基本上对于此查询,您需要3件事:
如果你想要5005总和将在2011-01-01日期我认为这是不可能的,因为如果列表中有超过2个Cumulative_sums,它们会相互重叠......
示例并不漂亮,因为您需要日期范围。有时在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