Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

calculate Median sql 2012

Posted on 2014-02-06
2
Medium Priority
?
502 Views
Last Modified: 2014-02-20
I need to calculate the Median of quotes based on BusinessUnit, and can't figure out why the following  code are not working:

;with QuoteCount as
(
SELECT
BusinessUnit,    
FullName,
CAST(DATEDIFF(dd, 0,  CreatedOn) AS datetime) as CreatedOn,
Count( QuoteId) as Quotes
FROM        
      quote  
WHERE
       BusinessUnit IN (@BusinessUnit)
      AND (CAST(DATEDIFF(dd, 0, CreatedOn) AS datetime) BETWEEN @StartDate AND @EndDate)
GROUP BY
      BusinessUnit,
      FullName,
      CAST(DATEDIFF(dd, 0,  CreatedOn) AS datetime)
)

,Median_all as
(
SELECT DISTINCT BusinessUnit, Quotes,
    PERCENTILE_CONT(0.5)
        WITHIN GROUP (ORDER BY Quotes)
        OVER (PARTITION BY BusinessUnit) AS MedianContAll
FROM QuoteCount
WHERE @BusinessUnit= null
ORDER BY BusinessUnit DESC
)

,Median as
(
SELECT DISTINCT  BusinessUnit, Quotes,
    PERCENTILE_CONT(0.5)
        WITHIN GROUP (ORDER BY Quotes)
        OVER (PARTITION BY BusinessUnit) AS MedianCont
FROM QuoteCount
WHERE BusinessUnit in (@BusinessUnit)
ORDER BY BusinessUnit DESC
)
select
qc.BusinessUnit,
qc.fullname,
qc.CreatedOn,
sum(qc.Quotes) as Quotes,
max(m.MedianCont) as MedianCont,
max(ma.MedianContAll ) as MedianContAll
from
      Median_all ma  
         inner join Median m  on m.BusinessUnit=ma.BusinessUnit
      inner join QuoteCount qc on ma.BusinessUnit=qc.BusinessUnit
group by
qc.BusinessUnit,
qc.fullname,
qc.CreatedOn
order by
qc.BusinessUnit,
qc.fullname,
qc.CreatedOn

Thank you in advance.
0
Comment
Question by:sharon2011
2 Comments
 
LVL 60

Accepted Solution

by:
Kevin Cross earned 2000 total points
ID: 39839619
Probably because of the "@BusinessUnit = NULL" - if you are partitioning by BusinessUnit, you do not need two CTEs.  If your intent is to get the median for everything when @BusinessUnit is NULL, you still can do this with one query.  Can you provide a table creation script and some sample data along with expected results.

In the interim, here is an example of how the median works, explaining the difference between the two new functions in SQL 2012:
http://nobrainerlab.com/2013/05/17/statistical-median-with-t-sql-2012/
http://technet.microsoft.com/en-us/library/hh231473.aspx
0
 

Author Comment

by:sharon2011
ID: 39874562
Kevin Cross,

Sorry for the delayed response. I removed the @BusinessUnit = Null. Instead Union all the query with all BusinessUnit. Then use the following code:

, ALLBUROWNum as
(
select grp,  cnt, ROW_NUMBER() over (order by  cnt) as n
from AllBUWeightCount

)

,findALLBUMedian as
(
select ceiling(max(n)/2.0) as n
from ALLBUROWNum
union all
select ceiling(max(n)/2.0)+1 as n
from ALLBUROWNum
having max(n) % 2 = 0
)

, AllBUMedian as
(
SELECT C.grp, AVG(1. * c.cnt) AS AllBUMedian
FROM ALLBUROWNum AS C
INNER JOIN findALLBUMedian AS f
on C.n = f.n
GROUP BY C.grp
)


Thank you so much for your immediate assistance.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

877 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