1

我有以下 2 个查询,可以找到每月股票的平均价格

查询1:从上午9.30到下午1.30一个月的平均价格

查询2:从上午1.30到下午2.30一个月的平均价格

我需要划分差异和价格变化百分比

查询1

--- 9.30 to 1.30 prices ---

select DATE_FORMAT(transact_time,'%Y %M %d'),sum(price*qty)/sum(qty) 
from trades1
where symbol='GREG.N0000'
and transact_time between '2013-01-01' and '2013-01-31'
and DATE_FORMAT(transact_time,'%T') between  '09:30:00' and '13:30:00'
group by DATE_FORMAT(transact_time,'%Y %M %d')

查询2

--- 1.30 TO 2.30 prices ---

SELECT DATE_FORMAT(transact_time,'%Y %M %d'),SUM(price*qty)/SUM(qty) 
FROM trades1
WHERE symbol='GREG.N0000'
AND transact_time BETWEEN '2013-01-01' AND '2013-01-31'
AND DATE_FORMAT(transact_time,'%T') BETWEEN  '13:30:00' AND '14:30:00'
GROUP BY DATE_FORMAT(transact_time,'%Y %M %d')

我需要获取 query1 的结果和 query2 的结果的差异以及价格变化百分比。有人请帮我解决这个问题。

4

1 回答 1

1

您是否正在寻找这个:

SELECT p1 - p2 , p2 /p1 FROM
(select DATE_FORMAT(transact_time,'%Y %M %d') d1,sum(price*qty)/sum(qty) p1
from trades1
where symbol='GREG.N0000'
and transact_time between '2013-01-01' and '2013-01-31'
and DATE_FORMAT(transact_time,'%T') between  '09:30:00' and '13:30:00'
group by DATE_FORMAT(transact_time,'%Y %M %d')) t1 JOIN
(
SELECT DATE_FORMAT(transact_time,'%Y %M %d') d2,SUM(price*qty)/SUM(qty) p2
FROM trades1
WHERE symbol='GREG.N0000'
AND transact_time BETWEEN '2013-01-01' AND '2013-01-31'
AND DATE_FORMAT(transact_time,'%T') BETWEEN  '13:30:00' AND '14:30:00'
GROUP BY DATE_FORMAT(transact_time,'%Y %M %d')) t2 ON t1.d1 = t2.d2
于 2013-03-06T07:15:31.060 回答