- SQL code
select UName, RANK() over( ltrim(sum(case Violation when '1' then 1 when '2' then 1 else 0 end)*100/count(1))+'%') as tongguofrom UQDgroup by UName
可是提示'RANK' 不是可以识别的 函数名。
我想实现根据ltrim(sum(case Violation when '1' then 1 when '2' then 1 else 0 end)*100/count(1))+'%'排名并且把名次放到查询的表里
------解决方案--------------------
- SQL code
--示例数据CREATE TABLE tb(Name varchar(10),Score decimal(10,2))INSERT tb SELECT 'aa',99UNION ALL SELECT 'bb',56UNION ALL SELECT 'cc',56UNION ALL SELECT 'dd',77UNION ALL SELECT 'ee',78UNION ALL SELECT 'ff',76UNION ALL SELECT 'gg',78UNION ALL SELECT 'ff',50GO--1. 名次生成方式1,Score重复时合并名次SELECT *,Place=(SELECT COUNT(DISTINCT Score) FROM tb WHERE Score>=a.Score)FROM tb aORDER BY Place/*--结果Name Score Place ---------------- ----------------- ----------- aa 99.00 1ee 78.00 2gg 78.00 2dd 77.00 3ff 76.00 4bb 56.00 5cc 56.00 5ff 50.00 6--*/--2. 名次生成方式2,Score重复时保留名次空缺SELECT *,Place=(SELECT COUNT(Score) FROM tb WHERE Score>a.Score)+1FROM tb aORDER BY Place/*--结果Name Score Place --------------- ----------------- ----------- aa 99.00 1ee 78.00 2gg 78.00 2dd 77.00 4ff 76.00 5bb 56.00 6cc 56.00 6ff 50.00 8--*/
------解决方案--------------------
- SQL code
SELECT UName , RANK() OVER ( ORDER BY LTRIM(SUM(CASE Violation WHEN '1' THEN 1 WHEN '2' THEN 1 ELSE 0 END) * 100 / COUNT(1)) + '%' ) AS tongguoFROM UQDGROUP BY UName
------解决方案--------------------