Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Max Qty
Message
 
 
General information
Forum:
Visual FoxPro
Category:
Databases,Tables, Views, Indexing and SQL syntax
Title:
Environment versions
Visual FoxPro:
VFP 9
OS:
Windows 10
Network:
Windows 2008 Server
Database:
MS SQL Server
Application:
Desktop
Miscellaneous
Thread ID:
01666828
Message ID:
01666839
Views:
90
This message has been marked as the solution to the initial question of the thread.
>Sir While using SQLSERVER 2012, I have this data
>
>
>CREATE CURSOR table1(id n(3),qty n(5))
>
>INSERT INTO table1 values(1,500)
>INSERT INTO table1 values(2,600)
>INSERT INTO table1 values(3,700)
>INSERT INTO table1 values(4,3500)
>INSERT INTO table1 values(5,900)
>INSERT INTO table1 values(6,1200)
>INSERT INTO table1 values(7,1300)
>INSERT INTO table1 values(8,1500)
>INSERT INTO table1 values(9,1700)
>INSERT INTO table1 values(10,1800)
>
>INSERT INTO table1 values(11,1900)
>INSERT INTO table1 values(12,2000)
>INSERT INTO table1 values(13,2500)
>INSERT INTO table1 values(14,1600)
>INSERT INTO table1 values(15,1000)
>
>
>I Want to get maximum qty from the last 5 rows
>
>I need this row
>
>13 2500
>
>Please
;with cte as (select *, row_number() over (order by Id DESC) as RowIdDesc from Table1),

cte2 as (select *, row_number() over (order by qty DESC) as QtyOrd from cte where RowIdDESC <=5)

select Id, qty from cte2 where QtyOrd = 1; -- max qty for last 5 Ids
If it's not broken, fix it until it is.


My Blog
Previous
Reply
Map
View

Click here to load this message in the networking platform