Ver Mensaje Individual
  #13 (permalink)  
Antiguo 24/09/2014, 08:24
aragonexiste
 
Fecha de Ingreso: abril-2005
Mensajes: 16
Antigüedad: 19 años, 9 meses
Puntos: 0
Respuesta: Consulta SQL para realizar vista de arbol

Cita:
Iniciado por Libras Ver Mensaje
Código SQL:
Ver original
  1. CREATE TABLE #temp
  2. (
  3. area VARCHAR(20),
  4. subarea VARCHAR(50),
  5. cp VARCHAR(20),
  6. numero INT,
  7. extra VARCHAR(20)
  8. )
  9.  
  10. INSERT INTO #temp VALUES ('Brabante','Nord Eindhoven','1186VZ',5,'')
  11. INSERT INTO #temp VALUES ('Brabante','Nord Eindhoven','1186VZ',7,'')
  12. INSERT INTO #temp VALUES ('Brabante','Nord Eindhoven','1186VZ',9,'')
  13. INSERT INTO #temp VALUES ('Brabante','Nord Eindhoven','1186VZ',9,'A')
  14. INSERT INTO #temp VALUES ('Brabante','Nord Eindhoven','1186VZ',11,'')
  15. INSERT INTO #temp VALUES ('Brabante','Zuid Eindhoven','1195AS',232,'')
  16.  
  17. CREATE TABLE #temp2
  18. (
  19. cp VARCHAR(20),
  20. numero INT,
  21. extra VARCHAR(20),
  22. STATUS VARCHAR(20)
  23. )
  24.  
  25. INSERT INTO #temp2 VALUES ('1186VZ',5,'','Completed')
  26. INSERT INTO #temp2 VALUES ('5836AB',12,'','Completed')
  27.  
  28.  
  29. SELECT area, subarea,COUNT(subarea) total ,SUM(clientes) clientes FROM(
  30. SELECT
  31. area,subarea,
  32. CASE WHEN isnull(t2.numero,0)=0 THEN 0 ELSE 1 END AS clientes
  33. FROM #temp AS t1
  34. FULL OUTER JOIN #temp2 AS t2 ON (t1.cp=t2.cp AND t1.numero=t2.numero AND t1.extra=t2.extra)
  35. ) t3
  36. WHERE area IS NOT NULL
  37. GROUP BY area,subarea

con algo como eso ;)
Hola de nuevo Libras,

Toda la parte de crear tabla e introducir los datos ha ido bien (he tenido que quitar los # delante de los nombres ya que si no el phpmyadmin no me dejaba), pero a la hora de hacer la consulta, recibo el siguiente error:

#1582 - Incorrect parameter count in the call to native function 'isnull'

Suponiendo que la funcion se escribe separada, me da el siguiente error:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'is null(t2.numero,0)=0 THEN 0 ELSE 1 END AS clientes
FROM temp AS t1
FULL OUTER ' at line 4

Muchas gracias de nuevo por tu ayuda