如何在MySql

时间:2017-07-09 04:43:35

标签: mysql

我正在尝试创建一个表格,以输入图表程序进行显示。列是日期(每日),Algo52Algo51,行是按日期排列的。我确实让它只用于每日结果。这是查询:

SELECT

BptChart.BptChartDate,
SUM(IF(q.Algo='53',q.PipTotals,0)) As Algo53,
SUM(IF(q.Algo='52',q.PipTotals,0)) As Algo52

FROM (

SELECT

BptAlgo.BptAccountID As Algo,
DATE(BptAlgo.CloseTime) As TradeDate,
SUM(CAST(BptAlgo.Pips AS DECIMAL(7,2))) AS PipTotals

FROM BptAlgo
INNER JOIN BptAccount ON BptAccount.BptAccountID = BptAlgo.BptAccountID
WHERE BptAlgo.Status = 'CLOSED' AND BptAlgo.CloseTime > '1970-01-01 00:00:00' AND BptAccount.Status = 'OPEN'
GROUP BY YEAR(TradeDate), MONTH(TradeDate), DAY(TradeDate), BptAlgo.BptAccountID
ORDER BY CloseTime DESC 
LIMIT 1500

) q

INNER JOIN BptChart ON q.TradeDate = BptChart.BptChartDate
GROUP BY YEAR(BptChart.BptChartDate), MONTH(BptChart.BptChartDate), 
DAY(BptChart.BptChartDate)
ORDER BY BptChart.BptChartDate ASC

我现在想要的是运行总计而不仅仅是每日结果。这是我试过的查询:

SELECT

BptChart.BptChartDate,
(@runtot53 := @runtot53 + (SUM(IF(q.Algo='53',q.PipTotals,0)))) As Algo53,
(@runtot52 := @runtot52 + (SUM(IF(q.Algo='52',q.PipTotals,0)))) As Algo52

FROM (

(SELECT

BptAlgo.BptAccountID As Algo,
DATE(BptAlgo.CloseTime) As TradeDate,
SUM(CAST(BptAlgo.Pips AS DECIMAL(7,2))) AS PipTotals

FROM BptAlgo
INNER JOIN BptAccount ON BptAccount.BptAccountID = BptAlgo.BptAccountID
WHERE BptAlgo.Status = 'CLOSED' AND BptAlgo.CloseTime > '1970-01-01 00:00:00' AND BptAccount.Status = 'OPEN'
GROUP BY YEAR(TradeDate), MONTH(TradeDate), DAY(TradeDate), BptAlgo.BptAccountID
ORDER BY CloseTime DESC 
LIMIT 1500

) q,

(SELECT @runtot53:=0) n53,
(SELECT @runtot52:=0) n52

)

INNER JOIN BptChart ON q.TradeDate = BptChart.BptChartDate
GROUP BY YEAR(BptChart.BptChartDate), MONTH(BptChart.BptChartDate), 
DAY(BptChart.BptChartDate)
ORDER BY BptChart.BptChartDate ASC 

它不起作用,因为它没有给我运行总计,它给出与原始相同的结果。我认为每行的变量都会重置为0。但不确定。注意:图表包只允许输入一个查询,因此不允许使用SET语句。

关于我在这里可以做什么的任何想法?

0 个答案:

没有答案