MYSQL根据同一个表中的行划分列值

时间:2016-09-16 06:10:01

标签: mysql

我有这样的表......

+-----------+------------+---------------+--------+
| ID        |  Name      | NILAI INDEKS  | SKS    |
+-----------+------------+---------------+--------+
|         3 | abc        | 3             |     4  |
|         3 | abc        | 2             |     2  |
|         3 | abc        | 3             |     4  |
|         4 | def        | 2             |     2  |
|         4 | def        | 3             |     4  |
|         4 | def        | 2             |     2  |
+-----------+------------+---------------+--------+

我想要这样的结果,任何建议都将不胜感激。感谢。

+-----------+------------+--------------+
| ID        |  Name      | IPS          | 
+-----------+------------+--------------|
|         3 | abc        | 1,25         |
|         4 | def        | 1,14         |
+-----------+------------+--------------+

IPS =来自sks的SUM除以来自Nilai Index的SUM

示例: IPS abc =(4 + 2 + 4)/(3 + 2 + 3)= 10/8 = 1,25

1 个答案:

答案 0 :(得分:0)

我找到了答案,

SELECT
    id,name,
        SUM(sks * nilai)/SUM(sks) AS IPS
FROM
    nilai
GROUP BY id
ORDER BY name