Solved

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

Posted on 2006-06-14
2
334 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sql server computed columns 11 31
SQL Querying data from 3 tables, all with 1 common column 4 36
SSIS with VPN COnnection 2 77
Update a text value in another table 10 40
I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

810 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