使用 DISTINCT 内连接 SQL
Using DISTINCT inner join SQL
我有两个表 T1 和 T2 我的表看起来像这样
T1 [ID, AppKey, CommenterKey, Comment, NoteTime]
T2 [ID, UserKey, Firstname, Lastname]
在 T2 上 UserKey 与 CommenterKey 相关
我想加入这两个表,同时根据每个唯一的 AppKey 过滤评论列上的重复评论
任何关于如何使这项工作的想法将不胜感激。
这是示例数据:
这里的想法是过滤与某个 appkey 相关的重复评论如果你查看 Appkey 列中的第 11-15 行它是相同的 appkey 如果你查看第 11-15 行它是相同的评论我想过滤掉这些评论,这样查询就不会 return 这些重复的行。
下面是我使用的查询
SELECT Notes.Appkey,
Notes.CommenterKey,
Notes.Comment,
Notes.NoteTime,
Users.Firstname,
Users.Lastname
FROM tblNotes AS Notes
inner join
tblUsers AS Users ON Commenterkey = UserKey
根据示例数据,类似这样的操作应该可行。
select n.NoteKey,
n.AppKey,
n.CommenterKey,
n.Comment,
u.Firstname,
u.Lastname
from Notes n
cross apply (
select AppKey,
CommenterKey,
Comment,
max(NoteTime) as NoteTime
from Notes n2
where n.AppKey = n2.AppKey
and n.CommenterKey = n2.CommenterKey
and n.Comment = n2.Comment
group by
n2.AppKey,
n2.CommenterKey,
n2.Comment
) ni
join Users u ON u.UserKey = n.CommenterKey
where ni.NoteTime = n.NoteTime
您最大的问题可能是性能。您可能需要考虑添加重复标志并通过触发器或计划作业进行检查。
您的示例数据相当难以阅读。但是,您可以使用 row_number()
或聚合。我认为这符合您的要求:
select un.*
from (select n.Appkey, n.CommenterKey, n.Comment, n.NoteTime,
u.Firstname, u.Lastname,
row_number() over (partition by u.UserKey, n.Comment order by u.UserKey) as seqnum
from tblNotes n inner join
tblUsers u
on n.Commenterkey = u.UserKey
) un
where seqnum = 1;
您也可以使用 CTE Table。下面 link 是关于 CTE Table 和如何使用它的介绍
https://www.essentialsql.com/introduction-common-table-expressions-ctes/
我认为这符合您的要求,
with cte as
(
select notes.Appkey as appKey, notes.CommenterKey as CommenterKey, notes.Comment as Comment, notes.NoteTime as NoteTime,
users.Firstname as Firstname, users.Lastname as Lastname,
row_number() over (partition by users.UserKey, notes.Comment order by users.UserKey) as sequenceNo
from tblNotes as notes inner join
tblUsers as users
on notes.Commenterkey = users.UserKey
)
select * from cte where sequenceNo = 1;
我有两个表 T1 和 T2 我的表看起来像这样
T1 [ID, AppKey, CommenterKey, Comment, NoteTime]
T2 [ID, UserKey, Firstname, Lastname]
在 T2 上 UserKey 与 CommenterKey 相关
我想加入这两个表,同时根据每个唯一的 AppKey 过滤评论列上的重复评论
任何关于如何使这项工作的想法将不胜感激。
这是示例数据:
这里的想法是过滤与某个 appkey 相关的重复评论如果你查看 Appkey 列中的第 11-15 行它是相同的 appkey 如果你查看第 11-15 行它是相同的评论我想过滤掉这些评论,这样查询就不会 return 这些重复的行。
下面是我使用的查询
SELECT Notes.Appkey,
Notes.CommenterKey,
Notes.Comment,
Notes.NoteTime,
Users.Firstname,
Users.Lastname
FROM tblNotes AS Notes
inner join
tblUsers AS Users ON Commenterkey = UserKey
根据示例数据,类似这样的操作应该可行。
select n.NoteKey,
n.AppKey,
n.CommenterKey,
n.Comment,
u.Firstname,
u.Lastname
from Notes n
cross apply (
select AppKey,
CommenterKey,
Comment,
max(NoteTime) as NoteTime
from Notes n2
where n.AppKey = n2.AppKey
and n.CommenterKey = n2.CommenterKey
and n.Comment = n2.Comment
group by
n2.AppKey,
n2.CommenterKey,
n2.Comment
) ni
join Users u ON u.UserKey = n.CommenterKey
where ni.NoteTime = n.NoteTime
您最大的问题可能是性能。您可能需要考虑添加重复标志并通过触发器或计划作业进行检查。
您的示例数据相当难以阅读。但是,您可以使用 row_number()
或聚合。我认为这符合您的要求:
select un.*
from (select n.Appkey, n.CommenterKey, n.Comment, n.NoteTime,
u.Firstname, u.Lastname,
row_number() over (partition by u.UserKey, n.Comment order by u.UserKey) as seqnum
from tblNotes n inner join
tblUsers u
on n.Commenterkey = u.UserKey
) un
where seqnum = 1;
您也可以使用 CTE Table。下面 link 是关于 CTE Table 和如何使用它的介绍
https://www.essentialsql.com/introduction-common-table-expressions-ctes/
我认为这符合您的要求,
with cte as
(
select notes.Appkey as appKey, notes.CommenterKey as CommenterKey, notes.Comment as Comment, notes.NoteTime as NoteTime,
users.Firstname as Firstname, users.Lastname as Lastname,
row_number() over (partition by users.UserKey, notes.Comment order by users.UserKey) as sequenceNo
from tblNotes as notes inner join
tblUsers as users
on notes.Commenterkey = users.UserKey
)
select * from cte where sequenceNo = 1;