如何过滤表以仅检索每个记录的一次出现
我试图为每个客户只找到一次出现的情况。
但是,在我的数据库中,我的客户已添加两次(在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以根据客户 ID 中是否有“C”将它们排名第一。这就是 cte 存在的原因。
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.
dbfiddle
这个问题有多种解决方案。例如,您可以使用 OUTER APPLY。即:
DBFiddle 演示
There are multiple solutions to this problem. You can, for example use OUTER APPLY. ie:
DBFiddle demo
另一个解决方案:
Another solution :