Possible Duplicate:
Function to Calculate Median in Sql Server
I have a table containing two field (more, but not relevant). The fields are Price and Quantity. I want to find several statistically data for this table, and among them is median price when adjusted to quantity.
Today I have a basic-slow-not so good looking function in place that takes the total Quantity and divide by 2. Then I walk the records ordered by price and subtracting the quantity from the total quantity. When the total quantity reached 0, I have my median.
As you may guess, this is slow... very slow. I'm looking for ideas on how to make this better, faster and more super-duper.
I use MS SQL Express 2008...