如何过滤表以仅检索每个记录的一次出现

发布于 2025-01-09 09:49:03 字数 1502 浏览 0 评论 0原文

我试图为每个客户只找到一次出现的情况。

但是,在我的数据库中,我的客户已添加两次(在 ERP 迁移之后)。

目前,

如果我尝试查找出现两次的客户,我必须保留“customer_id”中具有“C”的客户 在此示例

中,“Manu Johns”出现了 2 次,因此我们必须保留最终表中 customer_id 列中具有“C”的那个。

如果我只找到该客户的一次出现。但是,customer_id 列中没有“C”。我们必须按原样添加到最终表中

在这个示例中,我们有“Mathieu Wainers”,只有当我们将其保留在最终表中时,

它才会出现哪个查询将允许我得到以下结果: https://dbfiddle.uk/?rdbms=sqlserver_2019&fiddle=9484f43c0a6c1ccdae7d659ca53e1eab

CREATE TABLE PersonsInitial (
    tel int,
    firstname varchar(255),
    lastname varchar(255),
    Customer_ID varchar(255)
);
insert into PersonsInitial(tel,firstname,lastname,Customer_ID) values
('01234','Manu','Johns','456'),
('01234','Manu','Johns','C456'),
('21234','Fernand','Wajk','C389'),
('13554','Mathieu','Wainers','4683');

select distinct tel, firstname, lastname, customer_id from PersonsInitial

--if there is a person with the same tel number chose the customer id with 'C'
--if I don't have the choice add the customer without C

CREATE TABLE PersonsFinal (
    tel int,
    firstname varchar(255),
    lastname varchar(255),
    Customer_ID varchar(255)
);
insert into PersonsFinal(tel,firstname,lastname,Customer_ID) values
('01234','Manu','Johns','C456'),
('21234','Fernand','Wajk','C389'),
('13554','Mathieu','Wainers','4683');

select distinct tel, firstname, lastname, customer_id from PersonsFinal

I am trying to find only one occurrence for each customer.

However, in my database I have customers that have been added twice (following an ERP migration)

Currently,

If I try to find a customer that has two occurrences, I have to keep the customer that has a 'C' in the "customer_id" column

In this example we have "Manu Johns" who appears 2x so we must keep the one who has a 'C' in the customer_id column in the final table.

If I only find one occurrence of this customer. But, which does not have a 'C' in the customer_id column. We have to add it as is in the final table

In this example we have "Mathieu Wainers" which appears only once we keep it as it is in the final table

Which query would allow me to have this result : https://dbfiddle.uk/?rdbms=sqlserver_2019&fiddle=9484f43c0a6c1ccdae7d659ca53e1eab

CREATE TABLE PersonsInitial (
    tel int,
    firstname varchar(255),
    lastname varchar(255),
    Customer_ID varchar(255)
);
insert into PersonsInitial(tel,firstname,lastname,Customer_ID) values
('01234','Manu','Johns','456'),
('01234','Manu','Johns','C456'),
('21234','Fernand','Wajk','C389'),
('13554','Mathieu','Wainers','4683');

select distinct tel, firstname, lastname, customer_id from PersonsInitial

--if there is a person with the same tel number chose the customer id with 'C'
--if I don't have the choice add the customer without C

CREATE TABLE PersonsFinal (
    tel int,
    firstname varchar(255),
    lastname varchar(255),
    Customer_ID varchar(255)
);
insert into PersonsFinal(tel,firstname,lastname,Customer_ID) values
('01234','Manu','Johns','C456'),
('21234','Fernand','Wajk','C389'),
('13554','Mathieu','Wainers','4683');

select distinct tel, firstname, lastname, customer_id from PersonsFinal

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

青丝拂面 2025-01-16 09:49:03

您可以根据客户 ID 中是否有“C”将它们排名第一。这就是 cte 存在的原因。

with cte as (select row_number() over (partition by tel, firstname, lastname order by case when left(customer_id, 1) = 'C' then 0 else 1 end) rn, 
                    p.* 
               from PersonsInitial p)
select *
  from cte 
 where rn = 1; <-- selects only those with "C" or those for that there is no "C" lines

dbfiddle

You may rank them first based on whether it has or not "C" in the customer id. That's why cte is here for.

with cte as (select row_number() over (partition by tel, firstname, lastname order by case when left(customer_id, 1) = 'C' then 0 else 1 end) rn, 
                    p.* 
               from PersonsInitial p)
select *
  from cte 
 where rn = 1; <-- selects only those with "C" or those for that there is no "C" lines

dbfiddle

稀香 2025-01-16 09:49:03

这个问题有多种解决方案。例如,您可以使用 OUTER APPLY。即:

insert into PersonsFinal(tel,firstname,lastname,Customer_ID) 
select distinct pi1.tel, pi1.firstname, pi1.lastname, coalesce(pi.Customer_ID, pi1.Customer_ID) Customer_Id 
from PersonsInitial pi1
outer apply (select top(1) * 
     from PersonsInitial pi2 
     where pi1.tel = pi2.tel 
         and pi1.firstname = pi2.firstname 
         and pi1.lastname = pi2.lastname
         and pi2.Customer_ID like 'C%') pi;

DBFiddle 演示

There are multiple solutions to this problem. You can, for example use OUTER APPLY. ie:

insert into PersonsFinal(tel,firstname,lastname,Customer_ID) 
select distinct pi1.tel, pi1.firstname, pi1.lastname, coalesce(pi.Customer_ID, pi1.Customer_ID) Customer_Id 
from PersonsInitial pi1
outer apply (select top(1) * 
     from PersonsInitial pi2 
     where pi1.tel = pi2.tel 
         and pi1.firstname = pi2.firstname 
         and pi1.lastname = pi2.lastname
         and pi2.Customer_ID like 'C%') pi;

DBFiddle demo

北方的韩爷 2025-01-16 09:49:03

另一个解决方案:

WITH CTE AS
(
SELECT tel, 
       firstname, 
       lastname, 
       Customer_ID, 
       ROW_NUMBER() OVER (PARTITION BY Customer_ID ORDER BY COL_LENGTH('PersonsInitial','Customer_ID') DESC) AS RowNumber 
FROM   PersonsInitial
)

SELECT tel, 
       firstname, 
       lastname, 
       Customer_ID
FROM CTE 
WHERE RowNumber = 1

Another solution :

WITH CTE AS
(
SELECT tel, 
       firstname, 
       lastname, 
       Customer_ID, 
       ROW_NUMBER() OVER (PARTITION BY Customer_ID ORDER BY COL_LENGTH('PersonsInitial','Customer_ID') DESC) AS RowNumber 
FROM   PersonsInitial
)

SELECT tel, 
       firstname, 
       lastname, 
       Customer_ID
FROM CTE 
WHERE RowNumber = 1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文