问题
I have written a series of nested procedures which perform some pay calculations for employees. Throughout these procedures data is stored in temp tables.
Now the issue is some unexpected behaviour when I perform a GROUP BY on one of these temp tables. You can see an example of that step here: http://www.sqlfiddle.com/#!9/f4095/2
The expected behaviour is for that GROUP BY to output a list of employeeid
and the corresponding aggregations. The sqlfiddle works as expected. However, what is happening is that I am getting a single row in the output with a blank employeeid
and aggregated values for all rows. However, if I filter the data in a previous step to include a single unique employeeid
, then I get a single row in the output (as expected) and with the employeeid
column containing a value instead of being blank.
Initially, I thought this could be a memory issue since I am using lots of temporary tables (my understanding is that these are stored in memory) and wasn't dropping them. So I modified all of the procedures to drop any temporary tables as soon as they were no longer required. I am still facing the same issue. My tables are about 2000 rows and around 100 columns and the output of the GROUP BY should only be about 300 rows and 7 columns.
I'm going to continue thinking of different ways I can test this and will update if I find anything, but my question is: Does anyone know of anything that could potentially cause this type of behaviour?
EDIT: I've provided a simplified version in the sqlfiddle above, but here is the statement causing the problem (the table temp_rawpaycalc
which is being GROUPED contains shift_id
, employeeid
, and all of the columns which are being aggregated. employeeid
is in the SELECT
and the GROUP BY
, and shift_id
is excluded from both.
DROP TEMPORARY TABLE IF EXISTS temp_topupaggr;
CREATE TEMPORARY TABLE temp_topupaggr
SELECT employeeid,
SUM(hoursworked) AS TotalHours,
SUM(minguaranteepayamount) AS TotalMinGuarantee,
(
SUM(baseWagePayAmount) +
SUM(basicAppPayAmount) +
...
SUM(suppPayAmount)) AS TotalEarnings,
SUM(reported_hoursworked) AS reported_TotalHours,
SUM(reported_minGuaranteePayAmount) AS reported_TotalMinGuarantee,
(
SUM(reported_baseWagePayAmount) +
SUM(reported_basicAppPayAmount) +
...
SUM(reported_suppPayAmount)) AS reported_TotalEarnings
FROM temp_rawpaycalc
GROUP BY employeeid;
回答1:
Stored procedure parameters seem to get higher priority than tables' field names inside stored procedures; if there are ambiguous identifiers for field names and parameters in queries used in stored procedures, it can cause hard to identify problems.
Solution: always qualify such field names (good practice anyway) and/or do not use identifiers for parameters on stored procedures that might conflict with field names in tables the procedure references (also a good practice for general readability even if field names are always qualified).
来源:https://stackoverflow.com/questions/47479443/mysql-group-by-not-working-as-expected