Wrong result using sum at MySQL -
i have view lot of data. can got results using subqueries (data ok , optimized):
+------------+ | attendance | +------------+ | 319 | | 102 | | 598 | | 113 | | 6 | | 279 | | 366 | | 146 | | 669 | | 205 | | 123 | +------------+
the next time user update data, shows this:
+------------+ | attendance | +------------+ | 319 | | 102 | | 598 | | 113 | | 7 | | 279 | | 253 | | 146 | | 669 | | 561 | | 123 | +------------+
which ok, 'cause user update information 1 before has 6 attendance.
but problem comes when use data temptable , make:
select sum(attendance) total ( /* subquery returning above table */)
cause returns (in first place 1 user having 6 attendance):
+-------+ | total | +-------+ | 3169 | +-------+
and 7:
+-------+ | total | +-------+ | 3128 | +-------+
when should 3170!!!
ideas?
edit 1: pasting full query.
select sum(att_member) total (select attendance att_member (select id_branch_channel, id_member, attendance, timestamp, id_event view_event_attendance id_event = 782 order timestamp desc) temptable group id_member) total_attendance_temp
edit 2: pasting query got here
select last value using group @ mysql
here schema of view.
lets disect query, shall we?
i assume view_event_attendance has 1 record every attendee (member) attended @ event. id_event fk event, id_member fk attendee. inner select gives ordered list of members attended event #782
select id_branch_channel, id_member, attendance, timestamp, id_event view_event_attendance id_event = 782 order timestamp desc
so far, hoopy. wrap query in one:
select attendance att_member (subquery) group id_member
in sql dialetcs, syntax error. mysql allows this, result not looking for. attendance column every id_member attended in said event. might expect sum of attendances, don't in question. in case, every selected field should either in group by
clause or using aggregate function, such as
select sum(attendance) att_member (subquery) group id_member
or
select attendance att_member (subquery) group id_member, attendance
having said, don't see need use subquery begin with. let's assume wanted sum above, reprase single sql query:
select sum(attendance) att_member view_event_attendance id_event = 782 group id_member
if wanted total, leave out group by
clause, leaving this:
select sum(attendance) att_member view_event_attendance id_event = 782
if doesn't work expected, please describe in more detail you're storing in view_event_attendance, , want second query calculate.
Comments
Post a Comment