This multiple SELECT QUERY runs consecutively. I need one report with multiple columns for [GBPMID] and [EURMID].
Query 1
SELECT
(([askPrice] - [bidPrice]) / 2) + [bidPrice] AS [EURMID]
FROM TicksForex
WHERE [Symbol] = 'EUR/USD'
AND [Time] >= CONVERT(datetime, '6/6/2011 12:00 AM')
Query 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 - MY APOLOGIES! My Data tables do not share the same TIME Values... which was a surprise to me.. this is why the records do not line up.. so sorry!! I will run this as @Pranay describes below...
Forget it! It normalized the data (date) so they all shared the same date - ie.. updated prices开发者_如何学编程 at the same time.. and it still does not work!!
Make use of Case...when
will resolve you issue easily
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')
Try this:
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
精彩评论