我想获得每个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
帮助我。
答案 0 :(得分:2)
编辑:好的,现在你已经编辑了这个问题,这个答案看起来完全不相关......叹息......我会留下它以防万一它可以帮助你开始。欢呼声。
根据您的规格提供最简单的解决方案:
select teacherid
from mytable
group by teacherid;
除了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进行测试,因此语法可能不完全正确;但它很接近。
答案 1 :(得分:1)
WITH T AS (
SELECT yourTable.*, ROW_NUMBER() OVER(PARTITION BY TeacherID
ORDER BY ClassID) AS RN
FROM yourTable
)
SELECT *
FROM T
WHERE RN = 1
答案 2 :(得分:1)
<强> DDL 强>
create table #t
(
GV varchar(4),
Class varchar(4),
[SUM] int
)
示例记录
insert into #t(GV, Class, [SUM])values('GV1', 'L001', 5000)
insert into #t(GV, Class, [SUM])values('GV1', 'L002', 5000)
insert into #t(GV, Class, [SUM])values('GV1', 'L003', 5000)
insert into #t(GV, Class, [SUM])values('GV2', 'L002', 7000)
insert into #t(GV, Class, [SUM])values('GV2', 'L003', 7000)
insert into #t(GV, Class, [SUM])values('GV2', 'L001', 7000)
insert into #t(GV, Class, [SUM])values('GV3', 'L001', 8000)
insert into #t(GV, Class, [SUM])values('GV3', 'L002', 8000)
insert into #t(GV, Class, [SUM])values('GV3', 'L003', 8000)
<强>查询强>
Select GV, class, [sum] from
(
Select Row_Number() Over(Partition by GV Order by GV ) as RowId, * from #t
)K
Where RowId = 1
drop table #t
<强>结果集强>
GV class sum
---- ----- ----
GV1 L001 5000
GV2 L002 7000
GV3 L001 8000