Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

# Calculating a financial median  within a stored procedure that contains a function.

Posted on 2006-06-14
Medium Priority
347 Views
How can I calculate the median for the "ratio" filed in the stored procedure below?
----------------------------------------------------------------------------------------------------------------------------------------
b.txroll_streetNumber, b.txroll_StreetName, b.txroll_YearBuilt, b.Txroll_Keymap,
b.txroll_NeighborhoodCode, b.txroll_year, b.txroll_GBA, b.txroll_NoticedLandValue, b.txroll_NoticedTotalValue,
b.txroll_ClassCode, b.txroll_LandSize,
(b.txroll_NoticedTotalValue/a.sale_price) AS ratio
FROM MLS a
INNER JOIN tbl_TaxRoll b
WHERE 1=1
AND b.txroll_NeighborhoodCode IS NOT NULL
AND b.txroll_GBA > 0
AND a.sale_price > 0
AND cad_account_number IN  (select * from dbo.fn_ParseCSVString ('aaa,1160910270005,1160910150035,aaa'))
-- AND a.cad_account_number IN  (select * from dbo.fn_ParseCSVString(@strSelectedList))
ORDER BY ratio
---------------------------------------------------------------------------------------------------------------------------

I'd like to be able to create some sort financial median function but I don't know if that will work on a dynamically generated field.
The ratio field is based on a calculation from other actual fields.

It would be nice to have something like this:

(b.txroll_NoticedTotalValue/a.sale_price) AS ratio,
MY_MEDIAN(ratio) AS myMedian

Is this possible? If so how would one write the function? If not how can it be done directly on this stored procedure.

Thanks.

0
Question by:FastEddie___
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 30

Accepted Solution

nmcdermaid earned 1500 total points
ID: 16907815
Blowing cobwebs out of my brain!..... please remind me, median is where you line all the data points up in order then pick the one in the middle, is that right?

So if you had this data:

Ratio
1
2
2
2
3
5
6
8
10

Then the median woud be 3 (middle data point at row 5)

If you were to create such a function then your output would look like this:

Ratio  Median
1         5
2         5
2         5
2         5
3         5
5         5
6         5
8         5
10        5

Is that what you're after?

Its a simple matter to create a function that accepts a table variable (i.e. a list of numbers) and returns the middle number.

However the tricky part is rewriting your select query to use the median function correctly.

How does this sound so far?

0

Author Comment

ID: 16908077
Hi nmcdermaid,

Yes, that's the correct definition of median and yes, that was the logic that I was after.

However, it seems I don't have to go that route.

It looks like crystal reports.net's median function does support a financial median and not just a statistical median.
Statistical takes the middle value from an even number of rows, disregards the grater value and displays the lesser value. (maybe vice versa).
The financial median takes the two middle values adds them toghether and divides by 2 to get the median.

I'm glad I didn't have to mess with rewiting that select statement.

Thanks again. Point are awarded.

-Eddie

0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
###### Suggested Courses
Course of the Month10 days, left to enroll