473,404 Members | 2,195 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,404 software developers and data experts.

Median?

gateshosting
Ok, I have searched everywhere, and came up with a short-term solution. But I still need [oh no... daughter just spilled coffee all over my laptop and desk!!!! ok, it's clean...] help. I developed a statement that gets a median value from an invoicetotals view. Basically it takes the middle 2 values (in case there is an even number of rows) and averages them. The problem is, I want to do this in a customer grouping, to get all values at once. I have done it with everything, except median. It really needs to perform well as this is my biggest issue.

Anyone have any experience with this? I will post code if I need to, but it will probably only be beneficial if someone has had experience with this.

Best regards,

Michael C. Gates
Dec 6 '06 #1
5 2507
iburyak
1,017 Expert 512MB
You can write a function where you pass a group name and return a median.

Function can be used in Select portion of your query so no loops should be made.

If you need help with a function let me know, please.
Dec 6 '06 #2
I am new to functions. I just finally learned the real difference between views and stored procedures. Well, sort of, lol...

What are functions used for exactly? I mean, I know what they are used for in VB, .NET, etc., but not SQL.

My existing median SP has to be run individually per customer. So if there are 10 customers, that's 10 scripts sent from my ASP page. It is too much overhead because these big companies are impatient, as am I.

Thanks,

Michael C. Gates
Dec 6 '06 #3
iburyak
1,017 Expert 512MB
Try this

[HTML]--1. Create test table
Create table customers(CusID int, Revenue int)
go
--2. Insert test records
insert into customers values (1, 3)
insert into customers values (1, 4)
insert into customers values (1, 5)
insert into customers values (1, 7)
insert into customers values (1, 8)
insert into customers values (1, 9)


insert into customers values (2, 6)
insert into customers values (2, 7)
insert into customers values (2, 8)
insert into customers values (2, 9)
insert into customers values (2, 10)
insert into customers values (2, 11)
insert into customers values (2, 12)

go

--3. Create Function
Create Function GetMedian (@ID int)
returns int
AS
BEGIN
Declare @Median int, @RecordCount int

Declare @TempID table (ID int Identity(1,1), Revenue int)

INSERT INTO @TempID (Revenue)
Select Revenue
FROM customers Where CusID = @ID
ORDER BY Revenue


select @RecordCount = count(*) from @TempID

If @RecordCount = 0 -- no records were found
SELECT @Median = 0
ELSE
BEGIN
If @RecordCount % 2 = 1 -- check if record count is odd
SELECT @Median = Revenue FROM @TempID WHERE Id = (@RecordCount / 2 + 1)
Else
SELECT @Median = (SUM(Revenue) / 2) FROM @TempID WHERE Id in (@RecordCount / 2, @RecordCount / 2 + 1)
END
RETURN @Median
END

-- 4. Get result
select CusID,dbo.GetMedian(CusID)
from (select distinct CusID from customers) a[/HTML]
Dec 7 '06 #4
iburyak
1,017 Expert 512MB
Try this

[PHP]--1. Create test table
Create table customers(CusID int, Revenue int)
go
--2. Insert test records
insert into customers values (1, 3)
insert into customers values (1, 4)
insert into customers values (1, 5)
insert into customers values (1, 7)
insert into customers values (1, 8)
insert into customers values (1, 9)


insert into customers values (2, 6)
insert into customers values (2, 7)
insert into customers values (2, 8)
insert into customers values (2, 9)
insert into customers values (2, 10)
insert into customers values (2, 11)
insert into customers values (2, 12)

go

--3. Create Function
Create Function GetMedian (@ID int)
returns int
AS
BEGIN
Declare @Median int, @RecordCount int

Declare @TempID table (ID int Identity(1,1), Revenue int)

INSERT INTO @TempID (Revenue)
Select Revenue
FROM customers Where CusID = @ID
ORDER BY Revenue


select @RecordCount = count(*) from @TempID

If @RecordCount = 0 -- no records were found
SELECT @Median = 0
ELSE
BEGIN
If @RecordCount % 2 = 1 -- check if record count is odd
SELECT @Median = Revenue FROM @TempID WHERE Id = (@RecordCount / 2 + 1)
Else
SELECT @Median = (SUM(Revenue) / 2) FROM @TempID WHERE Id in (@RecordCount / 2, @RecordCount / 2 + 1)
END
RETURN @Median
END

-- 4. Get result
select CusID,dbo.GetMedian(CusID)
from (select distinct CusID from customers) a[/PHP]
Dec 7 '06 #5
iburyak
1,017 Expert 512MB
Sorry I posted the same twice because I was getting error messages.

Good luck.
Dec 7 '06 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Ross Contino | last post by:
Hello to all: I have been searching the web for examples on how to determine a median value in a mySQL table. I have reviewed the article at...
2
by: Hugo L. | last post by:
I really don't know how to calculate the median. Can anybody help me?
2
by: Bob | last post by:
I have been looking at the code for MedianFind(pDte As String) from the following thread from UtterAccess.com: "Finding Median average grouped by field" I have been able to get it to run using...
8
by: nick.vitone | last post by:
Hi, I'm somewhat of a novice at Access, and I have no experience programming whatsoever. I'm attempting to calculate the statistical median in a query. I need to "Group by" one column and find...
4
by: uspensky | last post by:
I have a table (cars) with 3 fields: VIN, Class, sell_price 101, sports, 10000 102, sports, 11000 103, luxury, 9000 104, sports, 11000 105, sports, 11000 106, luxury, 5000 107, sports, 11000
5
by: jonm4102 | last post by:
I'm trying to calculate the median of some numerical data. The data can only be found in a query (henceforth query 1) field I previously made, and I would prefer to calculate the median in a new...
3
by: Scott | last post by:
I need to take the median from a field of records in a report. Can someone shed the light how to do it. Thanks, Scott
7
by: Bhadan | last post by:
Hello, I have several jagged arrays which have been sorted. I'm trying to find the median of each array. Any tips appreciated. TIA. Bhads.
3
by: mehwishobaid | last post by:
i dont know wat is wrong with my code. when i compile. i get the error saying line 29: error: expression must have pointer-to-object type #include <iostream> using namespace std; #include...
6
by: rrstudio2 | last post by:
I am using the following vba code to calculate the median of a table in MS Access: Public Function MedianOfRst(RstName As String, fldName As String) As Double 'This function will calculate the...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.