Finding The Median Value From A Table, Group By Date Sqlserver
I have a complicated problem I am trying to solve. Please bear with me and feel free to ask any questions. I am quite new to SQL and having difficulty with this... I need to count
Solution 1:
How about something like this? (Only apply if you use SQL Server 2012 or above)
SELECTDISTINCT ForDate, PERCENTILE_CONT(0.5) WITHINGROUP (ORDERBY TicketCount) OVER (PARTITIONBY ForDate) AS Median
FROM #z;
In short, SQL-Server has two ways to calculate median, you can read about it here: https://msdn.microsoft.com/en-us/library/hh231327.aspx
You can compare them both in this case with the code here:
SELECTDISTINCT
ForDate
, PERCENTILE_DISC(0.5) WITHINGROUP (ORDERBY TicketCount) OVER (PARTITIONBY ForDate) AS MedianDisc
, PERCENTILE_CONT(0.5) WITHINGROUP (ORDERBY TicketCount) OVER (PARTITIONBY ForDate) AS MedianCont
FROM
#z;
Post a Comment for "Finding The Median Value From A Table, Group By Date Sqlserver"