SQL 应用不同的一列

SQL Apply Distinct One Column

我有一个 SQL 查询如下。我想将 DISTINCT 应用于此查询中的 Name 列。你能帮帮我吗?

SELECT Id,
       ConflictCheckRequestIndividualId,
       Name,
       Surname,
       it.IndividualType AS IndividualType,
       JobTitle,
       RegistrationNumber,
       Title,
       District,
       Status,
       CreatedBy,
       Created,
       ModifiedBy,
       Modified
  FROM ConflictCheckItoIndividual
  LEFT JOIN @IndividualTypes it
    ON it.IndividualId = ConflictCheckRequestIndividualId
 WHERE ConflictCheckRequestIndividualId IN
       (SELECT Id
          FROM ConflictCheckRequestIndividual
         WHERE ConflictCheckRequestId = @ConflictId
           AND SubStatus = 2)

只需意识到您的要求意味着什么:您获得了 1 个特定名称的随机数据集,但如果此刻这对您来说足够好,则此代码应该执行以下操作:

select * from (
SELECT 
       ROW_NUMBER() over (partition by Name order by Id) [row],
       Id,
       ConflictCheckRequestIndividualId,
       Name,
       Surname,
       it.IndividualType AS IndividualType,
       JobTitle,
       RegistrationNumber,
       Title,
       District,
       Status,
       CreatedBy,
       Created,
       ModifiedBy,
       Modified
  FROM ConflictCheckItoIndividual
  LEFT JOIN @IndividualTypes it
    ON it.IndividualId = ConflictCheckRequestIndividualId
 WHERE ConflictCheckRequestIndividualId IN
       (SELECT Id
          FROM ConflictCheckRequestIndividual
         WHERE ConflictCheckRequestId = @ConflictId
           AND SubStatus = 2)) data
           where [row] = 1

你能告诉我这是否适合你吗?

两种方式,结果略有不同。 “GROUP BY X”是“给我一行每个 X”的另一种说法。您必须将聚合函数应用于每一行,以便它知道如何将行压缩为 1:

SELECT MAX(Id),
       MAX(ConflictCheckRequestIndividualId),
       Name,
       MAX(Surname),
       MAX(it.IndividualType) AS IndividualType,
       MAX(JobTitle),
       MAX(RegistrationNumber),
       MAX(Title),
       MAX(District),
       MAX(Status),
       MAX(CreatedBy),
       MAX(Created),
       MAX(ModifiedBy),
       MAX(Modified)
  FROM ConflictCheckItoIndividual
  LEFT JOIN @IndividualTypes it
    ON it.IndividualId = ConflictCheckRequestIndividualId
 WHERE ConflictCheckRequestIndividualId IN
       (SELECT Id
          FROM ConflictCheckRequestIndividual
         WHERE ConflictCheckRequestId = @ConflictId
           AND SubStatus = 2)
GROUP BY Name

这可能会导致每个字段的数据来自不同的行。如果你希望所有数据都来自一行,你可以这样做

;WITH cte AS
(
SELECT Id,
       ConflictCheckRequestIndividualId,
       Name,
       Surname,
       it.IndividualType AS IndividualType,
       JobTitle,
       RegistrationNumber,
       Title,
       District,
       Status,
       CreatedBy,
       Created,
       ModifiedBy,
       Modified,
       ROW_NUMBER() OVER (PARTITION BY Name ORDER BY Modified DESC) as rownum
  FROM ConflictCheckItoIndividual
  LEFT JOIN @IndividualTypes it
    ON it.IndividualId = ConflictCheckRequestIndividualId
 WHERE ConflictCheckRequestIndividualId IN
       (SELECT Id
          FROM ConflictCheckRequestIndividual
         WHERE ConflictCheckRequestId = @ConflictId
           AND SubStatus = 2)
)
SELECT * FROM cte WHERE rownum = 1

这是将数据“分区”到每个名称的一个桶中。在每个桶中,它按修改后的降序排列行。然后我们只从每个桶中挑选一行 - 最近修改的一行。

顺便说一句,考虑到有一个姓名字段,我希望也按此分组