Solved

Median with Union

Posted on 2014-02-22
2
201 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

617 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