Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET
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 12-Oct-11 0:01am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Try this one
 
Select TOP 50 PERCENT * from table_name ORDER BY COLUMN_NAME DESC
  Permalink  
v2
Comments
|T|oniyo |J|ackson at 12-Oct-11 5:21am
   
Correct, 5!
Bala Selvanayagam at 12-Oct-11 5:23am
   
Excellent, my 5!
Simple.v at 12-Oct-11 5:24am
   
Thank u...TJ
Simple.v at 12-Oct-11 5:25am
   
Thank you Bala
Pravin Patil, Mumbai at 12-Oct-11 5:44am
   
Nice answer...
Simple.v at 12-Oct-11 6:31am
   
Thank you..
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi,
 
you can try this
 
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
  Permalink  
v3
Comments
soniya sangal at 12-Oct-11 5:12am
   
i am getting top 50 % data by
select TOP 50 PERCENT * from posted_data
 
but i want last
Muralikrishna8811 at 12-Oct-11 5:14am
   
order by id in desc
thn u get last
CodeHelper_PS at 12-Oct-11 5:15am
   
use order by id desc
Bala Selvanayagam at 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)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Use the limit clause:
 
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:
 
SELECT TOP 50 PERCENT * from tablename ORDER BY id desc
 
This works if 'id' is a primary key field which auto increments.
  Permalink  
v2
Comments
mhatwalne at 12-Oct-11 5:29am
   
We must use percent
Dave Kerr at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Select TOP 50 PERCENT  * from table_name ORDER BY COLUMN_NAME DESC
 
to get last 50%
  Permalink  
v4
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

Hello all,
Select top 50 will give only top 50 or last if order by DESC
but
 
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
Declare @Percentage float
set @percentage = 50
Select Top (@percentage) Percent * from table_name order by id DESC
  Permalink  
v4

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

  Print Answers RSS
0 OriginalGriff 280
1 Jochen Arndt 150
2 Kornfeld Eliyahu Peter 120
3 PIEBALDconsult 110
4 Richard MacCutchan 105
0 OriginalGriff 6,055
1 DamithSL 4,621
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,500
4 Sergey Alexandrovich Kryukov 3,294


Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 12 Oct 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100