Stack Overflow en español Asked by Gian Carlo on November 19, 2020
Buenas estoy tratando de mostrar los resultados que no coinciden de 2 columnas, el problema con la consulta que armé es que, cuando algo hace match se lleva todos los match y solo estaria necesitando que haga match el primero que pueda.
La consulta como está ahora es esta:
SELECT ID, COLUMN1, COLUMN2
FROM TABLEX AS A
WHERE ID NOT IN(SELECT ID FROM TABLEX AS B
WHERE A.ID = B.ID AND A.COLUMN1 = B.COLUMN2)
Este es un ejemplo de lo que tengo:
INPUT:
ID COLUMN1 COLUMN2
-------------------------------
1 6000 0
1 1000 0
1 2000 0
1 1000 0
1 0 1000
1 3000 3000
1 0 2000
1 4000 0
1 0 5000
1 0 6000
OUTPUT ACTUAL:
ID COLUMN1 COLUMN2
-------------------------------
1 4000 0
1 0 5000
OUTPUT ESPERADO:
ID COLUMN1 COLUMN2
-------------------------------
1 1000 0
1 4000 0
1 0 5000
Qué podria agregarle a la query para corregir esto?
Para SqlServer:
SELECT TOP 1 ID, COLUMN1, COLUMN2
FROM TABLEX AS A
WHERE ID NOT IN (SELECT ID FROM TABLEX AS B
WHERE A.ID = B.ID AND A.COLUMN1 = B.COLUMN2);
Para MySQL y PostgreSQL:
SELECT ID, COLUMN1, COLUMN2
FROM TABLEX AS A
WHERE ID NOT IN (SELECT ID FROM TABLEX AS B
WHERE A.ID = B.ID AND A.COLUMN1 = B.COLUMN2)
LIMIT 1;
Para ORACLE:
SELECT ID, COLUMN1, COLUMN2
FROM TABLEX AS A
WHERE ID NOT IN (SELECT ID FROM TABLEX AS B
WHERE A.ID = B.ID AND A.COLUMN1 = B.COLUMN2)
AND ROWNUM<=1;
Answered by Mauricio Ortega on November 19, 2020
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP