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 and group by

Status
Not open for further replies.

Doc94

Technical User
Aug 1, 2003
58
US
I am trying to expand upon and already useful median query in SQL. I need to figure out where to put a GROUP BY statement so I can get the median "length of stay" grouped by a field in the table called "TSheetTitleDescription". The folowing is the median query that works flawlessly:
SELECT Avg([R1]) AS Median
FROM [

SELECT Max(Q1.R2) AS R1

FROM

(SELECT TOP 50 PERCENT LOS_min_amb_work.los AS R2

FROM LOS_min_amb_work

ORDER BY LOS_min_amb_work.los ASC) AS Q1

UNION

SELECT Min(Q2.R2) AS R1

FROM

(SELECT TOP 50 PERCENT LOS_min_amb_work.los AS R2

FROM los_min_amb_work

ORDER BY LOS_min_amb_work.los DESC) AS Q2

]. AS Q3;

Question is where to put the group by in this query. And I suspect I need to define in the query structure that I want the filed in the query.
Any help would be greatly appreciated.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top