我的解决方案有问题,我找到了以下示例:
(SELECT COUNT(*) FROM person AS b
WHERE b.group = a.group AND b.age >= a.age) <= 2
ORDER BY a.group ASC,a.age DESC
(来自:Get top n records for each group of grouped results)
但是我需要在一个旧列的基础上创建一个新列,所以我需要进行一些计算,当我尝试添加更多列时,我会收到一条错误消息。 如果我只是添加满足感,就可以了,例如:
(SELECT COUNT(*) FROM person AS b
WHERE b.group = a.group AND b.age*100 >= a.age*100) <= 2
ORDER BY a.group ASC,a.age DESC
但是当我尝试重命名新列时,AS过多。
我也尝试使用UNION ALL,但是我的SQLite对()不满意。这根本不适合我:
(
select *
from mytable
where `year` = 2012
order by score*100/50 AS percent desc
LIMIT 2
)
UNION ALL
(
select *
from mytable
where `year` = 2013
order by score*100/50 AS percent desc
LIMIT 2
)
“结果:靠近”(“:语法错误
在第1行:
(“
即使我在()之前执行SELECT和FROM,也收到错误消息。
select * from mytable
(where `year` = 2012
order by score*100/50 AS percent desc
LIMIT 2)
UNION ALL
select * from mytable
(where `year` = 2013
order by score*100/50 AS percent desc
LIMIT 2)
“ WHERE”附近:语法错误
可以请人解释一下为什么吗?
编辑
这里是数据。
| Person | Year | Score | +--------+-------+-------+ | Bob | 2013 | 32 | | Jill | 2012 | 34 | | Shawn | 2012 | 42 | | Jake | 2012 | 29 | | Paul | 2013 | 36 | | Laura | 2013 | 39 |
所需结果集:
| Person | Year | Percent | +--------+-------+---------+ | Shawn | 2012 | 84 | | Jill | 2012 | 68 | | Laura | 2013 | 78 | | Paul | 2013 | 72 | +--------+-------+---------+
其中百分比=得分* 100/50