Click here to Skip to main content
15,891,423 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
HOW TO SELECT 50% LAST ROW FRO DATABASE TABLE..
like i hv 100 records.i want to get last 50% record .how do i get????


plz help


Thanx
Posted

Try this one

Select TOP 50 PERCENT * from table_name ORDER BY COLUMN_NAME DESC
 
Share this answer
 
v2
Comments
Toniyo Jackson 12-Oct-11 5:21am    
Correct, 5!
Bala Selvanayagam 12-Oct-11 5:23am    
Excellent, my 5!
sravani.v 12-Oct-11 5:24am    
Thank u...TJ
sravani.v 12-Oct-11 5:25am    
Thank you Bala
Pravin Patil, Mumbai 12-Oct-11 5:44am    
Nice answer...
Hi,

you can try this

SQL
SELECT     TOP   (SELECT     COUNT(*) / 2 AS Expr1
  FROM          QuestionTab) qid, qname, opt1, opt2, opt3, opt4, ans
FROM         QuestionTab AS QuestionTab_1
order by qid desc


But here it selects top half record

you can get last half by order by id

I hope you got What I said

All the Best
 
Share this answer
 
v3
Comments
soniya sangal 12-Oct-11 5:12am    
i am getting top 50 % data by
select TOP 50 PERCENT * from posted_data

but i want last
Muralikrishna8811 12-Oct-11 5:14am    
order by id in desc
thn u get last
P.Salini 12-Oct-11 5:15am    
use order by id desc
Bala Selvanayagam 12-Oct-11 5:21am    
Soniya Sangal,

you can swtich between top 50% and last 50% by the order by clause.

- Order by qid desc (50% of last record)
- Order by qid asc (50% of top record)
Use the limit clause:

SQL
SELECT * FROM tablename LIMIT 50, 50


i.e. select 50 records, starting at the 50th record.

This will work for MySQL but we need to know what your DBMS platform is as each platform works differently (e.g. MSSQL doesn't have LIMIT!)

For MSSQL try:

SQL
SELECT TOP 50 PERCENT * from tablename ORDER BY id desc


This works if 'id' is a primary key field which auto increments.
 
Share this answer
 
v2
Comments
Madhav Hatwalne 12-Oct-11 5:29am    
We must use percent
Dave Kerr 12-Oct-11 6:21am    
That's more correct yes, although the post says he has 100 records using TOP 50 PERCENT is safer and will work when the number of records changes.
SQL
Select TOP 50 PERCENT  * from table_name ORDER BY COLUMN_NAME DESC


to get last 50%
 
Share this answer
 
v4
Hello all,
Select top 50 will give only top 50 or last if order by DESC
but

SQL
Select Top 50 Percent * from table_name order by id DESC

will give last 50%

U can set the percentage in a variable and use like
SQL
Declare @Percentage float
set @percentage = 50
Select Top (@percentage) Percent * from table_name order by id DESC
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900