Ver Mensaje Individual
  #8 (permalink)  
Antiguo 06/12/2013, 00:19
Avatar de Libras
Libras
Colaborador
 
Fecha de Ingreso: agosto-2006
Ubicación: En la hermosa perla de occidente
Mensajes: 7.412
Antigüedad: 18 años, 4 meses
Puntos: 774
Respuesta: Validar registros duplicados de una columna, tomando varias posiciones de

espero que el siguiente codigo te sirva:

Código SQL:
Ver original
  1. CREATE TABLE #temp
  2. (
  3. code VARCHAR(100)
  4. )
  5.  
  6.  
  7. INSERT INTO #temp VALUES ('MARE30678965HP4')
  8. INSERT INTO #temp VALUES ('MARE30678965HP5')
  9. INSERT INTO #temp VALUES ('MARE30678965HP6')
  10. INSERT INTO #temp VALUES ('MARE30678965HP7')
  11. INSERT INTO #temp VALUES ('MARE30678965HP4')
  12. INSERT INTO #temp VALUES ('MARE30678965H')
  13. INSERT INTO #temp VALUES ('MARE3067896')
  14. INSERT INTO #temp VALUES ('MARE306789')
  15. INSERT INTO #temp VALUES ('MARE30678')
  16. INSERT INTO #temp VALUES ('MARE30678')
  17.  
  18. SELECT primero, CASE WHEN m15>1 THEN 'si' ELSE 'no' END AS m15,
  19. CASE WHEN m13>1 THEN 'si' ELSE 'no' END AS m13,
  20. CASE WHEN m11>1 THEN 'si' ELSE 'no' END AS m11,
  21. CASE WHEN m9>1 THEN 'si' ELSE 'no' END AS m9
  22. FROM(
  23. SELECT primero,SUM(m15) AS m15,SUM(m13) AS m13,SUM(m11) AS m11,SUM(m9) AS m9 FROM
  24. (
  25. SELECT primero,
  26. CASE WHEN m15='si' THEN 1 ELSE 0 END AS m15,
  27. CASE WHEN m13='si' THEN 1 ELSE 0 END AS m13,
  28. CASE WHEN m11='si' THEN 1 ELSE 0 END AS m11,
  29. CASE WHEN m9='si' THEN 1 ELSE 0 END AS m9
  30. FROM(
  31. SELECT  t1.code AS primero,t2.code AS segundo,
  32. CASE WHEN (t1.code=t2.code AND len(t1.code)=len(t2.code)) AND (len(t1.code))=15 THEN 'Si' ELSE 'No' END AS m15,
  33. CASE WHEN (t1.code=t2.code AND len(t1.code)=len(t2.code)) AND (len(t1.code))=13 THEN 'Si' ELSE 'No' END AS m13,
  34. CASE WHEN (t1.code=t2.code AND len(t1.code)=len(t2.code)) AND (len(t1.code))=11 THEN 'Si' ELSE 'No' END AS m11,
  35. CASE WHEN (t1.code=t2.code AND len(t1.code)=len(t2.code)) AND (len(t1.code))=9 THEN 'Si' ELSE 'No' END AS m9
  36.  FROM #temp AS t1
  37. , #temp AS t2
  38.  ) AS t3
  39.  ) AS total GROUP BY primero
  40.  ) AS resultado

saludos!
__________________
What does an execution plan say to t-sql query? Go f**k yourself, if you are not happy with me