0

如上图所示,我有一个当前正在显示的网格视图。我希望它只显示具有最大时间的记录,如图 2 所示。例如,对于 Q1 最大时间是 15 分钟,所以只显示 Q1 15min 等。这是动态的,因此字母和数字可能会发生变化。有人知道一个好的sql server 查询吗?

更新

在此处输入图像描述

4

2 回答 2

2

这对你有用吗?

Select distinct Number, MAX(Time) from MyTable group by Number

评论后:

create table #example(number varchar(50), id int, [time] nvarchar(50), descr nvarchar(50))
insert into #example (number, id, [time], descr) values ('Q1', 1, '10 Min', 'Shoe')
insert into #example (number, id, [time], descr) values ('Q1', 1, '15 Min', 'Car')
insert into #example (number, id, [time], descr) values  ('Q1', 1, '6 Min', 'Shirt')
insert into #example (number, id, [time], descr) values  ('L2', 1, '10 Min', 'Shoe')
insert into #example (number, id, [time], descr) values  ('P3', 2, ' 8 Min', 'Garage')
insert into #example (number, id, [time], descr) values  ('P3', 2, ' 3 Min', 'Plant')
insert into #example (number, id, [time], descr) values  ('P3', 2, '20 Min', 'Tree')

SELECT t1.*
FROM #example AS t1
LEFT OUTER JOIN #example AS t2
    ON (t1.number = t2.number AND t1.[time] < t2.[time])
WHERE t2.number IS NULL;
--In other words: fetch the row from t1 where no other row exists with the same Number and a greater Time.
于 2013-06-14T17:26:25.520 回答
0

我不确定这是否是您正在寻找的,但请检查一下:

create table #test (name varchar(10), number int)
insert into #test values ('a', 1)
insert into #test values ('a', 2)
insert into #test values ('a', 3)
insert into #test values ('b', 1)
insert into #test values ('b', 2)
insert into #test values ('b', 4)

select name, max(number) from #test group by name

结果:

name    (No column name)
a   3
b   4
于 2013-06-14T17:28:21.903 回答