Table 1: kpi_detail column[redValue, amberValue, greenValue]
Table 2: data_detail
$sql= "SELECT id,kpi_code,kpi_name,result_data,target, count_date, SUM(result_data) AS total_data, assign FROM data_detail GROUP BY kpi_code";
Expected table columns results:
id| kpi_code | kpi_name | result_data | target | count_date | total_data | assign | redValue | amberValue | greenValue |
It looks like the GROUP BY
is giving me trouble on joining two tables. How do I get the 3 columns of table1 kpi_detail
? Join the 3 columns from table 2 to table 1
You have "bare" columns in the GROUP BY
. You should be using aggregation functions for all the columns not in the GROUP BY
:
SELECT MAX(id), kpi_code, MAX(kpi_name), MAX(result_data),
MAX(target), MAX(count_date), SUM(result_data) AS total_data,
MAX(assign)
FROM data_detail
GROUP BY kpi_code
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.