Skip to content
Advertisement

Sum then Divide in a Group by Clause in a VIEW

So basically after creating a view with a case statement. I want to add a group by clause in the current view below. how do i do so? the group by statement example and case statements example are below

View with case statement which is working :

I want to use group by to group the rows based on the [roomcode], [datetime] 30-minute interval and sum([occupancy])/3.

I ran my create view with the group by clause code below and it returns an error:

Cannot use an aggregate or a subquery in an expression used for the group by list of GROUP BY clause

an example of the group by

UPDATE

This is how the view is right now :

the problem right now is since rows are grouped based on columns, those which have 2 rows originally with the occupancy value of ‘1’ and ‘0’ and when combined have an average of 0.5 which sql server sees it as 0.

Here is an example:

Raw data

raw data

Data after running VIEW:

enter image description here

What it is supposed to be :

enter image description here

Advertisement

Answer

Based on the comments you can try:

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement