Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 从记录集中消除不重复的行_Sql Server_Tsql - Fatal编程技术网

Sql server 从记录集中消除不重复的行

Sql server 从记录集中消除不重复的行,sql-server,tsql,Sql Server,Tsql,下面的T-SQL显示了它下面的图表所示的结果。我想以一种方式编写T-SQL,它排除用户ID为1、95和161的行。这些行只出现一次,我不想将它们包括在结果集中。我想要的是显示UserID多次出现的行。任何帮助都将不胜感激。非常感谢你 select top(100) UserID, PhotoLabel from dbo.Photos where left(PhotoLabel, 8) in ( select distinct to

下面的T-SQL显示了它下面的图表所示的结果。我想以一种方式编写T-SQL,它排除用户ID为1、95和161的行。这些行只出现一次,我不想将它们包括在结果集中。我想要的是显示UserID多次出现的行。任何帮助都将不胜感激。非常感谢你

select top(100) UserID, PhotoLabel
from dbo.Photos
where left(PhotoLabel, 8) in ( 
                                select distinct top(10) left(PhotoLabel, 8) as date
                                from dbo.Photos
                                order by left(PhotoLabel,8) desc
                             )
order by UserID asc, left(PhotoLabel,8);


UserID         PhotoLabel  
======         ==============
  1            20160702064633
  2            20150915101504
  2            20150915101307
  2            20150915101152
  95           20150726135443
  159          20160330234026
  159          20160330234018
  161          20160223112742

只需将条件添加到
where
子句:

select top(100) UserID, PhotoLabel
from dbo.Photos
where left(PhotoLabel, 8) in (select distinct top(10) left(PhotoLabel, 8) as date
                              from dbo.Photos
                              order by left(PhotoLabel, 8) desc
                             ) and
      UserId not in (1, 95, 161)
order by UserID asc, left(PhotoLabel, 8);
注:

如果希望“自动”提取单例(即不列出它们),则使用窗口函数。您还可以使用窗口功能替换中的

with t as (
      select UserID, PhotoLabel
      from dbo.Photos
      where left(PhotoLabel, 8) in (select distinct top(10) left(PhotoLabel, 8) as date
                                    from dbo.Photos
                                    order by left(PhotoLabel,8) desc
                                   ) 
     )
select t.*
from (select t.*, count(*) over (partition by userid) as cnt
      from t
     ) t
where cnt > 1
order by userid, left(PhotoLabel, 8);

只需将条件添加到
where
子句:

select top(100) UserID, PhotoLabel
from dbo.Photos
where left(PhotoLabel, 8) in (select distinct top(10) left(PhotoLabel, 8) as date
                              from dbo.Photos
                              order by left(PhotoLabel, 8) desc
                             ) and
      UserId not in (1, 95, 161)
order by UserID asc, left(PhotoLabel, 8);
注:

如果希望“自动”提取单例(即不列出它们),则使用窗口函数。您还可以使用窗口功能替换
中的

with t as (
      select UserID, PhotoLabel
      from dbo.Photos
      where left(PhotoLabel, 8) in (select distinct top(10) left(PhotoLabel, 8) as date
                                    from dbo.Photos
                                    order by left(PhotoLabel,8) desc
                                   ) 
     )
select t.*
from (select t.*, count(*) over (partition by userid) as cnt
      from t
     ) t
where cnt > 1
order by userid, left(PhotoLabel, 8);

我以1、95和161为例。我想消除所有UserID只出现一次的行…因此您的第一个答案不起作用,因为结果集总是有不同的值。至于你答案的第二部分,我能修改现有代码而不必编写窗口函数吗?@sonnyk2016。看修改后的答案。我重新阅读了这个问题,并认为这可能是您的意图。修改后的答案仍然会生成带有单个用户标识的行。我运行了您提供的SQL。@sonnyk2016。您可能只看到一行
用户ID
,因为前10名中只有一行。我给出了1、95和161作为示例。我想消除所有UserID只出现一次的行…因此您的第一个答案不起作用,因为结果集总是有不同的值。至于你答案的第二部分,我能修改现有代码而不必编写窗口函数吗?@sonnyk2016。看修改后的答案。我重新阅读了这个问题,并认为这可能是您的意图。修改后的答案仍然会生成带有单个用户标识的行。我运行了您提供的SQL。@sonnyk2016。您可能只看到一行
用户ID
,因为前10行中只有一行。