3

我有一张记录课程进度状态的表格。当课程开始时,会为每个用户/课程组合添加一条新记录。当课程完成时,该记录将更新为“完成”状态。我需要查找从未完成任何课程的用户的记录。

示例表:

User  Course  Status
A      1       S
A      2       C
B      1       S
C      2       S
D      2       C
C      3       S

我需要一个能找到以下内容的查询:

User  Course  Status
B      1        S
C      2        S
C      3        S

任何帮助表示赞赏。

4

4 回答 4

2
select user, course, status
from your_table
where user in 
(
    select user
    from your_table
    group by user
    having sum(CASE WHEN status = 'C' THEN 1 ELSE 0 END) = 0
)
于 2012-11-29T17:42:41.130 回答
1
Select User, Course, Status from MyTable where User not in (Select Distinct User from MyTable where Status = 'C')
于 2012-11-29T17:42:13.083 回答
1
SELECT User,Course,Status FROM YourTable a
LEFT JOIN
(SELECT DISTINCT User FROM YourTable WHERE Status='C') CompletedAnything
ON a.User=CompletedAnything.User
WHERE COmpletedAnything.User IS NULL
于 2012-11-29T17:44:17.370 回答
0

这是一个 SQL Fiddle,可以为您提供所需的内容: http ://sqlfiddle.com/#!2/b6988/1

查询是这样的:

select User, Course, Status
from mytable
where User not in 
  (select distinct User from mytable where status = 'C' ans User is not null)
于 2012-11-29T17:53:09.170 回答