I have attendance data that is stored like this:
Building | Room | Date | Morning | Evening
------------------------------------------
BuildA A1 1 10 15
BuildA A1 2 20 35
BuildA A1 3 30 15
BuildA A2 1 60 30
BuildA A2 2 30 10
BuildA A2 3 40 20
BuildB B1 1 20 25
BuildB B1 2 10 35
BuildB B1 3 30 10
BuildB B2 1 15 25
BuildB B2 2 25 35
BuildB B2 3 25 15
I then need to see the difference in attendance for each time of day from the previous day for each room. The result would look like this:
Building | Room | Date | Morning | Evening | MorningDiff | EveningDiff
-----------------------------------------------------------------------
BuildA A1 1 10 15 0 0
BuildA A1 2 20 35 10 20
BuildA A1 3 30 15 10 -20
BuildA A2 1 60 30 0 0
BuildA A2 2 30 10 -30 -20
BuildA A2 3 40 20 10 10
BuildB B1 1 20 25 0 0
BuildB B1 2 10 35 -10 10
BuildB B1 3 30 10 20 -25
BuildB B2 1 15 25 0 0
BuildB B2 2 25 35 10 10
BuildB B2 3 25 15 0 -20
The previous I was able to accomplish with this query:
select t.*,
COALESCE((`morning` -
(select `morning`
from data t2
where t2.date < t.date
and t2.room = t.room
order by t2.date desc
limit 1 )) ,0)
as MorningDiff,
COALESCE((`evening` -
(select `evening`
from data t2
where t2.date < t.date
and t2.room = t.room
order by t2.date desc
limit 1 )) ,0)
as EveningDiff
from data t
order by room,date asc;
So now I have the difference in attendance. This is where it gets a little complicated now. Maybe first seeing what the final product I am after may clear it up:
Building1 | Room1 | TimeOfDay1 | Direction1 | Building2 | Room2 | TimeOfDay2 | Direction2 | OccuranceCount | Room1DirectionCount | Room2DirectionCount
-----------------------------------------------------------------------------------------------------------------------------------------------------
BuildA A1 Morning Up BuildA A2 Morning Up 1 2 1
BuildA A1 Morning Up BuildA A2 Morning Down 1 2 1
BuildA A1 Morning Up BuildA A2 Evening Up 1 2 1
.
.
.
The reason for getting the difference between dates is to see if the attendance increased or decreased from the previous day. We are not actually concerned with the actual number from the difference, we are just interested if it went up or it went down.
OccuranceCount field - If a room's attendance went up/down one day we are trying to see whether another rooms attendance went up/down the next day. This field is used then to count how many times room2 went up/down one day and that room1 went up/down the next day. So if we take the first row as an example it shows that room A1
morning
attendance went up 1
time when room A2
's morning
attendance went up the previous day during the 3 day period.
Room1DirectionCount/Room2DirectionCount field - These fields simply show how many time each direction occurred for each room. So if in the time period of 100 days if room A1 increased attendance 60 times the count would be 60.
Since I am comparing all the rooms to each other I have tried to do a cross join to form a cartesian product but I have been unable to figure out how to do the join properly so it references the other room's previous day.
I am not sure why this question was marked as a duplicate of a question regarding pivot tables? I don't believe this question is answered by that.