0

我有下表:

TKG   hour  CAP SWITCH  CALLS
AAA   7     45  HH      56
AAA   8     35  HH      76
AAA   9     25  HH      43
BBB   7     32  LL      5
BBB   8     43  LL      65
BBB   9     434 LL      65
CCC   7     54  JJ      43
CCC   8     564 JJ      43
CCC   9     54  JJ      65
ddd   7     10  MM      4
ddd   8     10  MM      3
ddd   9     10  MM      5

我需要使用最大 CAP 通过 TKG 订购,因此输出将如下所示:

TKG hour    CAP SWITCH  CALLS
AAA 7       45  HH      56
BBB 9       434 LL      65
CCC 8       564 JJ      43
ddd 7       10  MM      4

我怎样才能做到这一点?

4

5 回答 5

4

如果您使用的是 Sql server 2005+,那么您可以使用row_number()andpartition by TKGorder by cap desc

select TKG, hour, CAP, switch, Calls
from 
(
  select *,
    row_number() over(partition by TKG order by cap desc) rn
  from yourtable 
) t1
where rn = 1

请参阅带有演示的 SQL Fiddle

结果:

| TKG | HOUR | CAP | SWITCH | CALLS |
-------------------------------------
| AAA |    7 |  45 |     HH |    56 |
| BBB |    9 | 434 |     LL |    65 |
| CCC |    8 | 564 |     JJ |    43 |
| ddd |    7 |  10 |     MM |     4 |
于 2012-12-21T12:01:24.973 回答
1

这个在甲骨文中工作

  select TKG,hour,CAP,SWITCH,CALLS from (select a1.*,rank over (partition  by TKG, order by CAP) rnk 
    from table a1) where rnk=1;
于 2012-12-21T11:56:38.380 回答
0

试试这个

 select * from table 
 where  cap=(select max(cap) from table group by tkg)
 group by tkg

或者

select top 1 * from table
 order by cap desc

或者

 select * from table
 order by cap desc
  limit (1,1)
于 2012-12-21T11:54:21.720 回答
0
Select a.* from Tab a
join
(
Select TKG,max(CAP) as CAP from Tab Group by TKG
) b on a.TKG=b.TKG and a.CAP=b.CAP
于 2012-12-21T11:58:25.763 回答
0

获取最大值并进入子查询并在 CAP val Other 的内部连接中链接到此

SELECT TKG, hour,  CAP, SWITCH,  CALLS
INNER JOIN --GET THE MAX CAP FOR EACH TKG
(SELECT TKG, MAX(CAP) AS MaxCAP
FROM myTable
GROUP BY TKG
) AS MaxCap
ON MyTable.Cap= MaxCap.MaxCAP
于 2012-12-21T12:12:02.060 回答