有什么工作方案可以为我的案件选择最少的号码?

时间:2019-04-23 18:31:51

标签: sql sql-server

我目前在选择最低分数(或分数,如果有类似分数)并用5列进行布局时遇到问题。我试图给我两个错误,但我不确定我的布局是否错误或是否需要对它们进行分组!

到目前为止,我尝试通过仅使用“ min”(仍然列出所有百分比分数)(而不是选择最低分数)并使用“ asc”选项来选择最低数字,这给了我一个错误。

一个选项:


    select name, minor, [Course Name], [Courses Taken t].Department, [percent score] , GPA
    from [Student Information t], [Courses Taken t], [Courses t]
    where name like 'Insert Name' 
    and [Student Information t].studentID=[Courses Taken t].studentID 
    and [Student Information t].Minor is not null
    group by [Percent Score] order [Percent Score] ASC limit 1

结果:

Error: Msg 102, Level 15, State 1, Line 6
Incorrect syntax near 'Percent Score'.

另一个选择:

select name, minor, [Course Name], [Courses Taken t].Department, min([percent score]) as [percent score], GPA
from [Student Information t], [Courses Taken t], [Courses t]
where name like 'Insert Name' 
and [Student Information t].studentID=[Courses Taken t].studentID 
and [Student Information t].Minor is not null
group by name, minor, [Course Name], [Courses Taken t].Department, GPA
Result: listed all percent scores (instead of the lowest)

我希望结果能给我最少的数字。在这种情况下,有两个相同的数字,因此我希望将它们都列出,但是我不确定自己做错了什么!任何提示将不胜感激<3

1 个答案:

答案 0 :(得分:2)

我的猜测是您想要的查询,但是其中有一些注释:

SELECT TOP 1
       SI.[name], --Guessed table
       SI.minor, --Guessed table
       CT.Department,
       C.[percent score], --Guessed table
       C.GPA --Guessed tables
FROM [Student Information t] SI
     JOIN [Courses Taken t] CT ON SI.studentID = CT.studentID
     CROSS JOIN [Courses t] C --Should this be a CROSS JOIN? You had no implicit JOIN in your WHERE
WHERE SI.[name] = 'Insert Name'
--Removed GROUP BY, this query has no aggregation
ORDER BY C.[percent score]; --ORDER BY, not ORDER

基本上,这就是您应该编写第一个查询的方式。希望,即使这不能给您想要的结果,也可以使您足够接近。