🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: bit.databases.mssql-l
Article #220

Re: Re: question

#220
From: ricksql
Date: Sun, 31 Aug 2003 21:38
92 lines
4027 bytes
Dear John Gilson,

In your posting Re: question from Sun, 31 Aug 2003 11:44:00 GMT you
write:

>
> "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


This worked for result:
1   marke1  26   mark2   24
2   fred1   19   fred2   17

How it modify it to result three highest maxes, like:
1  marke  26  mark2  24   mark3   20
2  fred1  19  fred2  17   fred3   15


 





-- 
Spam protected message from: 
Sent by  ricksql from yahoo piece of com
Posted via http://www.usenet-replayer.com/cgi/content/new
Technical Details
Message-ID: <r.1062365895.1747711181@[63.127.215.130]>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!logbridge.uoregon.edu!newsfeed.stueberl.de!feed.news.nacamar.de!news1.ip-mobilphone.net!news2.ip-mobilphone.net!u50415039@spawnkill.ip-mobilphone.net
References: <l.1062309783.1757537841@host-66-81-126-37.rev.o1.com> <4cl4b.60112$ev.13538296@twister.nyc.rr.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 109365,dccbf7583e9eb21c
X-Google-Attributes: gid109365,public
X-Google-Thread: 104de1,dccbf7583e9eb21c
X-Google-Attributes: gid104de1,public
X-Google-Thread: f6611,dccbf7583e9eb21c
X-Google-Attributes: gidf6611,public
X-Google-ArrivalTime: 2003-08-31 14:50:26 PST
From: u50415039@spawnkill.ip-mobilphone.net  (ricksql@yahoo.com)
Sender: u50415039@spawnkill.ip-mobilphone.net
Newsgroups: bit.databases.mssql-l,comp.databases.ms-sqlserver,microsoft.public.sqlserver.mseq
Subject: Re: Re: question
Date: Sun, 31 Aug 2003 21:38:15 GMT
Lines: 91
Message-ID: <r.1062365895.1747711181@[63.127.215.130]>
References: <l.1062309783.1757537841@host-66-81-126-37.rev.o1.com> <4cl4b.60112$ev.13538296@twister.nyc.rr.com>
NNTP-Posting-Host: 063.0127.215.130
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: 8bit
X-Mailer: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0) - ur
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!logbridge.uoregon.edu!newsfeed.stueberl.de!feed.news.nacamar.de!news1.ip-mobilphone.net!news2.ip-mobilphone.net!u50415039@spawnkill.ip-mobilphone.net