473,396 Members | 2,014 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,396 software developers and data experts.

Trying to SELECT a SELF JOIN Max value

Hello All, Hope some Guru will be able to help me with this.

Let's take this example table

A
-------------------------------------------------------------------------------
id | TicketNo | evaluation | Username
-------------------------------------------------------------------------------
1 1 9 Jamie
2 1 8.5 Jocasta
3 1 9.1 Claire
4 2 9 Jamie
5 2 7.3 Fergus
6 3 6 Fergus
-------------------------------------------------------------------------------
Basically, what I want to do is

FIND all the TicketNo for which the Maximum evaluation have been given
by Jamie.

It should then return :

4 2 9 Jamie

and my TicketNo would be 2.
If I want the same query for Fergus, the result would be:
5 2 7.3 Fergus
6 3 6 Fergus

And if I want for Jocasta there will be an empty set.

I have try so many different request, but I assume my Query should be
something like

SELECT id,TicketNo,evaluation,Username FROM A WHERE ....???

I suppose from here I should do a self JOIN on the table, joining the
TicketNo to the TicketNo, and finding the id with the MAXIMUM
"joinned" value of evaluation...

I'm getting headache!!

Please help!

Much thanks in advance!


Jul 20 '05 #1
4 10872
"Denis St-Michel" <ds*******@aei.ca> wrote in message
A
-------------------------------------------------------------------------- ----- id | TicketNo | evaluation | Username
-------------------------------------------------------------------------- ----- 1 1 9 Jamie
2 1 8.5 Jocasta
3 1 9.1 Claire
4 2 9 Jamie
5 2 7.3 Fergus
6 3 6 Fergus
-------------------------------------------------------------------------- -----

Basically, what I want to do is

FIND all the TicketNo for which the Maximum evaluation have been given
by Jamie.
Let's work towards a solution.

Which version of MySql are you using? Version 4.1 supports nested
sub-queries, meaning that in the where clause of a select statement you have
another select statement. I got a hunch those will come in handy here.

In earlier versions of MySql you can create a temporary table, do the
sub-query and store the results in this temporary table, then do the real
query which will join to this temporary table, then finally drop the
temporary table.
It should then return :

4 2 9 Jamie

and my TicketNo would be 2.
If I want the same query for Fergus, the result would be:
5 2 7.3 Fergus
6 3 6 Fergus
I don't understand why Fergus gave the largest evaluation for ticket 2.
Didn't Jamie give the highest evaluation, namely 9 points?

Let's clarify the algorithm you want, then we can come up with a SQL
statement.
And if I want for Jocasta there will be an empty set.

I have try so many different request, but I assume my Query should be
something like

SELECT id,TicketNo,evaluation,Username FROM A WHERE ....???

I suppose from here I should do a self JOIN on the table, joining the
TicketNo to the TicketNo, and finding the id with the MAXIMUM
"joinned" value of evaluation...


Something like

SELECT id,TicketNo,evaluation,Username FROM A WHERE EXISTS (SELECT ... FROM
A WHERE ...)

might work.

See my post with subject "find all products with maximum version" which you
can search in groups.google.com with

find all products with maximum version group:mailing.database.mysql

or if you can get this link to work:

http://groups.google.com/groups?hl=e...ic.15911%24_o3
..514673%40bgtnsc05-news.ops.worldnet.att.net&rnum=1&prev=/groups%3Fq%3Dfind%
2Ball%2Bproducts%2Bwith%2Bmaximum%2Bversion%2Bgrou p:mailing.database.mysql%2
6hl%3Den%26lr%3D%26ie%3DUTF-8%26group%3Dmailing.database.mysql%26selm%3DXDli
c.15911%2524_o3.514673%2540bgtnsc05-news.ops.worldnet.att.net%26rnum%3D1
Jul 20 '05 #2
On Fri, 27 Aug 2004 04:29:50 GMT, "Siemel Naran"
<Si*********@REMOVE.att.net> wrote:

