SQl 2012 FIRST_Value and Last_Value SubQuery Not Working

I am trying to implement a First_Value and Last_Value Query for TICK Data
My SQL falls over when trying to generate my Open (O) and Close (C) columns.
I have attached 3 minutes of the View data to test. The data is being put in the minute timeframe based on the UTC column.

Pls advise where my syntax is failing with the nested query. I am aiming for 3 lines output with this sample.

SELECT       Count(Id) as Count,Instrument, (MAX(Bid) + MAX(Ask)) / 2 AS H, (MIN(Bid) + MIN(Ask)) / 2 AS L, UTCDateTime AS UTC, SUM(CAST(Volume AS int)) AS V
FROM             (SELECT FIRST_VALUE((MAX(Bid) + MAX(Ask)) / 2)  OVER (order BY UTCDateTime)  as O, * FROM dbo.Vw_AUD_CAD) AS subqry
GROUP BY Instrument, UTCDateTime

Open in new window

TickData3MinutesWorth.xlsx
LVL 7
XGISAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hnasrCommented:
You need group by Bid, Ask

Other issue, the select statement for FIRST_VALUE should return one value when it is selected with * (all fields).
0
XGISAuthor Commented:
Hello hnasr.. i am not sure how that will work. I am attempting to take the average of those two fields and then use it to represent 4 new fields. Open, High, Low and Close.  
This data is being converted to the timeframe of 1 minute so i was grouping based on UTCdatetime.   If I understand what you are saying by grouping by Bid/Ask I would end up with multiple records per minute as each price may change multiple times during the minute.

I was also trying to avoid having to run a second view as I have 100 of these little buggers.
0
hnasrCommented:
What is the expected output?
The attached sheet, shows the raw data.

Upload same excel by adding a sheet, or using the same sheet , showing the expected output after applying the query.
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

XGISAuthor Commented:
Hello hnasr.  like this.  same in new spreadsheet..thankyou

C(ID) Instrument      O                H                      L                C                      V       UTC
5      AUD/CAD              0.981405        0.981455      0.981395        0.981455      85       47:00.0
6      AUD/CAD              0.981545        0.981545      0.981465        0.98149              73       48:00.0
6      AUD/CAD              0.981505        0.981505      0.98139        0.981395      86       49:00.0

The UTC will be proper as per sql datetime, it is purely just the query structure to be able to integrate the First and Last methods using sql 2012, for the calculated "MID" column as calculated in the new SS
TickData3MinutesWorth-Calculated.xlsx
0
XGISAuthor Commented:
The solution was found using the following sql that integrates with an SP.

SELECT * INTO #T FROm Vw_AUD_CAD

DECLARE @TimeInterval BIGINT
SET @TimeInterval=5

SELECT
COUNT(Id) AS CID,
Instrument

,DATEADD(mi,DATEPART(hh,AstTime)*60+(DATEPART(mi,AstTime)/@TimeInterval)*@TimeInterval, CONVERT(Datetime, CONVERT(varchar,AstTime,102))) AS 'Time'
,(SELECT MIN(Price) FROM #T WHERE AstTime = MIN(T.AstTime)) AS 'Open'

, MAX(Price) AS 'High'

, MIN(Price) AS 'Low'
,SUM(Volume) AS Volume
,(SELECT MAX(Price) FROM #T WHERE AstTime = MAX(T.AstTime)) AS 'Close'

FROM #T T

GROUP BY Instrument, DATEADD(mi,DATEPART(hh,AstTime)*60+(DATEPART(mi,AstTime)/@TimeInterval)*@TimeInterval, CONVERT(Datetime, CONVERT(varchar,AstTime,102)))

ORDER BY DATEADD(mi,DATEPART(hh,AstTime)*60+(DATEPART(mi,AstTime)/@TimeInterval)*@TimeInterval, CONVERT(Datetime, CONVERT(varchar,AstTime,102)))

DROP TABLE #T

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
XGISAuthor Commented:
The solution was found by using a modified approach to that of the original sample.. Thankyou for your contributions and time.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.