SQL 根据特定条件查询显示唯一记录
SQL Query to display unique records based on certain conditions
[在发布这个问题之前我经历了大量的问题。]
我有一个 table,其中包含 4 个字段。它是 ClientId、ClientName、ClientAddress、ClientCity。
现在,我有一个自动完成的文本框控件,我需要在其中获取和显示客户端名称。
问题是在我们的数据库中,我们有来自同一城市不同地址的相同客户。
现在给我的要求是用户应该能够看到 "ClientName" 或 "ClientName + ClientCity" 或 "ClientName+ClientCity+ClientAddress" 以方便用户 select客户.
这意味着我需要在查询中添加一列,直到它唯一。
我确信一定有一些简单的解决方案,我在过去两天都没有得到。
如下示例数据所示,如果我只向最终用户显示 "D" 作为客户端名称,他会感到困惑,因为他必须 [=44= 哪个客户端 "D" ].所以我们需要连接城市和地址以使其唯一。
我期待如下输出。
您可以为此使用 COUNT() OVER()
:
;WITH CTE AS(
SELECT *,
ByName = COUNT(*) OVER(PARTITION BY ClientName),
ByCity = COUNT(*) OVER(PARTITION BY ClientName,ClientCity)
FROM Client
)
SELECT
CASE
WHEN ByName = 1 AND ByCity = 1 THEN ClientName
WHEN ByName > 1 AND ByCity = 1 THEN ClientName + ', ' + ClientCity
WHEN ByName > 1 AND ByCity > 1 THEN ClientName + ', ' + ClientCity + ', ' + ClientAddress
END
FROM CTE
ORDER BY ClientID
结果
Client
--------------------------------------------------------
A
B
C, New York
D, London, LSE Houghton Streen London WC2A 2AE
D, London, Hard Rock Cafe London 150 Old Park Lane
F
C, Paris
参见SQL Fiddle。
如果您使用的是 SQL 服务器,您可以尝试使用“+”运算符进行字符串连接,如下所示
select
ClientName + ', ' + ClientCity + ', ' + ClientAddress as ClientData,
Concat(ClientName, ', ', ClientCity, ', ', ClientAddress) Client
from client
第二个连接是使用 SQL CONCAT() funtion 构建的,将适用于 SQL Server 2012 及更高版本
对于SELECT语句后面的条件数据可以提供帮助,
select
-- ClientName + ', ' + ClientCity + ', ' + ClientAddress as ClientData,
-- Concat(ClientName, ', ', ClientCity, ', ', ClientAddress) ClientDtata2,
client =
case when count(*) over (partition by ClientName) = 1 then ClientName
else
case when count(*) over (partition by ClientName, ClientCity) = 1 then CONCAT(ClientName, ', ' , ClientCity)
else Concat(ClientName, ', ', ClientCity, ', ', ClientAddress)
end
end
from client
试试这个,
Declare @t table (clientid int identity(1,1),clientname varchar(50),clientCITY varchar(50)
,clientaddress varchar(200))
insert into @t values ('A','PARIS','DFSDFSDF'), ('C','NEW YORK','DFSDFSDF')
,('C','PARIS','WEQWEQWE'),('D','LONDON','QWE342'),('D','LONDON','21DXCXZC')
;With CTE as
(select *,ROW_NUMBER()over(partition by clientname order by clientid)rn
,ROW_NUMBER()over(partition by clientname,ClientCity order by clientid)rn1
from @T A
)
--select * from cte
select clientname+','+ clientCITY
from cte A WHERE
EXISTS(SELECT CLIENTID FROM CTE WHERE clientname=A.clientname AND RN>1 AND RN1<=1)
UNION ALL
select clientname+','+ clientaddress
from cte A WHERE
EXISTS(SELECT CLIENTID FROM CTE WHERE clientname=A.clientname AND RN1>1)
UNION ALL
select clientname
from cte A WHERE not
EXISTS (SELECT CLIENTID FROM CTE WHERE clientname=A.clientname AND RN>1 )
[在发布这个问题之前我经历了大量的问题。]
我有一个 table,其中包含 4 个字段。它是 ClientId、ClientName、ClientAddress、ClientCity。
现在,我有一个自动完成的文本框控件,我需要在其中获取和显示客户端名称。
问题是在我们的数据库中,我们有来自同一城市不同地址的相同客户。
现在给我的要求是用户应该能够看到 "ClientName" 或 "ClientName + ClientCity" 或 "ClientName+ClientCity+ClientAddress" 以方便用户 select客户.
这意味着我需要在查询中添加一列,直到它唯一。
我确信一定有一些简单的解决方案,我在过去两天都没有得到。
如下示例数据所示,如果我只向最终用户显示 "D" 作为客户端名称,他会感到困惑,因为他必须 [=44= 哪个客户端 "D" ].所以我们需要连接城市和地址以使其唯一。
我期待如下输出。
您可以为此使用 COUNT() OVER()
:
;WITH CTE AS(
SELECT *,
ByName = COUNT(*) OVER(PARTITION BY ClientName),
ByCity = COUNT(*) OVER(PARTITION BY ClientName,ClientCity)
FROM Client
)
SELECT
CASE
WHEN ByName = 1 AND ByCity = 1 THEN ClientName
WHEN ByName > 1 AND ByCity = 1 THEN ClientName + ', ' + ClientCity
WHEN ByName > 1 AND ByCity > 1 THEN ClientName + ', ' + ClientCity + ', ' + ClientAddress
END
FROM CTE
ORDER BY ClientID
结果
Client
--------------------------------------------------------
A
B
C, New York
D, London, LSE Houghton Streen London WC2A 2AE
D, London, Hard Rock Cafe London 150 Old Park Lane
F
C, Paris
参见SQL Fiddle。
如果您使用的是 SQL 服务器,您可以尝试使用“+”运算符进行字符串连接,如下所示
select
ClientName + ', ' + ClientCity + ', ' + ClientAddress as ClientData,
Concat(ClientName, ', ', ClientCity, ', ', ClientAddress) Client
from client
第二个连接是使用 SQL CONCAT() funtion 构建的,将适用于 SQL Server 2012 及更高版本
对于SELECT语句后面的条件数据可以提供帮助,
select
-- ClientName + ', ' + ClientCity + ', ' + ClientAddress as ClientData,
-- Concat(ClientName, ', ', ClientCity, ', ', ClientAddress) ClientDtata2,
client =
case when count(*) over (partition by ClientName) = 1 then ClientName
else
case when count(*) over (partition by ClientName, ClientCity) = 1 then CONCAT(ClientName, ', ' , ClientCity)
else Concat(ClientName, ', ', ClientCity, ', ', ClientAddress)
end
end
from client
试试这个,
Declare @t table (clientid int identity(1,1),clientname varchar(50),clientCITY varchar(50)
,clientaddress varchar(200))
insert into @t values ('A','PARIS','DFSDFSDF'), ('C','NEW YORK','DFSDFSDF')
,('C','PARIS','WEQWEQWE'),('D','LONDON','QWE342'),('D','LONDON','21DXCXZC')
;With CTE as
(select *,ROW_NUMBER()over(partition by clientname order by clientid)rn
,ROW_NUMBER()over(partition by clientname,ClientCity order by clientid)rn1
from @T A
)
--select * from cte
select clientname+','+ clientCITY
from cte A WHERE
EXISTS(SELECT CLIENTID FROM CTE WHERE clientname=A.clientname AND RN>1 AND RN1<=1)
UNION ALL
select clientname+','+ clientaddress
from cte A WHERE
EXISTS(SELECT CLIENTID FROM CTE WHERE clientname=A.clientname AND RN1>1)
UNION ALL
select clientname
from cte A WHERE not
EXISTS (SELECT CLIENTID FROM CTE WHERE clientname=A.clientname AND RN>1 )