Solved

Median with Union

Posted on 2014-02-22
2
199 Views
Last Modified: 2014-02-24
Experts,
I have a query that looks like the code below.
I would like to calculate the Median of Price in addition to the Max, Min and Avg.
How would I go about doing that with a query of this form?

Thanks in advance.

SELECT          a.productid
            , Count(a.productID) as Cnnt
            , Max(a.Price) as 'MaxPrice'
            , MIN(a.Price) as 'MinPrice'
            , CONVERT(decimal(11,0),CAST(Avg(a.Price) as decimal) ) as 'AvgPrice'
            -- Want to have Median price here

FROM
      (

      (
      select  productid, [End Price] as 'Price'
      from Table1
      where productid is not null
      )
      
      UNION ALL
      
      (
      select  productid, [Sale Amount] as 'Price'
      from Table2
      where productid is not null
      )

      ) as a
0
Comment
Question by:bobinorlando
[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 35

Accepted Solution

by:
David Todd earned 500 total points
ID: 39880305
Hi,

This doesn't look particularly simple or easy, but this post should help
http://www.sqlperformance.com/2012/08/t-sql-queries/median

HTH
  David
0
 
LVL 1

Author Comment

by:bobinorlando
ID: 39884000
Thanks I've seen that page and some on MSDN.
However, I now see that page links to a 2014 update that gave me a nice solution for SQL Server 2012.

http://www.sqlperformance.com/2014/02/t-sql-queries/grouped-median
0

Featured Post

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

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

Suggested Solutions

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

751 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