Which version of MySql are you using? Version 4.1 supports nested
sub-queries, meaning that in the where clause of a select statement you have
another select statement. I got a hunch those will come in handy here.
I'ts 4.0.20-standard therefore it doesn't support nested
sub-queries...that's too bad..

I don't understand why Fergus gave the largest evaluation for ticket 2.
Didn't Jamie give the highest evaluation, namely 9 points?


You're totally right.. my mistake..
I finally been able to do what I Wanted by building my request this
way:

SELECT t1.id,t1.TicketNo,t1.evaluation,t1.Username FROM A t1 INNER
JOIN A t2 ON t1.TicketNo = t2.TicketNo GROUP BY t1.id, t1.TicketNo,
t1.evaluation, t1.Username HAVING t1.evaluation = MAX(t2.evaluation);

and it seems to be working well.
But I'm still concern about the efficiency. Is this the best way to do
it (considering I can't update to MySQL 4.1 for now) ..?

Thanks again,
Jul 20 '05 #3
"Denis St-Michel" <ds*******@aei.ca> wrote in message
I finally been able to do what I Wanted by building my request this
way:

SELECT t1.id,t1.TicketNo,t1.evaluation,t1.Username FROM A t1 INNER
JOIN A t2 ON t1.TicketNo = t2.TicketNo GROUP BY t1.id, t1.TicketNo,
t1.evaluation, t1.Username HAVING t1.evaluation = MAX(t2.evaluation);

and it seems to be working well.


Clever! Only thing is you have to repeat the select columns in the group
by, which means lots of typing if you have more columns.
But you mentioned you want to find ticket numbers where a certain Username
gave the maximum evaluation. For this I think the following should work:

SELECT t1.id,t1.TicketNo,t1.evaluation,t1.Username
FROM
A t1
INNER JOIN A t2 ON t1.TicketNo = t2.TicketNo
where t1.Username = 'Fergus'
GROUP BY t1.id, t1.TicketNo, t1.evaluation, t1.Username
HAVING t1.evaluation = MAX(t2.evaluation)
;

It's important to know what you're after, as you'd create different indexes
for each scenario: find ticket nos for which Username gave the maximum
evaluation as you said in the original post, or find the Username for every
ticket no who gave the maximum evaluation. They differ by just a where
clause on Username = ?. but may require different indices to optimize.
I think it should be efficient if you have an index on a ( TicketNo,
evaluation desc, Username ). Hopefully the engine drives by t2 on this
index, quickly finding the maximum evaluation for every ticket no, joins to
t1 on the same index using all three columns of the index. But running the
SQL in the control center shows it drives by t1 using no index, then joins
to t2 using the above index. Note: I may be reading the explain tab
incorrectly, but it lists t1 first then t2, so I think it's driving by t1.
And this even if I GROUP BY t1.TicketNo, t1.evaluation desc, t1.Username,
t1.id, or add an ORDER BY t1.TicketNo ..., or reverse the table list
(selecting from t2 first then t1).
Better yet, the engine realizes that the join to t1 selects the same row
(kind of) and so avoids the join, and instead just applies the where
Username = ? clause to determine whether to show this row or move on to the
next one.

Of course, in order to quickly find the maximum evaluation for each ticket
no, we'd like a special index: one way is that the index on ( TicketNo,
.... ) stores the TicketNo, followed by the number of records with this
TicketNo, followed by this many pointers to the original records. The
advantage of this data structure is that we can quickly find the maximum
evaluation for the first ticket no (it's the first record), then quickly
jump to the next ticket no (because we know the number of records to skip).
But I don't know what kind of indexes MySql uses, and create index does not
allow us to specify the type of index.
Note that the IN method (possible on version 4.1 and later) also drives by
no index on the outer table, then the index ( TicketNo, evaluation desc,
Username ) on t2. Again, this is assuming I'm reading the explain tab
correctly, which lists t1 first. Here goes:

select t1.id,t1.TicketNo,t1.evaluation,t1.Username
from A t1 where t1.evaluation = (
select max(evaluation)
from A t2
where t1.ticketno = t2.ticketno
)
and Username = 'Fergus'
;

We could try an index ( Username, TicketNo, evaluation desc ). It seems the
engine drives by t1, but only uses the first column of the index.
The select from view method

select t.id,t.TicketNo,t.evaluation,t.Username
from A t,
(select t2.ticketno as ticketno, max(evaluation) as evaluation
from A t2 group by t2.ticketno) as tt
where
t.ticketno = tt.ticketno and t.evaluation = tt.evaluation
and t.Username = 'Fergus'
;

appears to drive by oops, what? The explain tab has me confused.
Finally, you could try the force index and related extension of MySql to try
to force a certain driving table and index. I haven't tried this though.
And you can explicitly do the inner query first by using temporary tables.

create temporary table A2 (
ticketno int,
evaluation double
);

insert into A2 (ticketno, evaluation)
select ticketno, max(evaluation)
from A
group by ticketno;

create index A2_index on A2 ( ticketno, evaluation );

select A.id,A.TicketNo,A.evaluation,A.Username
from A inner join A2 on A.ticketno = A2.ticketno and A.evaluation =
A2.evaluation
where A.Username = 'Fergus'
;

Jul 20 '05 #4
On Sat, 28 Aug 2004 17:34:33 GMT, "Siemel Naran"
<Si*********@REMOVE.att.net> wrote:

Wow, that was a crash course for me. I'm kind of new to MySQL. your
explanations are very welcomes and helps me getting even more
interested in MySQL.

thanks again!!

Dennis

Jul 20 '05 #5

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

Similar topics

1
by: D. Shifflett | last post by:
Hi all, I am having trouble with a program that ran fine on Python 2.0 (#0, Mar 1 2001, 01:47:55) on linux2 but will not work on Python 2.3.2 (#1, Oct 8 2003, 17:33:47) on linux2
4
by: Denis St-Michel | last post by:
Hello All, Hope some Guru will be able to help me with this. Let's take this example table A ------------------------------------------------------------------------------- id | ...
17
by: kalamos | last post by:
This statement fails update ded_temp a set a.balance = (select sum(b.ln_amt) from ded_temp b where a.cust_no = b.cust_no and a.ded_type_cd = b.ded_type_cd and a.chk_no = b.chk_no group by...
1
by: dbuchanan | last post by:
VB.NET 2003 / SQLS2K The Stored Procedure returns records within Query Analyzer. But when the Stored Procedure is called by ADO.NET ~ it produced the following error message. ...
0
by: Mark Gifford | last post by:
Hi, I've inherited an Access database and need to create a form which will allow me to view and alter the data. I've created a new form and using a tip I got from this helpful site...
0
by: Susan Bricker | last post by:
The following error: "The current field must match the join key '?' in the table that seves as t the 'one' side of one-to-many relationship. Enter a record in the 'one' side table with the...
2
by: Ev | last post by:
I have a database table in SQL Server that has a self join. In C# I have a DataTable with a self-join. I have defined a foreign key constraint on the DataTable for the self join. The...
2
by: comp.lang.php | last post by:
var $filterArray = array('reverse' =IMG_FILTER_NEGATE, 'edge highlight' =IMG_FILTER_EDGEDETECT, 'emboss' =IMG_FILTER_EMBOSS, 'gaussian blur' =IMG_FILTER_GAUSSIAN_BLUR, 'blur'...
5
by: RioRanchoMan | last post by:
I have a forum table where the field Forum_ID of the first thread corresponds to itself in the field Forum_Ancestor, and 0 (zero) for the field Forum_Parent when it is the first topic in a thread:...
22
by: MP | last post by:
vb6,ado,mdb,win2k i pass the sql string to the .Execute method on the open connection to Table_Name(const) db table fwiw (the connection opened via class wrapper:) msConnString = "Data Source="...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.