473,378 Members | 1,639 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

question

#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
Jul 20 '05 #1
3 1830
"ri*****@yahoo.com" <u5*******@spawnkill.ip-mobilphone.net> wrote in message
news:l.*********************@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
Jul 20 '05 #2
Dear John Gilson,

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

"ri*****@yahoo.com" <u5*******@spawnkill.ip-mobilphone.net> wrote in message
news:l.*********************@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
Jul 20 '05 #3
"ri*****@yahoo.com" <u5*******@spawnkill.ip-mobilphone.net> wrote in message
news:r.*********************@[63.127.215.130]...
Dear John Gilson,

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

"ri*****@yahoo.com" <u5*******@spawnkill.ip-mobilphone.net> wrote in message
news:l.*********************@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


As I think I see where this is going, allow me to suggest a normalization
of relations. Let's assume your data isn't in a temp table as I will be
defining a view. Let's place your data in table T.

CREATE VIEW Rank (order_id, fname, age, rank)
AS
SELECT T1."order", T1.fname, T1.age, COUNT(DISTINCT T2.age)
FROM T AS T1
INNER JOIN
T AS T2
ON T1."order" = T2."order" AND
T2.age >= T1.age
GROUP BY T1."order", T1.fname, T1.age

SELECT R1.order_id, R1.fname AS fname1, R1.age AS age1,
R2.fname AS fname2, R2.age AS age2,
R3.fname AS fname3, R3.age AS age3
FROM Rank AS R1
LEFT OUTER JOIN
Rank AS R2
ON R1.order_id = R2.order_id AND
R2.rank = 2
LEFT OUTER JOIN
Rank AS R3
ON R3.order_id = R1.order_id AND
R3.rank = 3
WHERE R1.rank = 1

Regards,
jag
Jul 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Mohammed Mazid | last post by:
Can anyone please help me on how to move to the next and previous question? Here is a snippet of my code: Private Sub cmdNext_Click() End Sub Private Sub cmdPrevious_Click() showrecord
3
by: Stevey | last post by:
I have the following XML file... <?xml version="1.0"?> <animals> <animal> <name>Tiger</name> <questions> <question index="0">true</question> <question index="1">true</question> </questions>
7
by: nospam | last post by:
Ok, 3rd or is it the 4th time I have asked this question on Partial Types, so, since it seems to me that Partial Types is still in the design or development stages at Microsoft, I am going to ask...
3
by: Ekqvist Marko | last post by:
Hi, I have one Access database table including questions and answers. Now I need to give answer id automatically to questionID column. But I don't know how it is best (fastest) to do? table...
10
by: glenn | last post by:
I am use to programming in php and the way session and post vars are past from fields on one page through to the post page automatically where I can get to their values easily to write to a...
10
by: Rider | last post by:
Hi, simple(?) question about asp.net configuration.. I've installed ASP.NET 2.0 QuickStart Sample successfully. But, When I'm first start application the follow message shown. ========= Server...
53
by: Jeff | last post by:
In the function below, can size ever be 0 (zero)? char *clc_strdup(const char * CLC_RESTRICT s) { size_t size; char *p; clc_assert_not_null(clc_strdup, s); size = strlen(s) + 1;
56
by: spibou | last post by:
In the statement "a *= expression" is expression assumed to be parenthesized ? For example if I write "a *= b+c" is this the same as "a = a * (b+c)" or "a = a * b+c" ?
2
by: Allan Ebdrup | last post by:
Hi, I'm trying to render a Matrix question in my ASP.Net 2.0 page, A matrix question is a question where you have several options that can all be rated according to several possible ratings (from...
3
by: Zhang Weiwu | last post by:
Hello! I wrote this: ..required-question p:after { content: "*"; } Corresponding HTML: <div class="required-question"><p>Question Text</p><input /></div> <div...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.