我希望能够在表中找到一列相同但另一列不同的事件。
示例表:
id team week
1 1 1
1 2 1
2 2 1
2 1 2
我想要一个查询,找到团队不同的所有ID,但周仍然是相同的,所以像团队这样的东西不等于团队,但是周等于一周。
基本上我想知道在同一周内是否有任何ID改变了团队,我该怎么做?
答案 0 :(得分:6)
SELECT
t1.id,
t1.week
FROM
YourTable t1
JOIN YourTable t2
ON t1.ID = t2.ID
AND t1.team < t2.team
AND t1.week = t2.week
答案 1 :(得分:0)
SELECT ID
, COUNT(DISTINCT TEAM) AS CNT_TEAM
, COUNT(DISTINCT WEEK) AS CNT_WEEK
FROM TABLENAME
GROUP BY ID
HAVING COUNT(DISTINCT TEAM) > 1
AND COUNT(DISTINCT WEEK) = 1
答案 2 :(得分:0)
这样的东西?
SELECT distinct id
FROM TeamWeek
GROUP BY id, week
HAVING Count(team) > 1