Ver Mensaje Individual
  #12 (permalink)  
Antiguo 05/12/2015, 09:56
Avatar de horacionomade
horacionomade
 
Fecha de Ingreso: diciembre-2015
Ubicación: Hurlingham, Buenos Aires
Mensajes: 9
Antigüedad: 8 años, 5 meses
Puntos: 0
Respuesta: Vista de datos por año para cobertura sig

Gracias al invaluable aporte y acompañamiento de gnzsoloyo ("... eso se llama tabla inversa") encontré la solución a mi problema, con el siguiente query, las ordenes "view", "case when" y "sum" logramos armar una salida csv para incorporar a una vista de un sistema de información geográfico!!

Mil gracias!!! GONZALO!!!!

Código MySQL:
Ver original
  1. /*Invertir filas en columnas*/
  2. create OR REPLACE view expedientes_historial as (
  3.  
  4.     SELECT Provincia,anio, COUNT(*) TotlaPOrPeriodo
  5.     FROM territor_pgestionbdtmp.expedientes_historicos
  6.     GROUP BY idjurisbahra, anio
  7. );
  8.  
  9. create OR REPLACE view expedientes_historial_inversa as (
  10.     expedientes_historial.*,
  11.     case when anio = "2002" then TotlaPOrPeriodo end as '2002',
  12.     case when anio = "2003" then TotlaPOrPeriodo end as '2003',
  13.     case when anio = "2005" then TotlaPOrPeriodo end as '2005',
  14.     case when anio = "2006" then TotlaPOrPeriodo end as '2006',
  15.     case when anio = "2007" then TotlaPOrPeriodo end as '2007',
  16.     case when anio = "2008" then TotlaPOrPeriodo end as '2008',
  17.     case when anio = "2009" then TotlaPOrPeriodo end as '2009',
  18.     case when anio = "2010" then TotlaPOrPeriodo end as '2010',
  19.     case when anio = "2011" then TotlaPOrPeriodo end as '2011',
  20.     case when anio = "2012" then TotlaPOrPeriodo end as '2012',
  21.     case when anio = "2013" then TotlaPOrPeriodo end as '2013',
  22.     case when anio = "2014" then TotlaPOrPeriodo end as '2014',
  23.     case when anio = "2015" then TotlaPOrPeriodo end as '2015'
  24.   from expedientes_historial
  25. order by Provincia, anio
  26. );
  27. /*
  28. Select * from expedientes_historial_inversa
  29. */
  30. create OR REPLACE view expedientes_historial_cosmetica as (
  31.     Provincia,
  32.     sum(expedientes_historial_inversa.2002) as '2002',
  33.     sum(expedientes_historial_inversa.2003) as '2003',
  34.     sum(expedientes_historial_inversa.2005) as '2005',
  35.     sum(expedientes_historial_inversa.2006) as '2006',
  36.     sum(expedientes_historial_inversa.2007) as '2007',
  37.     sum(expedientes_historial_inversa.2008) as '2008',
  38.     sum(expedientes_historial_inversa.2009) as '2009',
  39.     sum(expedientes_historial_inversa.2010) as '2010',
  40.     sum(expedientes_historial_inversa.2011) as '2011',
  41.     sum(expedientes_historial_inversa.2012) as '2012',
  42.     sum(expedientes_historial_inversa.2013) as '2013',
  43.     sum(expedientes_historial_inversa.2014) as '2014',
  44.     sum(expedientes_historial_inversa.2015) as '2015'
  45.   from expedientes_historial_inversa
  46.   group by Provincia
  47. );
  48.  
  49. Select * from expedientes_historial_cosmetica
  50.  
  51. /*FIN*/

Mil Gracias Libras!!! Gonzalo ya me orientó más que bien!!

Nos leemos!!

Última edición por horacionomade; 05/12/2015 a las 10:07