如何获得每年 SQLite 的 x 个最高结果
How to get x top results for each year SQLite
我的解决方案有问题,我找到了这个例子:
(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
)
"Result: near "(": syntax error
At line 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)
near "WHERE": syntax error
有人可以解释一下为什么吗?
编辑
这是数据。
| 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
此语法应该有效:
select *
from (
select *
from mytable
where `group` = 1
order by age desc
LIMIT 2
)
UNION ALL
select *
from (
select *
from mytable
where `group` = 2
order by age desc
LIMIT 2
);
但你也可以用 ROW_NUMBER():
select t.`group`, t.age, ....<rest of the columns from mytable>
from (
select *, row_number() over (partition by `group` order by age desc) rn
from mytable
where `group` in (1, 2)
) t
where t.rn <= 2
我的解决方案有问题,我找到了这个例子:
(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
)
"Result: near "(": syntax error
At line 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)
near "WHERE": syntax error
有人可以解释一下为什么吗?
编辑
这是数据。
| 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
此语法应该有效:
select *
from (
select *
from mytable
where `group` = 1
order by age desc
LIMIT 2
)
UNION ALL
select *
from (
select *
from mytable
where `group` = 2
order by age desc
LIMIT 2
);
但你也可以用 ROW_NUMBER():
select t.`group`, t.age, ....<rest of the columns from mytable>
from (
select *, row_number() over (partition by `group` order by age desc) rn
from mytable
where `group` in (1, 2)
) t
where t.rn <= 2