Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Query Recordcount 1

Status
Not open for further replies.

matrixindicator

IS-IT--Management
Joined
Sep 6, 2007
Messages
418
Location
BE
Hey,

I have a query of about 1.580.000 records.
I need to get tree parts in the underlying table.
1. 500.000
2. 500.000
3. 580.000

The best way (fast) is to do this with a few delete queries.
For the first part I need to say delete everyting that is > 500.000. Can I use something like recordcount and how do I write this in the where condition

 
To get the first 500,000

Code:
SELECT TOP 500000 field1, field2, field3
FROM yourtable
ORDER BY field1

Note that the order by is important as otherwise it will just use the first 500000 records that get retrieved.

You can then use that as a subquery for deleting from the main table. Repeat the same query for the second 500,000 and what is left is the 580,000 records.

John

 
If I understand your requirement, you need to break the query down to 3 separate queries?

How about...

Q1:
SELECT TOP 500000 *
FROM tblNameOfTable

Q2:
SELECT TOP 500000 *
FROM tblNameOfTable
WHERE PrimaryKey Not IN(Q1)

Q2:
SELECT *
FROM tblNameOfTable
WHERE PrimaryKey Not IN(Q1)
AND PrimaryKey Not IN(Q2)


Randy
 
thanks Randy, I think its a good option to do this your way.
Easy and a correct result.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top