GabrielVa
GabrielVa

Reputation: 2388

SQL Query Max ID

Working on a SQL query here, I have an ID column that I created and I want to be able to take the max ID number and show the 12 behind it. See picture. I want to show Row 20-8 and everything above 8, dismiss. Can someone help me with an orderby or between statement?

Thanks

enter image description here

my sql query:

SELECT TOP (100) PERCENT ROW_NUMBER() OVER (ORDER BY FiscalYear, FiscalMonth) as ID,FiscalYear, FiscalMonth, SUM(STDCOST) AS STDCost, Concat FROM dbo.[13568] GROUP BY FiscalYear, FiscalMonth, Concat ORDER BY FiscalYear, FiscalMonth

Upvotes: 0

Views: 216

Answers (1)

Cade Roux
Cade Roux

Reputation: 89661

Am I missing something?

SELECT * FROM (
    SELECT TOP 13 ROW_NUMBER() OVER (ORDER BY FiscalYear, FiscalMonth) as ID
        ,FiscalYear
        ,FiscalMonth
        ,SUM(STDCOST) AS STDCost
        ,Concat
    FROM dbo.[13568]
    GROUP BY FiscalYear, FiscalMonth, Concat
    ORDER BY FiscalYear DESC, FiscalMonth DESC
) AS X
ORDER BY FiscalYear, FiscalMonth

Upvotes: 1

Related Questions