hiveql查询选择classid,studname,table1的studname,classid,studname classID;

发布于 2025-01-19 16:18:52 字数 1082 浏览 1 评论 0原文

很抱歉在主题上写了一个明显的不良查询,但它准确地描述了我想要的结果。

我有一个带有classid int,studname string,grade int的表。我需要一个结果,列出了每个课程中获得最高成绩的每个类别的studname。多个学生可以达到成绩,每个学生应首先按ClassID列出,然后由StudName列出。

样本输出: | classid | StudName | | ---------- | ----------- | | 101 |玛丽| | 101 | Nate | | 101 |克里斯| | 102 |本杰明| | 103 | Nate | | 103 |汤姆| etc...

my first guess is:

SELECT classID, studName from table1 where grade = MAX(grade) group by classID, studName;

but this gives an error: ... not yet supported place for UDAF 'max'

I have also tried creating a view:

CREATE VIEW newView as select classID, MAX(grade) from table1 group by classID;

and using it in a subquery in the where statement:

select classID, studName from table1 where grade IN (select * from newView) group by classID, studName;

but it seems that : "SubQuery can contain only 1 item in SELECT List"

I have poured over "Apache Hive Essentials" book with no luck either.

我是Hiveql的新手,这让我在晚上起床。任何帮助将不胜感激。

谢谢

Sorry for writing an obvious bad query in subject but it accurately describes the result I am looking for.

I have a table with classID int, studName string, grade int. I need a result that lists each classID, studName that achieved the highest grade in each course. multiple students can achieve the grade and each should be listed in descending order first by classID then by studName.

sample output:
| classID | studName|
|---------|---------|
| 101 | Mary |
| 101 |Nate |
| 101 |Chris |
| 102 |Benjamin |
| 103 |Nate |
|103 |Tom |
etc...

my first guess is:

SELECT classID, studName from table1 where grade = MAX(grade) group by classID, studName;

but this gives an error:
... not yet supported place for UDAF 'max'

I have also tried creating a view:

CREATE VIEW newView as select classID, MAX(grade) from table1 group by classID;

and using it in a subquery in the where statement:

select classID, studName from table1 where grade IN (select * from newView) group by classID, studName;

but it seems that :
"SubQuery can contain only 1 item in SELECT List"

I have poured over "Apache Hive Essentials" book with no luck either.

I am new to HiveQL and this one is keeping me up at night. Any help would be GREATLY appreciated.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深居我梦 2025-01-26 16:18:52

您的SQL -
您需要命名最大列,然后在子句中的子查询中使用它。
更改下面的视图。

CREATE VIEW newView as select classID, MAX(grade) as maxgrade from table1 group by classID;

更改您的SQL

select classID, studName from table1 where grade IN (select maxgrade from newView) group by classID, studName;

解决方案
但是,此SQL将通过调整您的SQL来帮助您实现“在每个主题中获得最高成绩的学生”所需的目标。

select classID, studName, grade
from table1 
join (select classID mxid, MAX(grade) as maxgrade from table1 group by classID) mx -- this subquery will pick maximum grade in each class.
ON grade =mx.maxgrade and classID = mx.mxid -- This join will select only the students with maximum grade in each class.

Issue with your SQL -
You need to name the max column and then use it in subquery IN clause.
change the view like below.

CREATE VIEW newView as select classID, MAX(grade) as maxgrade from table1 group by classID;

Change your SQL like this

select classID, studName from table1 where grade IN (select maxgrade from newView) group by classID, studName;

Solution
But this SQL will help you in achieving what you need 'find students who got max grade in each subject' by tweaking your SQL.

select classID, studName, grade
from table1 
join (select classID mxid, MAX(grade) as maxgrade from table1 group by classID) mx -- this subquery will pick maximum grade in each class.
ON grade =mx.maxgrade and classID = mx.mxid -- This join will select only the students with maximum grade in each class.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文