0

I would like to ask as to how to output a column with the tickerID.

As of now I have the following:

select distinct 
    mtime,  
    avg(lastBid) Bid ,
    avg(lastAsk) Ask 
from 
    (Select 
         a.mtime, a.IntradayTime, f.lastBid, f.lastAsk 
     from 
         (select 
              cte.*, 
              (select top 1 datetime 
               from IntradayHistory_1min.dbo.IntradayDataHistory' + @product + '_1min' + ' 
               where datetime <= cte.mtime 
                 and TickerID = ' + cast(@ticker as nvarchar(24)) + ' 
               order by datetime desc) as IntradayTime 
          from 
              cte) a 
    left join 
        Intradayhistory_1min.dbo.IntradayDataHistory'+ @product + '_1min' + ' f on f.datetime = a.IntradayTime and f.tickerid = ' + cast(@ticker as nvarchar(24)) + ') b 
group by 
    mtime 
order by 
    mtime 
option (maxrecursion 0)

A screenshot of dbo.IntradayDataHistory_Rebar_1min (in this case: Rebar is @product):

enter image description here

Updated for clarification: Output to look like this,

enter image description here

I have tried the following too, which didn't work:

select distinct mtime,
(ticker) tickerid
avg(lastBid) Bid ,
avg(lastAsk) Ask 

from ( Select a.mtime, a.IntradayTime, f.tickerid, f.lastBid, f.lastAsk 
from (select cte.*, (select top 1 datetime from IntradayHistory_1min.dbo.IntradayDataHistory' + @product + '_1min' + ' 
where datetime <= cte.mtime and TickerID = ' + cast(@ticker as nvarchar(24)) + ' order by datetime desc) as IntradayTime from cte ) a 
left join Intradayhistory_1min.dbo.IntradayDataHistory'+ @product + '_1min' + ' f 

on f.datetime = a.IntradayTime and f.tickerid = ' + cast(@ticker as nvarchar(24)) + ' ) b 
group by mtime order by mtime option (maxrecursion 0)

The error I get:

Column 'b.tickerid' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
fauxpas
  • 93
  • 1
  • 9

1 Answers1

0

I found out the solution is to actually add an avg function in front of ticker, so as followed:

select distinct mtime,
avg(ticker) tickerid
avg(lastBid) Bid ,
avg(lastAsk) Ask 

from ( Select a.mtime, a.IntradayTime, f.tickerid, f.lastBid, f.lastAsk 
from (select cte.*, (select top 1 datetime from IntradayHistory_1min.dbo.IntradayDataHistory' + @product + '_1min' + ' 
where datetime <= cte.mtime and TickerID = ' + cast(@ticker as nvarchar(24)) + ' order by datetime desc) as IntradayTime from cte ) a 
left join Intradayhistory_1min.dbo.IntradayDataHistory'+ @product + '_1min' + ' f 

on f.datetime = a.IntradayTime and f.tickerid = ' + cast(@ticker as nvarchar(24)) + ' ) b 
group by mtime order by mtime option (maxrecursion 0)

The reason behind is in here: Column "invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause"

fauxpas
  • 93
  • 1
  • 9
  • you didn't specify in your question that you need an average in your ticker, may be you look for something else? what is the point of taking average of an ID? – Hatik Jul 27 '17 at 03:54