Solved

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

Posted on 2006-06-14
2
329 Views
Last Modified: 2008-02-01
How can I calculate the median for the "ratio" filed in the stored procedure below?
----------------------------------------------------------------------------------------------------------------------------------------
     SELECT a.cad_account_number, a.date_sold, a.county_name, a.sale_price,
            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
     ON a.cad_account_number = b.txroll_CadAccountNumber
     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
Comment
Question by:FastEddie___
2 Comments
 
LVL 30

Accepted Solution

by:
nmcdermaid earned 500 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

by:FastEddie___
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now