Select all rows but the last 480 in SQL

Basically What I want to do is
if the count of rows for a job is > 480. I want to select all but the most current 480 so I  can bulk transfer those out  to a history table
public void DoMove(int jobID)
{
DECLARE @TheCount INT;
SET @TheCount = SELECT COUNT(UID) FROM SS_Pressure WHERE JobID = "+ JobID +";

IF @TheCount > 480

SELECT Top ? FROM SS_Pressure Where JobID = "+ JobID +"
}
this is pseudo code I just need help with the queries
LVL 6
r3nderAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Vitor MontalvãoMSSQL Senior EngineerCommented:
What is the criteria to be on the TOP 480?
When you know it just adjust the following script to get what you want:
DECLARE @TheCount INT;
SET @TheCount = SELECT COUNT(UID) FROM SS_Pressure WHERE JobID = "+ JobID +";

IF @TheCount > 480
 SELECT S1.* 
 FROM SS_Pressure S1
 WHERE NOT EXISTS (SELECT TOP 480 NULL
		FROM SS_Pressure S2
		WHERE S1.JobID = S2.JobID)

Open in new window

Mike EghtebasDatabase and Application DeveloperCommented:
DECLARE @OffsetRows int = 0;
Select @OffsetRows = Count(*) From Table1;
Set @OffsetRows = @OffsetRows - 480;

SELECT *
FROM Table1
WHERE supplierid = 1
ORDER BY ID
OFFSET 481 ROWS FETCH FIRST @OffsetRows ROWS ONLY;

Open in new window


will select all past 460 record for you to work with.

Mike

This post has been revised...
Scott PletcherSenior DBACommented:
Presumably you have some type of datetime, as you reference the "most current".

SELECT <list_of_all_needed_columns>
FROM (
    SELECT *,
        ROW_NUMBER() OVER(ORDER BY datetime_column DESC) AS row_num
    FROM SS_Pressure
    WHERE JobID = <variable_job_id>
) AS derived
WHERE
    row_num > 480

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
r3nderAuthor Commented:
Thanks Scott!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.