我有9个字段的表“ datakota”,然后我进行查询以选择所有字段的最大值,最小值。但是结果在所有行中都显示了“ 1”值。如果我错了,请帮助我。
|id| |kota_kab| |ipm| |pddmiskin| |ginirasio| |rasio_k| |indks_pend| |indks_kes| |ppk| |tpt| |tpak|
SELECT ipm/MAX(ipm) AS maxipm,
pddmiskin/MIN(pddmiskin) AS minpddmiskin,
ginirasio/MIN(ginirasio) AS minginirasio,
rasio_k/MIN(rasio_k) AS minrasio_k,
indks_pend/MIN(indks_pend) AS minpend,
indks_kes/MAX(indks_kes) AS maxkes,
ppk/MAX(ppk) AS maxppk,
tpt/MIN(tpt) AS mintpt,
tpak/MAX(tpak) AS maxtpak
FROM datakota
答案 0 :(得分:1)
在MySQL 5.7和更低版本中,您将需要在一个单独的子查询中进行所有聚合,然后逐行进行划分:
SELECT ipm/maxipm,
pddmiskin/minpddmiskin,
ginirasio/minginirasio,
rasio_k/minrasio_k,
indks_pend/minpend,
indks_kes/maxkes,
ppk/maxppk,
tpt/mintpt,
tpak/maxtpak
FROM datakota d
JOIN (SELECT MAX(ipm) AS maxipm,
MIN(pddmiskin) AS minpddmiskin,
MIN(ginirasio) AS minginirasio,
MIN(rasio_k) AS minrasio_k,
MIN(indks_pend) AS minpend,
MAX(indks_kes) AS maxkes,
MAX(ppk) AS maxppk,
MIN(tpt) AS mintpt,
MAX(tpak) AS maxtpak
FROM datakota) minmax
在MySQL 8.0+中,您可以使用窗口函数来简化查询:
SELECT ipm/(MAX(ipm) OVER ()) AS maxipm,
pddmiskin/(MIN(pddmiskin) OVER ()) AS minpddmiskin,
ginirasio/(MIN(ginirasio) OVER ()) AS minginirasio,
rasio_k/(MIN(rasio_k) OVER ()) AS minrasio_k,
indks_pend/(MIN(indks_pend) OVER ()) AS minpend,
indks_kes/(MAX(indks_kes) OVER ()) AS maxkes,
ppk/(MAX(ppk) OVER ()) AS maxppk,
tpt/(MIN(tpt) OVER ()) AS mintpt,
tpak/(MAX(tpak) OVER ()) AS maxtpak
FROM datakota