Article View: bit.databases.mssql-l
Article #219Re: question
From: John Gilson
Date: Sun, 31 Aug 2003 11:44
Date: Sun, 31 Aug 2003 11:44
68 lines
3573 bytes
3573 bytes
"ricksql@yahoo.com" <u50415039@spawnkill.ip-mobilphone.net> wrote in message
news:l.1062309783.1757537841@host-66-81-126-37.rev.o1.com...
> #temptable got order,fname and age.
> trying to find two high maxes per each order.
> query returns (1) but (2) is correct answer.
> supposedly, max(age2) < max(age1).
> ****
> select d.order,case d.t when 1 then s.fname else null end as fname,
> case d.t when 1 then s.age else null end as age1,
> case d.t when 2 then s.fname else null end as fname,
> case d.t when 2 then s.age else null end as age2
> from #temptable s
> join ( select order, max(age) age, 1 t
> from #temptable group by order
> union all select order, max(age) age, 2 t
> from #temptable group by order) d on d.order = s.order
> and s.age = d.age
> group by d.order, d.t,s.fname,s.age
> ****
>
> (1)
> 1 mark1 26
> 1 mark1 26
> 2 fred1 19
> 2 fred1 19
>
>
> (2)
> 1 marke1 26 mark2 24
> 2 fred1 19 fred2 17
>
>
>
>
>
>
> --
> Sent by ricksql from yahoo in area com
> This is a spam protected message. Please answer with reference header.
> Posted via http://www.usenet-replayer.com/cgi/content/new
Posting DDL statements (CREATE TABLE statements), sample data
(in the form of INSERT statements), and the expected query result are
all important accompaniments when posing a question.
This is offered untested.
SELECT T1."order", T1.fname AS fname1, T1.age AS age1,
T3.fname AS fname2, T3.age AS age2
FROM #temptable AS T1
LEFT OUTER JOIN
#temptable AS T2
ON T1."order" = T2."order" AND
T2.age > T1.age
LEFT OUTER JOIN
#temptable AS T3
ON T3."order" = T1."order" AND
T3.age < T1.age AND
NOT EXISTS (SELECT *
FROM #temptable AS T4
WHERE T4."order" = T1."order" AND
T4.age > T3.age AND
T4.age < T1.age)
WHERE T2.age IS NULL
ORDER BY T1."order", T1.age, T1.fname
Regards,
jag
Technical Details
Message-ID:
<4cl4b.60112$ev.13538296@twister.nyc.rr.com>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!feed1.newsreader.com!newsreader.com!news3.optonline.net!cyclone.rdc-nyc.rr.com!news-out.nyc.rr.com!twister.nyc.rr.com.POSTED!not-for-mailReferences:
<l.1062309783.1757537841@host-66-81-126-37.rev.o1.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 104de1,dccbf7583e9eb21c X-Google-Attributes: gid104de1,public X-Google-Thread: f6611,dccbf7583e9eb21c X-Google-Attributes: gidf6611,public X-Google-Thread: 109365,dccbf7583e9eb21c X-Google-Attributes: gid109365,public X-Google-ArrivalTime: 2003-08-31 04:49:35 PST Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!feed1.newsreader.com!newsreader.com!news3.optonline.net!cyclone.rdc-nyc.rr.com!news-out.nyc.rr.com!twister.nyc.rr.com.POSTED!not-for-mail Reply-To: "John Gilson" <jag@acm.org> From: "John Gilson" <jag@acm.org> Newsgroups: bit.databases.mssql-l,comp.databases.ms-sqlserver,microsoft.public.sqlserver.mseq References: <l.1062309783.1757537841@host-66-81-126-37.rev.o1.com> Subject: Re: question Lines: 70 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1158 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165 Message-ID: <4cl4b.60112$ev.13538296@twister.nyc.rr.com> Date: Sun, 31 Aug 2003 11:44:00 GMT NNTP-Posting-Host: 66.108.53.81 X-Complaints-To: abuse@rr.com X-Trace: twister.nyc.rr.com 1062330240 66.108.53.81 (Sun, 31 Aug 2003 07:44:00 EDT) NNTP-Posting-Date: Sun, 31 Aug 2003 07:44:00 EDT Organization: Road Runner - NYC