我有这样的sql代码,当我用GROUP BY
执行代码时,它只显示第一个记录并且它没有使用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)
答案 0 :(得分:0)
GROUP BY
将您的记录分组。如果您有记录值,则必须决定将哪个结果显示为行的摘要。例如,您可以将一个组中的所有值视为正数(ABS
)并将它们相加(SUM
)。然后尝试将此模式转换为您的案例(以便更好地理解):
SELECT
`group`,
ABS(
SUM(`value`)) AS `sum`
FROM my_table
GROUP BY `group`