我想获得使用相同实体的外国团队和团队主页的名称。
这是我的关系
team : id, name
Game : id, team_id, team_fk_id
我想获得team team_id名称和 team_fk_id名称。
不知道该怎么做。
这是答案:
SELECT g.id, t1.name AS home_team, t2.name AS away_team
FROM Game AS g
JOIN team AS t1 ON t1.id = g.team_id
JOIN team AS t2 ON t2.id = g.team_fk_id
GROUP BY g.id
limit 1
答案 0 :(得分:0)
加入,[SelfJoinSet] as (
select
T1.[Contract], -- Contract from the base
T1.[Date], -- Date from the base
T2.[Customer], -- Customers from the current contract
T2.[Action], -- ActionType of the customer
-- RowNumber in order to get the last record for each customer within the current contract:
[RN] = row_number() over (partition by T2.[Customer],T1.[Date] order by T2.[Date] desc)
from T1
left join T1 as T2 -- SelfJoin
on T1.[Contract] = T2.[Contract] -- Within the current contract
and T1.[Date] >= T2.[Date] -- Get all records till the current date
where T1.[Action] = '-' --Filter out all '+' actions, we'll get only '-' records anyway (for performance optimization reasons).
)
select
[Contract],
[Date]
from [SelfJoinSet]
where [RN] = 1 --Show only last record per each customer
group by [Contract],[Date] -- Collapse all to the base table records
having count(distinct iif([Action] = '-',[Customer],NULL)) = count(distinct [Customer]) -- Leave only records where the last action is '-' for all customers
两次,一次为游戏中的每个团队加入:
team
答案 1 :(得分:0)
您需要在团队中[['X', 'AX', 'CAX', 'XYZ'], ['X', 'AX', 'CAX', 'ABC'], ['X', 'AX', 'AXG', 'DEF'], ['X', 'AX', 'AXG', 'MNO']]
两次,例如:
JOIN