Corey Scheich
asked on
Select (10 to 25) Percent
Is there a syntax to be able to select records between 2 values.
Select Between 10 to 25 percent * from Table1
So if I have a table with 100 rows it would return rows
10 through 25
Similar to Select top 10 percent but with a range.
Select Between 10 to 25 percent * from Table1
So if I have a table with 100 rows it would return rows
10 through 25
Similar to Select top 10 percent but with a range.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SELECT *
FROM (SELECT *,ROW_NUMBER() OVER(ORDER BY YourPrimaryKeyColumn) RN FROM YourTable) Tab
WHERE RN BETWEEN (SELECT COUNT(*) FROM YourTable)*0.1 AND (SELECT COUNT(*) FROM YourTable)/4.0
Sharath, it would be nice if you explain wht that NASAlike query does so maybe we can understand that is really useful. :o)
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks Guys.
I have accepted CGLuttrells answer because it was first and most desirable, though I couldn't get it to work because I do have an XML collumn which Sharath pointed out why. The rest of the answers are all acceptable solutions.
Though it sure would be nice to have a Between that was as simple to addin without extra thinking like TOP.
Thank you all.
Corey
I have accepted CGLuttrells answer because it was first and most desirable, though I couldn't get it to work because I do have an XML collumn which Sharath pointed out why. The rest of the answers are all acceptable solutions.
Though it sure would be nice to have a Between that was as simple to addin without extra thinking like TOP.
Thank you all.
Corey
>> I have accepted CGLuttrells answer because it was first and most desirable, though I couldn't get it to work because I do have an XML collumn which Sharath pointed out why. <<
Even though you don't have XML data type column in your table, you can not use CGLuttrells's solution. In order to use CGLuttrells solution (assuming you dont have XML data type column), you need to wrap the two queries as derived tables and then only you can apply EXCEPT.
See the example.
create table #Temp1(col1 int)
declare @int int
set @int = 1
while @int<=100
begin
insert into #Temp1 values(@int)
set @int = @int + 1
end
select top 25 * from #Temp1 order by col1
except
select top 10 * from #Temp1 order by col1
/*
Msg 156, Level 15, State 1, Line 10
Incorrect syntax near the keyword 'except'.
*/
select * from (select top 25 * from #Temp1 order by col1) t1
except
select * from (select top 10 * from #Temp1 order by col1) t2
drop table #Temp1
ASKER
Thank you for the clarification.
Open in new window