是否有一个工作选项可以为我的情况选择最少的数字



我目前在选择最低百分比分数(如果有类似分数(并将其布置为 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

我的猜测是你想要的查询是这个,但是,它有一些评论:

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

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

最新更新