获得一组的第一行 [英] Get first row for one group

查看:87
本文介绍了获得一组的第一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为每个GV(TeacherID)获取第一行

I want to get the first row for every GV(TeacherID)

GV |   Class|  SUM 
GV1|   L001 |  5000 
GV1|   L002 |  5000 
GV1|   L003 |  5000 
GV2|   L002 |  7000 
GV2|   L003 |  7000 
GV2|   L001 |  7000 
GV3|   L001 |  8000 
GV3|   L002 |  8000 
GV3|   L003 |  8000 

救救我.

推荐答案

好的,现在您对问题进行了编辑,以使该答案看起来完全不相关...帮助您入门.干杯.

ok, now you've edited the question such that this answer looks completely irrelevant...sigh...I'll leave it in case it helps you get started. Cheers.

根据您的规格,最简单的解决方案:

The simplest solution given your specs:

  select teacherid 
    from mytable 
group by teacherid;

如果除了teacherid之外还需要其他信息:

If you need other information in addition to the teacherid:

  select teacherid, ...other cols...
    from (select teacherid, ...other cols...
                 row_number() over (
                     partition by teacherid
                     order by classid /* or class as per edit */) as row_num
            from mytable) my_derived_table
   where my_derived_table.row_num = 1;

告白者:我没有方便进行测试的SQL-Server安装,因此语法可能并不完全正确.但很近.

Caveat emptor: I don't have an installation of SQL-Server handy to test on, so syntax may not be exactly correct; but it's close.

这篇关于获得一组的第一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