Solved

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

Posted on 2006-06-14
2
341 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___
[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
  • Learn & ask questions
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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to shrink a transaction log file down to a reasonable size.

726 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