我有这样的sql代码,当我使用GROUP BY执行代码时,它仅显示记录编号1,并且不使用ABS函数。有人可以帮我吗?

SELECT *, `tblLifeAgency`.`AgencyName`, `mlp`.`Basic40`, `mlp`.`ADB40`, `mlp`.`CiAccel40`, `mlp`.`Basic50`, `mlp`.`ADB50`, `mlp`.`CiAccel50`, `mlp`.`Basic60`, `mlp`.`ADB60`, `mlp`.`CiAccel60`
FROM (`tblPackage`)
INNER JOIN `tblMatrixLifePackage` mlp ON `mlp`.`PackageID` = `tblPackage`.`PackageID`
INNER JOIN `tblCompany` ON `tblPackage`.`CompanyID` = `tblCompany`.`CompanyID`
INNER JOIN `tblLifeAgency` ON `tblLifeAgency`.`CompanyID` = `tblCompany`.`CompanyID`
AND `tblPackage`.`IsActive` =  '1'
WHERE `tblPackage`.`PackageType` =  '2'
GROUP BY tblPackage.CompanyID
ORDER BY abs(tblPackage.TotalPremi - 250000)

最佳答案

GROUP BY将您的记录分组。如果具有记录值,则必须决定将哪个结果显示为一行的摘要。例如,您可以将一组中的所有值作为正数(ABS)求和(SUM)。然后尝试将此模式转换为您的案例(以更好地理解):

SELECT
  `group`,
  ABS(
    SUM(`value`)) AS `sum`
FROM my_table
GROUP BY `group`

09-11 20:21