473,387 Members | 3,033 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

top n percentage

Hello!

I have a need to create a function that will return a sublist of
elements from a table based on an associated column value and its
ranking within the table (based on a percentage range).

To expand with a simple example (only 4 elements):

Element | Value
---------------
A102 | 5
A506 | 10
A322 | 15
A342 | 20

I would like to be able to return the range of elements that lie in the
50-75% value range (e.g. third quartile range). Therefore with only
these four elements I would want to return the element code 'A322'.

I have attempted to achieve this by using SELECT TOP n PERCENT. I can
bring back the required range by combining two select percent
statements.

SELECT Element from ELEMENT_TABLE
WHERE Element IN
(SELECT TOP 75 PERCENT Element FROM ELEMENT_TABLE ORDER BY Value)
AND Element NOT IN
(SELECT TOP 50 PERCENT Element FROM ELEMENT_TABLE ORDER BY Value)

The problem is that I cannot seem to pass a variable to this percentage
so if I want another percentage range set, I ave to define another
function.

My question therefore is does anyone know either

(1) a way I can pass a variable (n) to these SELECT n PERCENT
statements

OR

(2) an alternative way of doing this?

Many thanks!

Andrew

Jul 23 '05 #1
3 6329
Stu
You can do this using dynamic SQL, like so:

DECLARE @SQL nvarchar(4000)
DECLARE @BeginRange int
DECLARE @EndRange int

SET @BeginRange = 50
SET @EndRange = 75

SET @SQL = 'SELECT Element from ELEMENT_TABLE
WHERE Element IN
(SELECT TOP ' + CONVERT(varchar(3), @EndRange) + ' PERCENT Element
FROM ELEMENT_TABLE ORDER BY Value)
AND Element NOT IN
(SELECT TOP ' + CONVERT(varchar(3), @BeginRange) + ' PERCENT
Element FROM ELEMENT_TABLE ORDER BY Value) '

EXEC (@SQL)
HTH,
Stu

Jul 23 '05 #2
that is excellent. Now all working :-) thanks.

Jul 23 '05 #3
[posted and mailed]

andrew (ac****@gmail.com) writes:
My question therefore is does anyone know either

(1) a way I can pass a variable (n) to these SELECT n PERCENT
statements


Use SQL 2005. :-)

Dynamic SQL is probably the way to go, but there are a couple of
caveats. Before you start to use dynamic SQL all over town, you
might want to learn more about it. I have an article on my web site
about it: http://www.sommarskog.se/dynamic_sql.html.


--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Conax | last post by:
Hello, This is actually a general programming question but I don't know which newsgroup best to put it so please pardon me. I've always been looking for the best way to get an accurate...
7
by: Rob Meade | last post by:
Hi all, I have a recordset iterating through and dumping out to the screen a series of percentages, using the precision 5 and numericscale 2 etc. When I dump them to the page some of the...
2
by: thomasamillergoogle | last post by:
I am going to be distributing a shareware application soon and would like to know what percentage of MS OS machines have the framework installed. I have looked in multiple places (google,...
22
by: Les Juby | last post by:
I am trying to adjust the window/table size of a website (www.worklaw.co.za) which has made use of DIV tags with its settings embedded in an CSS file. The client wants its width and height to...
0
by: fake ID | last post by:
Since you can't search for these symbols used in asp.net "<%#" or '<%=' I thought i'd post this to make things a little easier to find. Potential search word combinations: -lessthan Percentage...
5
by: James Conrad StJohn Foreman | last post by:
Have found http://www-128.ibm.com/developerworks/db2/library/techarticle/lyle/0110lyle.html which is quite helpful, but doesn't quite tell me what I want. I have a table, advertising_spend with...
6
by: Hacking Bear | last post by:
Hi, I still don't quite fully understand how to handle mixing border/margin pixel width with percentage width. In the example below, I want to place side-by-side two DIV boxes inside a box....
4
by: Micheal | last post by:
Greetings Access Group, Being relatively new to Access, I try to work through problems on my own and have been very successful, although I have a conundrum that I have been working on for two days...
8
Nathan H
by: Nathan H | last post by:
I am trying to write a query that will result in showing records that have a large percentage difference among fields in a table. For instance: Table field1.Item field2.account1 (percentage)...
5
by: Aswanth | last post by:
I'm Using Asp.Net with C# & Working with SSRS 2005 for Generating Reports.. The Following Expression I'm using in Reports to Show the Percentage of Particular Items in REPORT.. ...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.