Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
子查询中的SQL ORDER BY不返回任何结果_Sql_Subquery_Sql Order By_H2_Sql In - Fatal编程技术网

子查询中的SQL ORDER BY不返回任何结果

子查询中的SQL ORDER BY不返回任何结果,sql,subquery,sql-order-by,h2,sql-in,Sql,Subquery,Sql Order By,H2,Sql In,我正在使用SQL H2数据库引擎1.4.181版,并试图总结一个学生的前5个分数。结果表包括studentID、eventID和points。每个学生只能参加一次活动。下面的子查询是我试图为id为5的学生执行此操作的子查询 SELECT SUM(points) FROM RESULTS WHERE eventID IN (SELECT TOP 5 eventID FROM RESULTS WHERE studentID = 5 ORDER B

我正在使用SQL H2数据库引擎1.4.181版,并试图总结一个学生的前5个分数。结果表包括studentID、eventID和points。每个学生只能参加一次活动。下面的子查询是我试图为id为5的学生执行此操作的子查询

SELECT SUM(points) FROM RESULTS 
    WHERE eventID IN
        (SELECT TOP 5 eventID FROM RESULTS 
             WHERE studentID = 5 ORDER BY points DESC) 
        AND studentID = 5;
但是,此查询返回null。我发现,如果删除了ORDER BY points DESC,那么查询的其余部分就可以工作了。有人知道如何通过合并订单,或者为什么它不起作用吗


谢谢

尝试使用join,您可以像这样使用sql

select sum(x.points) from 
(select points , event_id  from RESULTS) X 
(select eventID from 
(SELECT  eventID, row_number() over (partition by points ORDER BY points DESC ) tops FROM RESULTS )  X
where tops<6 ) Y 
where X.eventID=y.eventID 
and X.studentID = 5;

这看起来像H2中的一个bug。你可以加入。完整的测试用例:

create table results(eventId int, points int, studentId int);
insert into results values(1, 10, 1), (2, 20, 1), (3, 5, 1);
insert into results values(1, 10, 2), (2, 20, 2), (3, 5, 2);
insert into results values(1, 10, 3), (2, 20, 3), (3, 5, 3);

SELECT SUM(r.points) FROM RESULTS r,
(SELECT eventID FROM RESULTS 
  WHERE studentID = 2 
  ORDER BY points DESC
  LIMIT 2 ) r2
WHERE r2.eventID = r.eventId
AND studentID = 2;

结果证明我根本不需要IN查询。以下几点非常有效:

SELECT SUM(points) FROM 
    (SELECT TOP 5 points FROM RESULTS 
        WHERE studentID = 5
        ORDER BY points DESC);

您使用的是什么数据库系统,它的版本是什么?point列是否有空值?我发现:SQL语句中的语法错误SELECT SUMX.POINTS FROM SELECT POINTS,EVENTID FROM RESULTS X[*]SELECT EVENTID FROM SELECT EVENTID,ROW_NUMBER OVER PARTITION BY POINTS ORDER BY POINTS DESC TOPS FROM RESULTS X WHERE TOPS