Skip to content
Advertisement

SQL – Calculated Column

I have a table which shows me a stock ticker, asofdate, open, close. I have created a computed column called daily PL which is simply the daily close-open.

I cannot figure out the logic of using the {open} from the first asofdate and the {close} from the most recent asofdate. and the table will naturally grow, so the logic needs to be dynamic. see below:

enter image description here

Advertisement

Answer

You can use window functions. I think first_value() does what you want — getting the first open price for the ticker:

If you just want one row in the result set for each ticker, then you can use:

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement