Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select only half the records

Tags:

tsql

I am trying to figure out how to select half the records where an ID is null. I want half because I am going to use that result set to update another ID field. Then I am going to update the rest with another value for that ID field.

So essentially I want to update half the records someFieldID with one number and the rest with another number splitting the update basically between two values for someFieldID the field I want to update.

like image 362
PositiveGuy Avatar asked Mar 26 '09 16:03

PositiveGuy


People also ask

How do I select half of a record in MySQL?

Try this: select * from aadhar limit ((select count(*) from aadhar)/2); The count clause returns the number of records, and half of that value is taken in limit.

How do I limit one record in SQL?

The SQL LIMIT clause restricts how many rows are returned from a query. The syntax for the LIMIT clause is: SELECT * FROM table LIMIT X;. X represents how many records you want to retrieve. For example, you can use the LIMIT clause to retrieve the top five players on a leaderboard.

How do I select only certain rows in SQL?

Selection symbols to narrow row selection To select rows using selection symbols for character or graphic data, use the LIKE keyword in a WHERE clause, and the underscore and percent sign as selection symbols.


2 Answers

In oracle you can use the ROWNUM psuedocolumn. I believe in sql server you can use TOP. Example:

select TOP 50 PERCENT * from table
like image 200
Shea Avatar answered Oct 23 '22 02:10

Shea


You can select by percent:

 SELECT TOP 50 PERCENT *fields* FROM YourTable WHERE ...
like image 30
Dana Avatar answered Oct 23 '22 03:10

Dana