我有一个简单的表格,其中包含某个组的日期和总销售额:
date | totalsales
=======================
2014-05-01 | 3000
2014-05-02 | 3100
2014-05-03 | 3500
2014-05-04 | 3650
我喜欢计算一些东西:
结果应该看起来像(手动计算所以可能错误:))
date | sales | average | growth
=======================================
2014-05-01 | 0 | 0 | 0
2014-05-02 | 100 | 50 | 100
2014-05-03 | 400 | 166.66 | 400
2014-05-04 | 150 | 162.5 | 37.5
这是否可以在sql语句中使用,还是应该用PHP或其他服务器软件计算?
答案 0 :(得分:3)
假设每个日期都有自己唯一的行,您可以通过加入原来的表来完成:
SELECT t1.Date, CASE WHEN t2.Date IS NULL THEN 0 ELSE (t1.totalsales - t2.totalsales)
END AS sales
FROM table t1
LEFT JOIN table t2 ON t2.Date = DATE_ADD(t1.Date, INTERVAL -1 DAY)
ORDER BY 1
这至少会给你第一列,你应该能够从那里找出其余的数学。在此处使用左连接和CASE语句非常重要,否则您将无法获得表格中的最低日期(第一行)
如果每个日期不获得自己的唯一行,则此方法仍然可行,您只需要在日期列上使用GROUP BY和SUM在子查询中创建数据集。
答案 1 :(得分:2)
以下是每行没有子选择的完整查询:(感谢 @nmarsh 撰写最难的部分)
请参阅SQL小提琴: http://sqlfiddle.com/#!2/be4654/34/0
SELECT
t1.Date,
CASE
WHEN t2.date IS NULL THEN 0 ELSE (t1.totalSales - t2.totalSales)
END AS sales,
CASE
WHEN t2.date IS NULL THEN 0 / (@curRow := @curRow + 1) ELSE ((@curSum := @curSum + (t1.totalSales - t2.totalSales)) / (@curRow := @curRow + 1))
END AS average,
CASE
WHEN t3.date IS NULL AND t2.date IS NULL THEN 0
WHEN t3.date IS NULL THEN (t1.totalSales - t2.totalSales)
WHEN t2.date IS NULL THEN 0 ELSE ((t1.totalSales - t2.totalSales) * 100) / (t2.totalSales - t3.totalSales)
END AS growth
FROM test t1
LEFT JOIN test t2 ON t2.date = DATE_ADD(t1.Date, INTERVAL -1 DAY)
LEFT JOIN test t3 ON t3.date = DATE_ADD(t2.Date, INTERVAL -1 DAY)
JOIN (SELECT @curRow := 0) r
JOIN (SELECT @curSum := 0) ct
ORDER BY 1;
原始表:
date | totalsales
=======================
2014-05-01 | 3000
2014-05-02 | 3100
2014-05-03 | 3500
2014-05-04 | 3650
<强>输出强>
date | sales | average | growth
=======================================
2014-05-01 | 0 | 0 | 0
2014-05-02 | 100 | 50 | 100
2014-05-03 | 400 | 166.66 | 400
2014-05-04 | 150 | 162.5 | 37.5
答案 2 :(得分:0)
您可以使用recursive statement。在每次迭代中,计算一天所请求的数据,并删除第一天(最早)。
你也可以使用PHP,这看起来更好,因为你不想在MySQL表上加载太多负载以防万一它不能节省你的时间/计算。
答案 3 :(得分:0)
对数字不太确定,但如果您对结果更具体,我可以仔细检查。
您可以使用ROW_NUMBER()创建2个数据集并将它们连接到ROW_NUMBER()和ROW_NUMBER() - 1以获取当前值和先前值的偏移量以计算增长。样本:
DECLARE @Data TABLE (SalesDate DATETIME, totalSales INT)
INSERT INTO @Data (SalesDate , totalSales) VALUES ('2014-05-01' , 3000)
INSERT INTO @Data (SalesDate , totalSales) VALUES ('2014-05-02' , 3100)
INSERT INTO @Data (SalesDate , totalSales) VALUES ('2014-05-03' , 3500)
INSERT INTO @Data (SalesDate , totalSales) VALUES ('2014-05-04' , 3650)
SELECT
CurrentDt.SalesDate
,ISNULL(CurrentDt.totalSales - PreviousDt.totalSales ,0) AS Sales
,FirstDate.FirstDate
, NULLIF(CAST((CurrentDt.SalesDate - FirstDate.FirstDate) AS INT)+1,0) AS SellingDays
,(ISNULL(CurrentDt.totalSales - PreviousDt.totalSales ,0))
/ NULLIF(CAST((CurrentDt.SalesDate - FirstDate.FirstDate) AS INT)+1,0) AS AverageSales
FROM
(SELECT Min(SalesDate) AS FirstDate FROM @Data) AS FirstDate,
/*Base Sales Data*/
(
SELECT
ROW_NUMBER() OVER(ORDER BY SalesDate) AS RowNum
,SalesDate
,totalSales
FROM
@Data
) AS CurrentDt
/*Previous Value for Growth*/
LEFT JOIN
(
SELECT
ROW_NUMBER() OVER(ORDER BY SalesDate) AS RowNum
,SalesDate
,totalSales
FROM
@Data
) AS PreviousDt
ON CurrentDt.RowNum -1 = PreviousDt.RowNum
我使用过MSSQL,但MySQL支持ROW_NUMBER OVER。
答案 4 :(得分:0)
我希望此查询可以帮助您
SELECT
sample.id,
sample.date AS oggi,
sample.value AS sales,
((SELECT SUM(sample.value) FROM sample WHERE sample.date <= oggi ) / (SELECT COUNT(sample.value) FROM sample WHERE sample.date <= oggi ) ) AS avarege,
sample.value / IF((SELECT sample.value FROM sample WHERE sample.date = (oggi - INTERVAL 1 DAY )) = 0,sample.value,(SELECT sample.value FROM sample WHERE sample.date = (oggi - INTERVAL 1 DAY ))) *100 AS 'growt-percent'
-- (SELECT SUM(sample.value) FROM sample WHERE sample.date <= oggi ) AS somma,
-- (SELECT count(sample.value) FROM sample WHERE sample.date <= oggi ) AS conta,
-- (SELECT sample.value FROM sample WHERE sample.date = (oggi - INTERVAL 1 DAY )) as valoreieri,
FROM sample
WHERE sample.date BETWEEN '2014-05-01 00:00:00' AND '2014-05-31 00:00:00'
表数据是
id日期值
1 2014-05-01 00:00:00 0 2 2014-05-02 00:00:00 100 3 2014-05-03 00:00:00 400 4 2014-05-04 00:00:00 150 5 2014-05-05 00:00:00 200
结果是
id oggi sales avarege growt-percent
1 2014-05-01 00:00:00 0 0.0000 (NULL) 2 2014-05-02 00:00:00 100 50.0000 100.0000 3 2014-05-03 00:00:00 400 166.6667 400.0000 4 2014-05-04 00:00:00 150 162.5000 37.5000 5 2014-05-05 00:00:00 200 170.0000 133.3333
请注意我使用datetime字段不仅仅是日期 如果您对查询有疑问,请
抱歉我的英文不好编辑 最后3行被评论,因为我只将它用于测试