Median Calculation on a Column in a Table

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sqls33ker
    New Member
    • Feb 2012
    • 6

    #1

    Median Calculation on a Column in a Table

    Hi there,

    I have the following code:

    Code:
    Select gender,
    CASE
    when gender = 'F' then 'Female'
    when gender = 'M' then 'Male'
    else 'Unknown'
    end as test,
    datediff(day, [tbl_record], getdate()) as 'datediffcal',
    from [tbl_record]
    I need to calculate a median on datediffcal column, but not sure how should the syntax goes.

    It doesn't matter if the result shows only a median. For example, if it just shows:

    median
    15

    that's fine. I just need to learn how the median calculation is done in T-SQL.

    Any ideas?
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    Take the last record of the top 50 percentile and the first record of the bottom 50 percentile. Add them together and divide by 2. That will give you your median.

    Comment

    • sqls33ker
      New Member
      • Feb 2012
      • 6

      #3
      I see.

      I have seen some other posts that have similar answer. I will give it a try.

      thank you

      Comment

      Working...