2013-03-06 82 views
1

我有2個查詢,低於該認定每月股票問題總結

QUERY1的平均價格:平均價格爲一個月,從上午9:30至下午1:30

QUERY2:平均價格爲一個月1.30時至下午2.30

我需要劃分的差異和價格變化的百分比

QUERY1

--- 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') 

QUERY2

--- 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的結果和價格變化的百分比之差。有人請幫助我。

回答

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 
+0

是它的工作原理,對我非常有幫助,非常感謝 – user1793864 2013-03-06 08:21:13

+0

所以,請標記爲回答。謝謝,GL! – www 2013-03-06 08:24:34