开发者

Combine Select SQL into Single Query w/ Mutiple Columns

开发者 https://www.devze.com 2023-03-11 12:47 出处:网络
This multiple SELECT QUERY runs consecutively.I need one report with multiple columns for [GBPMID] and [EURMID].

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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号