Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Median Calculation

Status
Not open for further replies.

SQLMeToo

Technical User
Apr 10, 2000
12
AU
I would like to know how to calculate the median of<br>multiple fields in my table. The result would be<br>Field 1 Median = X, Field 2 Median = Y<br><br>This is my SQL Median Statement for a single field<br>--------------------------------------------------<br>DECLARE @A_Median <br>SELECT @A_Median = AVG(DISTINCT Field_1)FROM<br>(SELECT F1.Field_1 FROM Table F1, Table F2<br>GROUP BY F1.Field_ID, F1.Field_1<br>HAVING SUM(CASE WHEN F2.Field_1 = F1.Field_1 THEN 1 ELSE 0 END)<br>&gt;= ABS(SUM(CASE WHEN F2.Field_1 &lt; F1.Field_1 THEN 1<br>WHEN F2.Field_1 &gt; F1.Field_1 THEN –1<br>ELSE 0 END )))<br>AS Median<br>-----------------------------------------------------<br><br>My aim is a single SQL to retrieve the Median of multiple<br>filds.<br><br>Thanks in advance<br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top