SQL SUM聚合问题

时间:2014-01-24 12:09:39

标签: sql

ID Title Qty

1  BMW   2

2  VW    3

我想在一个SQL查询中查找数量之和并将其除以当前数量。 例如:

SUM (Qty) = 5,所以我想按公式计算分数。 2/5和3/5

ID title newscores
1  BMW   2/5
1  vW    2/5

4 个答案:

答案 0 :(得分:3)

Demo

 SELECT id
,title
,qty/(select sum(qty) from cars) 
FROM cars 
GROUP BY id

答案 1 :(得分:0)

这应该有效

select title , sum (QTY) , t2.total, sum(QTY) / t2.total
from table , (select sum(QTY) as total from table) as t2
group by title

答案 2 :(得分:0)

select id, title, qty/(select sum(qty) from make)
from make

请参阅SQLFiddle

请注意,group by子句不需要任何内容​​。

答案 3 :(得分:0)

SELECT ID, Title, CONCAT( Qty,  '/', (SELECT SUM( Qty) FROM table1) ) AS  'newscores'
FROM  `table1`