这个多个SELECT QUERY连续运行。我需要一个包含[GBPMID]和[EURMID]多列的报告。
查询1
SELECT
(([askPrice] - [bidPrice]) / 2) + [bidPrice] AS [EURMID]
FROM TicksForex
WHERE [Symbol] = 'EUR/USD'
AND [Time] >= CONVERT(datetime, '6/6/2011 12:00 AM')
查询2
SELECT
[Time]
,[askPrice]
,[bidPrice]
,(([askPrice] - [bidPrice]) / 2) + [bidPrice] AS [GBPMID]
FROM TicksForex
WHERE [Symbol] = 'GBP/USD'
AND [Time] >= CONVERT(datetime, '6/6/2011 12:00 AM')
@Pranay& @Magnus - 我的APOLOGIES!我的数据表不共享相同的TIME值...这对我来说是一个惊喜..这就是为什么记录不排队..很抱歉!我将按照@Pranay在下面描述的那样运行...
算了吧!它标准化了数据(日期),因此他们共享相同的日期 - 即..同时更新价格..但它仍然不起作用!!
答案 0 :(得分:1)
利用Case...when
可以轻松解决问题
SELECT
[Time]
,[askPrice]
,[bidPrice],
( CASE WHEN Symbol =
'GBP/USD' THEN ((([askPrice] - [bidPrice]) / 2) + [bidPrice])
ELSE 0
END) AS [GBPMID],
( CASE WHEN Symbol =
'EUR/USD' THEN ((([askPrice] - [bidPrice]) / 2) + [bidPrice])
ELSE 0
END) AS [EURMID]
FROM TicksForex
WHERE ([Symbol] = 'GBP/USD' or [Symbol] = 'EUR/USD' )
AND [Time] >= CONVERT(datetime, '6/6/2011 12:00 AM')
答案 1 :(得分:0)
试试这个:
SELECT
*
FROM
(SELECT
[Symbol],
[Time],
[askPrice],
[bidPrice],
([askPrice] - [bidPrice]) / 2 As calc
FROM
TicksForex
WHERE
([Symbol] = 'GBP/USD' or [Symbol] = 'EUR/USD') AND
[Time] >= CONVERT(datetime, '6/6/2011 12:00 AM')) As srcTable
PIVOT
(
MAX(calc)
FOR Symbol IN([EUR/USD], [GBP/USD])
) As PivotTable