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

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.

###### Who is Participating?

x

Commented:
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 Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.