mysql - Calculating overall total column for each row in a table with dynamic SQL statement -
and query table show total payment based on location (which has dynamic values) every user + overall total follow: 
i able breaking locations point, thing not total column, here sql:
use fypdb; set @sql = null; select group_concat( distinct concat('sum(case when attendance.location_id=''', attendance.location_id,''' format(position.pay_rate*time_to_sec(timediff(attendance.logout_time, attendance.login_time))/3600, 2) else 0 end) `', location.name,'`') ) @sql attendance join location on attendance.location_id = location.location_id; set @sql = concat('select user.user_name,', @sql, ' sum(format(position.pay_rate*time_to_sec(timediff(attendance.logout_time, attendance.login_time))/3600, 2)) total ', #this problematic attribute 'from attendance join user on attendance.user_id = user.user_id join position on user.position_id = position.position_id group attendance.user_id'); prepare stmt @sql; execute stmt; deallocate prepare stmt; as per above, if remove problematic attribute (total) commands works fine, when try total following error:
error code: 1064. have error in sql syntax; check manual corresponds mysql server version right syntax use near 'sum(format(position.pay_rate*time_to_sec(timediff(attendance.logout_time, attend' @ line 4 can tell me doing wrong? thank much!
note: using mysql version 5.7.9
as can see use concatenation , missing space after total in problematic line. next line 'from has no space resulting query as totalfrom instead of as total from

Comments
Post a Comment