Quantcast
Channel: SQL Service Broker forum
Viewing all articles
Browse latest Browse all 461

Get the last but one value

$
0
0

I would like to get the last row from every order and the quantity of the last row but one:

SELECT o.order_id, o.order_user, o.Q,
Q_before=(SELECT TOP 1 Q FROM dbo.orders WHERE order_id=o.order_id AND order_date<o.order_date 
ORDER BY order_date DESC) FROM dbo.Orders AS o

How can this be done with new functions in SQL 2012?


Viewing all articles
Browse latest Browse all 461

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>