Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/86.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/24.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_Sql Server_Select_Sql Server 2012 - Fatal编程技术网

SQL Server-选择不同的查询

SQL Server-选择不同的查询,sql,sql-server,select,sql-server-2012,Sql,Sql Server,Select,Sql Server 2012,我有一个tblA,我有几千个客户。我正在尝试编写一个查询,向我显示具有多个特定类型记录的客户端。这就是我的桌子的样子 ClientID TypeB 123 1 145 1 123 2 199 1 199 2 145 2 123 1 正如您在这里看到的(这不是一个完整的表,但这只是相关

我有一个tblA,我有几千个客户。我正在尝试编写一个查询,向我显示具有多个特定类型记录的客户端。这就是我的桌子的样子

ClientID          TypeB
123               1
145               1
123               2
199               1
199               2
145               2
123               1
正如您在这里看到的(这不是一个完整的表,但这只是相关的)。对于TypeB=1和TypeB=2,每个客户机应该只有一条记录。然而,对于类型B=1,一些客户端(123)有多个记录。我正在尝试查找具有多个类型为1的记录的所有客户端

预期的最终结果:

ClientID           TypeB
123                1
123                1
这就是我一直想做的

 select distinct(clientid), TypeB
 from tblA 
 where TypeB=1
 having count(TypeB)>1
 group by clientid
尝试使用

SELECT a.clientid, 
       a.typeb 
FROM   tblA  a 
WHERE  (SELECT Count(*) 
        FROM   tblA  b 
        WHERE  b.clientid = a.clientid 
               AND b.typeb = 1) > 1 
尝试使用

SELECT a.clientid, 
       a.typeb 
FROM   tblA  a 
WHERE  (SELECT Count(*) 
        FROM   tblA  b 
        WHERE  b.clientid = a.clientid 
               AND b.typeb = 1) > 1 

查看它是否在sql server中工作:

select *
from client
qualify sum()over(partition by clientId, typeB)>1
另一个简单的方法是

select *
from client
where clientId in 
(select clientId 
from client
having count(*)>1)

查看它是否在sql server中工作:

select *
from client
qualify sum()over(partition by clientId, typeB)>1
另一个简单的方法是

select *
from client
where clientId in 
(select clientId 
from client
having count(*)>1)
你可以试试这个

;with cte as (
select clientid, typeb, row_number() over(partition by clientid, typeb order by clientid) as rn
from yourtable ) select clientid, typeb from cte where rn > 1
你可以试试这个

;with cte as (
select clientid, typeb, row_number() over(partition by clientid, typeb order by clientid) as rn
from yourtable ) select clientid, typeb from cte where rn > 1

我想你需要这样的东西

SELECT COUNT(ClientID), ClientID, TypeB
FROM tblA
WHERE TypeB = 1
GROUP BY ClientID, TypeB
HAVING COUNT(ClientID) > 1

我想你需要这样的东西

SELECT COUNT(ClientID), ClientID, TypeB
FROM tblA
WHERE TypeB = 1
GROUP BY ClientID, TypeB
HAVING COUNT(ClientID) > 1

我知道您已经接受了答案,但我要指出的是,您实际上只需要在查询中反转
having
groupby
TypeB
上的分组不是解决方案的必要部分。@shawnt00-我注意到它足够接近了。但有时我试图构建一个查询,却不断在sql server中发现错误,这很烦人,但我很接近。非常感谢。我知道您已经接受了答案,但我要指出的是,您实际上只需要在查询中反转
having
groupby
TypeB
上的分组不是解决方案的必要部分。@shawnt00-我注意到它足够接近了。但有时我试图构建一个查询,却不断在sql server中发现错误,这很烦人,但我很接近。非常感谢。