简体   繁体   English

使用 DISTINCT 内连接 SQL

[英]Using DISTINCT inner join SQL

I have two tables T1 & T2 my tables look something like this我有两张桌子 T1 和 T2 我的桌子看起来像这样

T1 [ID, AppKey, CommenterKey, Comment, NoteTime]
T2 [ID, UserKey, Firstname, Lastname]

on T2 UserKey is correlated to CommenterKey T2 上的 UserKey 与 CommenterKey 相关

I would like to join these two tables while filtering the duplicate Comments on the Comments Column per each unique AppKey我想加入这两个表,同时根据每个唯一的 AppKey 过滤评论列上的重复评论

Any ideas on how to make this work would be greatly appreciated.任何关于如何使这项工作的想法将不胜感激。

This is the sample data:这是示例数据:

在此处输入图片说明

The idea here is to filter the duplicate comments pertaining to a certain appkey if you look at row 11-15 in the Appkey column it is the same appkey if you look at row 11-15 it is the same comment I want to filter these comments out so the query doesn't return these duplicate rows.这里的想法是过滤与某个 appkey 有关的重复评论,如果您查看 Appkey 列中的第 11-15 行它是相同的 appkey 如果您查看第 11-15 行它是相同的评论我想过滤这些评论out 所以查询不会返回这些重复的行。

Below is the query I used下面是我使用的查询

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

Your sample data is rather hard to read.您的示例数据很难阅读。 However, you can use row_number() or aggregation.但是,您可以使用row_number()或聚合。 I think this does what you want:我认为这可以满足您的要求:

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;

Based on the sample data, something like this should work.根据示例数据,这样的事情应该可以工作。

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

Your biggest issues is probably going to be performance.您最大的问题可能是性能。 You may want to consider adding a duplicate flag and either checking via a trigger, or scheduled job.您可能需要考虑添加重复标志并通过触发器或计划作业进行检查。

You can also use CTE Table.您还可以使用 CTE 表。 Below link is introduction about CTE Table and How to use it下面的链接是关于 CTE 表和如何使用它的介绍

https://www.essentialsql.com/introduction-common-table-expressions-ctes/ https://www.essentialsql.com/introduction-common-table-expressions-ctes/

I think this does what you want,我认为这可以满足您的要求,

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM