说我有下表:
--------------------
|ID|Yes|No|Filename|
|01|15 |10|1.php |
|02|12 |5 |2.php |
|03|6 |1 |3.php |
--------------------
我想要做的是在sql select语句中应用Yes / Yes + No并回显最高值的文件名。
“是”和“否”会不时更新,因此创建另一列来处理等式会更好吗,还是可以在sql语句中使用等式?选择的陈述对此会是什么样的?
由于
答案 0 :(得分:6)
代表MySQL
,
SELECT Filename,
CAST((Yes / (Yes + No)) AS DECIMAL(10,5)) rating
FROM table1
WHERE CAST((Yes / (Yes + No)) AS DECIMAL(10,5)) =
(
SELECT MAX(rating)
FROM
(
SELECT CAST((Yes / (Yes + No)) AS DECIMAL(10,5)) rating
FROM table1
) x
)
代表SQL Server
SELECT TOP 1 WITH TIES Filename,
(Yes / (Yes + No)) rating
FROM table1
ORDER BY rating DESC
答案 1 :(得分:2)
您可以使用此方法:
SELECT TOP 1 sf.Filename
, (sf.Yes / (sf.Yes + sf.No)) AS RatioValue
FROM dbo.storeFile AS sf
ORDER BY (sf.Yes / (sf.Yes + sf.No)) DESC