One possibility is to use the analytical function FIRST_VALUE()
to returns the first value in an ordered set of values partitioned by the data of a specified column.
Data set for the test:
CREATE TABLE VW_DM_REPORTS(
CODIGO_PARCEIRO VARCHAR(14), NOME_PARCEIRO VARCHAR(30)
);
INSERT INTO VW_DM_REPORTS VALUES (N'27179922000164', N'LUCENA TELECOM');
INSERT INTO VW_DM_REPORTS VALUES (N'27179922000164', N'LUCENA TELECOM');
INSERT INTO VW_DM_REPORTS VALUES (N'02570688000170', N'BRASIL TELECOM S/A');
INSERT INTO VW_DM_REPORTS VALUES (N'27179922000164', N'LUCENA TELECOM EIRELI');
INSERT INTO VW_DM_REPORTS VALUES (N'27179922000164', N'LUCENA TELECOM EIRELI');
INSERT INTO VW_DM_REPORTS VALUES (N'27179922000164', N'LUCENA TELECOM EIRELI');
INSERT INTO VW_DM_REPORTS VALUES (N'27179922000164', N'LUCENA TELECOM');
INSERT INTO VW_DM_REPORTS VALUES (N'02570688000170', N'BRASIL TELECOM');
Example 1:
SELECT CODIGO_PARCEIRO,
FIRST_VALUE(NOME_PARCEIRO) OVER (
PARTITION BY CODIGO_PARCEIRO
ORDER BY NOME_PARCEIRO ASC
)
FROM VW_DM_REPORTS
WHERE CODIGO_PARCEIRO = '27179922000164'
Example 2:
SELECT CODIGO_PARCEIRO,
FIRST_VALUE(NOME_PARCEIRO) OVER (
PARTITION BY CODIGO_PARCEIRO
ORDER BY NOME_PARCEIRO ASC
)
FROM VW_DM_REPORTS
Without the structure of your tables and sample data it is very difficult to opine in solving your problem
– Sorack