哪些子句不适用 - SQL查询

时间:2017-11-07 11:11:32

标签: sql sql-server

我有这个选择查询:

(SELECT TOP (1) TRASSESSOR.ASSESSORID
 FROM TRAINEE 
 INNER JOIN TRASSESSOR ON TRAINEE.TRAINEEID = TRASSESSOR.TRAINEEID
 WHERE (TRASSESSOR.ASSESSORTYPE = 'E' OR TRASSESSOR.ASSESSORTYPE = 'F') AND
       (TRASSESSOR.TRAINEEID = TRAINEE.TRAINEEID)
 ORDER BY TRASSESSOR.ENDDATE DESC)

这样做的目的是为特定的ASESSORID

选择唯一的TRAINEEID

由于某些原因,我每ASESSORID收到相同的TRAINEEID,而不是根据联接过滤到正确的ASESSORID

有什么想法吗?

编辑 - 这是我得到的示例的片段:

MISLearnerID    StudentID   MISEmployerID   Courses/Course/MISAssessorID
0295-0003-315032    0295-0003-315032    0295-0003-259362    0295-0001-100053
0295-0003-315032    0295-0003-315032    0295-0003-259362    0295-0001-100053
0624-0001-104878    0624-0001-104878    0295-0003-203134    0295-0001-100053
0624-0001-104878    0624-0001-104878    0295-0003-203134    0295-0001-100053
0624-0001-104878    0624-0001-104878    0295-0003-203134    0295-0001-100053
0624-0001-104878    0624-0001-104878    0295-0003-203134    0295-0001-100053
0295-0003-306792    0295-0003-306792    0295-0003-256903    0295-0001-100053
0295-0003-306792    0295-0003-306792    0295-0003-256903    0295-0004-250714
0295-0003-306792    0295-0003-306792    0295-0003-256903    0295-0001-100053
0295-0003-306792    0295-0003-306792    0295-0003-256903    0295-0001-100053
0295-0003-306792    0295-0003-306792    0295-0003-256903    0295-0001-100053

编辑2 - 我实际上要尝试实现的是选择与ASSESSORIDASSESSORTYPE = 'E' OR 'F'相关联的TRAINEEID TRAINEEID查询中的每个TRAINEEID。该查询将加载数百行,但目前它不是基于TRASSESSOR选择,而是查看ASSESSORID表,只选择每行找到的第一个sed 1d *.csv | sort -r| uniq > merged-file.csv

1 个答案:

答案 0 :(得分:1)

您的问题很难阅读。您应该发布有关数据外观和期望的示例。根据你的评论,我认为你需要做这样的事情:

测试数据

生:

enter image description here

Trassor:

enter image description here

select * from trainee a
inner join (
select trassorid,assesorid from (
select  trassorid,assesorid,ROW_NUMBER() over(PARTITION by trassorid order by assesorid ) as rn from trassor
where assesorid in ('e','f')
)z where rn = 1
) x on x.trassorid = a.traineeid

根据你的说法你只需要一个随机的assesor,如果有几个。在这里,我使用ROW_NUMBER从您的trassorID中选择1。然后我和你的traineeids一起加入,以匹配你需要的一个随机匹配。

<强>结果

enter image description here

如果您需要与您的traineeID匹配的所有trassorID,以便获得多次点击,则只需删除ROW_NUMBER

select * from trainee a
inner join (
select trassorid,assesorid from (
select  trassorid,assesorid from trassor
where assesorid in ('e','f')
)z 
) x on x.trassorid = a.traineeid

enter image description here