Mysql
 sql >> Datenbank >  >> RDS >> Mysql

SQL-Auswahlabfrage mit Verknüpfungen, Gruppieren nach und Aggregatfunktionen

Dies ist nicht möglich WHERE inc_amount=max(inc_amount) Verwenden Sie in der where-Klausel entweder HAVING oder tun Sie es im Join-Zustand, versuchen Sie stattdessen Folgendes:

SELECT 
  e.emp_id, 
  e.inc_date,
  t.TotalInc, 
  t.MaxIncAmount
FROM salary_increase AS i
INNER JOIN emp_table AS e ON i.emp_id=e.emp_id
INNER JOIN
(
   SELECT 
     emp_id,
     MAX(inc_amount)     AS MaxIncAmount, 
     COUNT(i.inc_amount) AS TotalInc
   FROM salary_increase
   GROUP BY emp_id
) AS t ON e.emp_id = t.emp_id AND e.inc_amount = t.MaxIncAmount;