【问题标题】:SQL SERVER Select Distinct ID is not workingSQL SERVER Select Distinct ID 不起作用
【发布时间】:2020-12-08 11:25:53
【问题描述】:

我正在尝试通过下面的这个 sql Select 语句,但我无法正确完成。

Select DISTINCT vc.cid, vt.tid, vc.device,
    STUFF((select ', ' + c.tName from thumbTbl t2
                    join tags v ON t2.cid = v.cid
                    join config c on v.tid = c.tid
                    where vc.cid = t2.cid
                    group by c.tName 
FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(max)'), 1, 1, '') tName
from tags vt
    Inner join thumbTbl vc on vt.cid = vc.cid
    Left join config vtc on vt.tid = vtc.tid
order by vc.cid asc

结果如下:

cid tid device      tName
--- --- ---------   ---------
159 7   Mat Dwens    Escalation, Follow Up, More Benefits
159 11  Mat Dwens    Escalation, Follow Up, More Benefits
159 12  Mat Dwens    Escalation, Follow Up, More Benefits
160 7   Jeniffer P    Rectro
162 8   Marc Novice   More Benefits, Rectro
162 6   Marc Novice   More Benefits, Rectro
165 4   Jeniffer P    Follow up

问题:如何只显示每个 Selected Id 的 1 行?,我尝试使用 SELECT DISTINCT 但仍然给了我相同的结果。

我想看到的结果应该是这样的:

cid tid device      tName
------- ---------   ----------
159 12  Mat Dwens   Escalation, Follow Up, More Benefits
160 7   Jeniffer P  Rectro
162 8   Marc Novice More Benefits, Rectro
165 4   Jeniffer P  Follow up

有人有想法吗?

【问题讨论】:

  • 请提供样本数据和期望的结果。
  • @GordonLinoff 只需添加示例数据,谢谢

标签: sql sql-server distinct for-xml-path select-for-xml


【解决方案1】:

您要的不是DISTINCT,而是GROUP BYtid 上的聚合函数。结果DISTINCT,因为您在每一行上都有不同的tid 值,但是,您想要的是MAX。由于缺乏样本数据,这是未经测试的,但是,我认为这是正确的:

SELECT vc.cid,
       MAX(vt.tid) AS tid,
       vc.device,
       STUFF((SELECT ', ' + c.tName
              FROM thumbTbl t2
                   JOIN tags v ON t2.cid = v.cid
                   JOIN config c ON v.tid = c.tid
              WHERE vc.cid = t2.cid
              GROUP BY c.tName
             FOR XML PATH(''), TYPE).value('(./text())[1]', 'nvarchar(max)'),1,2,'') AS tName --Changed to use .text() and also removed leading space
FROM tags vt
     INNER JOIN thumbTbl vc ON vt.cid = vc.cid
     LEFT JOIN config vtc ON vt.tid = vtc.tid
GROUP BY vc.cid,
         vc.device
ORDER BY vc.cid ASC;

【讨论】:

  • 成功了,这把它拉出来了,只是我不想使用Max()GroupBy 函数。除了GroupBy查询,你没有别的办法了吗?
  • 没有样本数据,我不想冒险猜测,@PatsonLeaner。然而,这是一个不同的问题,应该作为一个新问题发布。
  • 我做了,但是提供了上面关于问题的示例数据。你能用得上吗?
  • 问题中没有样本数据,@PatsonLeaner,只有结果;不希望的结果和预期的结果。但这并没有改变我的评论;如果您想问如何以不同的方式解决问题,那是一个不同的问题,您应该问一个新问题。将问题更改为该问题会使我和 Gordon 的答案都无效,并且会被用户回复,或者很容易导致问题吸引反对票。社区非常不赞成将问题更改为无效的现有答案。
【解决方案2】:

在外部查询中删除SELECT DISTINCT 和与config 的连接。然后修复内部查询,使其只从config 中引入您想要的内容:

select vc.cid, vt.tid, vc.device,
       stuff((select ', ' + c.tName
              from config c on v.tid = c.tid
              where v.tid = c.tid
              for xml path(''), TYPE
             ).value('.', 'NVARCHAR(max)'
                    ), 1, 1, ''
            ) tNames
from tags vt join
     thumbTbl vc
     on vt.cid = vc.cid
group by vc.cid, vt.tid, vc.device
order by vc.cid asc

【讨论】:

  • 加入标签 v ON t2.cid = v.cid 这是我声明 v 的地方。现在在你上面的代码中 v 没有启动。我应该保留它吗?还有fromon Incorrect Syntax 在东西下
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-25
  • 2017-04-12
  • 2021-12-29
相关资源
最近更新 更多