Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Median with Union

Posted on 2014-02-22
2
Medium Priority
?
204 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 2000 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

Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

715 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