I am trying to find the monthly median of my category EVS. To do this I need to summarize all the events of each logical device name. However, This needs to be done on a monthly basis. I have two queries the first one, which I will paste below summarizes all the events for that robot on a daily basis but I need to further summarize to make it a monthly basis. Also I need to do this on a monthly basis over multiple years.
select logicalDeviceName, Sum(Events) as consolidatedEvents, EVS, StartDate
From report.DisinfectionStatsCombined
group By LogicalDeviceName, EVS, StartDate
Order By EVS
The outcome is this
Furthermore, here is my code for attempting to calculate the median with a sample of the outcome.
select logicalDeviceName, Sum(Events) as consolidatedEvents, EVS, StartDate,
PERCENTILE_CONT(0.5) within group (order by Sum(Events))
OVER (Partition BY year(StartDate),Month(StartDate),EVS) AS MedianCont
From report.DisinfectionStatsCombined
group By LogicalDeviceName, EVS, StartDate,Events
Order By EVS
Again, I am trying to calculate the median, by summarizing all the consolidated events by each logical device name. So per month there should only be one of each name. Then, I will use this information to calculate median for each evs on a monthly basis. There is 4 evs types, I will provide a snip of another type. I feel I might have to change the grouping, in order to show the different types of evs. Ideally, I would like to only have it grouped by month/year. the dates go all the way back to 2012.
picture to show example of the other evs
Below is a sample of what I want my desired outcome to be sample outcome and data
My understanding of your goals:
My working assumption is that for any given LogicalDeviceName there is only one matching EVS. For example, when the LogicalDeviceName value is "Apollo", the EVS value will always be "Commercial".
Note that I created and populated test table to get screenshots below. Script included below.
Goal 1: Aggregate the count of events per LogicalDeviceName per month to answer questions like "how many events occurred for Apollo in January 2015?". A query like this should do the trick. Separating out the year and month parts from the day allows the month aggregate:
Goal 2: Calculate the median number of events each month for each LogicalDeviceName
Need to use DISTINCT because PARTITION BY includes a row for each record. Also note that while this syntax is easier, there are potentially more performant ways to calculate median.
Goal 3: Calculate the median number of events each month for each EVS using the "consolidated" (SUM) of events for each LogicalDeviceName
This is where I'm again a bit foggy about what you're trying to accomplish. Will update if further clarification provided. Edit below based on provided screenshot of before/after sets. Off the top of my head, CTE building off of the first query is easiest way to accomplish:
Test Table Create/Populate Script:
A little help was provided from this SO Post for generating random values.