RE: Distinct Max Value?

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Alejandro Mesa (AlejandroMesa_at_discussions.microsoft.com)
Date: 01/17/05


Date: Mon, 17 Jan 2005 11:37:03 -0800

Try:

declare @t table (run_id int not null, SignOffTime datetime)

insert into @t values(001, '2005-01-17T20:00:00')
insert into @t values(001, '2005-01-17T21:00:00')
insert into @t values(001, '2005-01-17T22:00:00')

select
        a.run_id,
        a.SignOffTime,
           b.MAXSignOffTime
from
        @t as a
        inner join
        (select run_id, max(SignOffTime) from @t group by run_id) as b(run_id,
MAXSignOffTime)
        on a.run_id = b.run_id
order by
        1
go

AMB

"rjcmbc" wrote:

> We have numerous instances of the same Runnumber (eg. Runnumber = 001) that
> with different SignOffTimes (eg, 8pm, 9pm, 10pm etc).
>
> I am trying to isolate the MAX SignOffTime for each Runnumber group (in this
> example - 001 = 10pm) as well as populating an additional field in a view
> called 'MaxSignOffTime' for that Runnumber group. Ultimately my view would
> show the following:
>
> Run# SignOffTime MAXSignOffTime
> 001 8pm 10pm
> 001 9pm 10pm
> 001 10pm 10pm
>
> Anyhelp is appreciated!
>
> rjcmbc



Relevant Pages

  • Re: Distinct Max Value?
    ... rjcmbc wrote: ... > I am trying to isolate the MAX SignOffTime for each Runnumber group ... > field in a view called 'MaxSignOffTime' for that Runnumber group. ...
    (microsoft.public.sqlserver.programming)
  • Distinct Max Value?
    ... I am trying to isolate the MAX SignOffTime for each Runnumber group (in this ... Run# SignOffTime MAXSignOffTime ...
    (microsoft.public.sqlserver.programming)
  • Re: Distinct Max Value?
    ... Well, again, assuming SignOffTime is datetime: ... Bob Barrows ... rjcmbc wrote: ... Please reply to the newsgroup. ...
    (microsoft.public.sqlserver.programming)
  • Re: Distinct Max Value?
    ... 'SignOffTime' field. ... >> I am trying to isolate the MAX SignOffTime for each Runnumber group ... > FROM tablename ... > quicker response by posting to the newsgroup. ...
    (microsoft.public.sqlserver.programming)