简体   繁体   中英

get the price of the first transaction and last transaction

the question is about getting the price of the first transaction and last transaction of company in in each day,i can get the prices in this code

select t.date,t.PriceofShare as 'opening price'
from Trans t, Session s,Orders o
where s.date=t.Sdate
and t.Sdate=o.Sdate
and o.Sdate=s.date
and o.SID='MSFT'

returning this

date               opening price 
16:00:00.0000000    4000000.00
09:00:00.0000000    300000.00 

but i don't know how to get the first one as opening price and last one as last price i tried

select t.date,t.PriceofShare as 'opening price'
from Trans t, Session s,Orders o
where s.date=t.Sdate
and t.Sdate=o.Sdate
and o.Sdate=s.date
and o.SID='MSFT'
and t.date=(select Min(date)
from Trans)
union
select t.date,t.PriceofShare as 'closing price'
from Trans t, Session s,Orders o
where s.date=t.Sdate
and t.Sdate=o.Sdate
and o.Sdate=s.date
and o.SID='MSFT'
and t.date=(select Max(date)
from Trans)

the result was

date               opening price  
16:00:00.0000000    4000000.00

please help could my ER be wrong can i post my ER?

Not exactly sure how you want the show the opening and closing price, but this should give you an idea..

SELECT *
FROM   Session s
       INNER JOIN Orders o
               ON o.Sdate = s.date
       CROSS apply (SELECT TOP 1 t.PriceofShare, t.date
                    FROM   Trans t
                    WHERE  s.date = t.Sdate
                           AND t.Sdate = o.Sdate
                    ORDER  BY t.date) o (OpeningPrice, OpeningPriceDate)
       CROSS apply (SELECT TOP 1 t.PriceofShare, t.date
                    FROM   Trans t
                    WHERE  s.date = t.Sdate
                           AND t.Sdate = o.Sdate
                    ORDER  BY t.date DESC) c (ClosingPrice, ClosingPriceDate)
WHERE  o.SID = 'MSFT' 

Start using INNER JOIN syntax to join the table instead of old style comma separated join. Here is a good article about this Bad habits to kick : using old-style JOINs

Why do you even need session?
Work with this.

select * 
from ( select t.date, t.PriceofShare as 'price'
            , row_number() over (partition by CONVERT(date, t.Sdate) order by t.date desc) as open  
            , row_number() over (partition by CONVERT(date, t.Sdate) order by t.date asc)  as close 
         from Trans t  
         join Orders o
           on o.Sdate = t.Sdate
          and o.SID   = 'MSFT'  
) tt
where tt.open = 1 or tt.close = 1 
order by t.date

Just saw this question. You can use windows function first_value and last_value.

select 
first_value(PriceofShare) over(order by t.date) as first_value,
last_value(PriceofShare) over(order by t.date) as last_value
from Trans t, Session s,Orders o
where s.date=t.Sdate
and t.Sdate=o.Sdate
and o.Sdate=s.date
and o.SID='MSFT'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM