[英]I want to select two different columns from two tables based on another column by SQL
I really get stuck with this question: I have two tables from an Oracle 10g XE database. 我真的陷入了这个问题:我有一个来自Oracle 10g XE数据库的两个表。 I have been asked to give out the FName and LName of the fastest male and female athletes from an event. 我被要求给出比赛中最快的男女运动员的FName和LName。 The eventID will be given out. 将给出eventID。 It works like if someone is asking the event ID, the top male and female's FName and LName will be given out separately. 就像有人在询问事件ID一样,男性和女性的头名FName和LName将分别给出。
I should point out that each athlete will have a unique performance record. 我应该指出,每位运动员都有独特的表现记录。 Thanks for reminding that! 感谢您的提醒!
Here are the two tables. 这是两个表。 I spent all the night on that. 我整夜都在上面。
ATHLETE 运动员
╔═════════════════════════════════════════╦══╦══╗
║ athleteID* FName LName Sex Club ║ ║ ║
╠═════════════════════════════════════════╬══╬══╣
║ 1034 Gabriel Castillo M 5011 ║ ║ ║
║ 1094 Stewart Mitchell M 5014 ║ ║ ║
║ 1161 Rickey McDaniel M 5014 ║ ║ ║
║ 1285 Marilyn Little F ║ ║ ║
║ 1328 Bernard Lamb M 5014 ║ ║ ║
║ ║ ║ ║
╚═════════════════════════════════════════╩══╩══╝
PARTICIPATION_IND PARTICIPATION_IND
╔═════════════════════════════════════════════╦══╦══╗
║ athleteID* eventID* Performance_in _Minutes ║ ║ ║
╠═════════════════════════════════════════════╬══╬══╣
║ 1094 13 18 ║ ║ ║
║ 1523 13 17 ║ ║ ║
║ 1740 13 ║ ║ ║
║ 1285 13 21 ║ ║ ║
║ 1439 13 25 ║ ║ ║
╚═════════════════════════════════════════════╩══╩══╝
SELECT * FROM (
SELECT
a.FName
, a.LName
FROM ATHLETE a
JOIN PARTICIPATION_IND p
ON a.athleteID = p.athleteID
WHERE a.Sex = 'M'
AND p.eventID = 13
ORDER BY p.Performance_in_Minutes
)
WHERE ROWNUM = 1
UNION
SELECT * FROM
SELECT (
a.FName
, a.LName
FROM ATHLETE a
JOIN PARTICIPATION_IND p
ON a.athleteID = p.athleteID
WHERE a.Sex = 'F'
AND p.eventID = 13
ORDER BY p.Performance_in_Minutes
)
WHERE ROWNUM = 1
here you go 干得好
Select A.FName, A.LName
from Athelete A
Join Participation_IND P
on A.AtheleteID= p.AtheleID
And
P.Performance_in_Minutes >=(Select max (performace_in_minutes) from Participation_IND)
Thanks 谢谢
Here's one approach: 这是一种方法:
SELECT a.FName
, a.LName
, a.sex
, p.eventID
, p.performance_in_minutes
FROM ( SELECT i.eventID, g.sex, MIN(i.performance_in_minutes) AS fastest_pim
FROM athlete g
JOIN participation_ind i
ON i.athleteID = g.athleteID
WHERE i.eventID IN (13) -- supply eventID here
GROUP BY i.eventID, g.sex
) f
JOIN participation_ind p
ON p.eventID = f.eventID
AND p.performance_in_minutes = f.fastest_pim
JOIN athlete a
ON a.athleteID = p.athleteID
AND a.sex = f.sex
The inline view aliased as f gets the fastest time for each eventID, at most one per "sex" from the athlete table. 别名为f的内联视图为每个eventID获得最快的时间,对于运动者表中的每个“性别”最多为一个。
We use a JOIN operation to extract the rows from participation_ind that have a matching eventID and performance_in_minutes, and we use another JOIN operation to get matching rows from athlete. 我们使用JOIN操作从具有相同的eventID和performance_in_minutes的participation_ind中提取行,并且使用另一个JOIN操作从运动员中获取匹配的行。 (Note that we need to include join predicate on the "sex" column, so we don't inadvertently pull a row with a matching "minutes" value and a non-matching gender.) (请注意,我们需要在“性别”列上包括加入谓词,因此我们不会无意间拉出具有匹配的“分钟”值和性别不匹配的行。)
NOTE: If there are two (or more) athletes of the same gender that have a matching "fastest time" for a particular event, the query will pull all those matching rows; 注意:如果有两个(或更多)相同性别的运动员在特定事件中具有匹配的“最快时间”,则查询将拉出所有匹配的行; the query doesn't distinguish a single "fastest" for each event, it distinguishes only the athletes that have the "fastest" time for a given event. 该查询不会为每个事件区分一个“最快”,而是仅区分给定事件的“最快”时间的运动员。
The "extra" columns in the SELECT list can be omitted/removed. SELECT列表中的“额外”列可以省略/删除。 Those aren't required; 这些不是必需的。 they are only included to aid in debugging. 仅包括它们以帮助调试。
SELECT a.FName, a.LName
FROM ATHLETE a
WHERE
a.athleteID IN (
SELECT mp.athleteID
FROM PARTICIPATION_IND mp
INNER JOIN ATHLETE ma
ON mp.athleteID = ma.athleteID
WHERE
mp.eventID = 13 AND
ma.Sex = 'M'
HAVING MAX(mp.Performance_in_minutes)
UNION
SELECT fp.athleteID
FROM PARTICIPATION_IND fp
INNER JOIN ATHLETE fa
ON fp.athleteID = fa.athleteID
WHERE
fp.eventID = 13 AND
fa.Sex = 'F'
HAVING MAX(fp.Performance_in_minutes)
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.