SQL - Optimizar y crear bucle sql

 
Vista:
Imágen de perfil de Nacho
Val: 2
Ha disminuido su posición en 6 puestos en SQL (en relación al último mes)
Gráfica de SQL

Optimizar y crear bucle sql

Publicado por Nacho (1 intervención) el 24/07/2020 09:24:29
Hola buenas a todos soy nuevo en el foro y no se me da muy bien sql, necesitaría alguna ayuda por favor.

Me han dicho que tengo que optimizar esta consulta ya que siempre que se ejecuta el servidor se cae porque no da abasto. También me han que debo crear un bucle para ahorrar lineas.

En el bucle había pensando en un while para que se ejecute los 12 meses del año con un if para que cuando llegue al mes 12 se reinicie a 1. Ya que si ejecutamos la consulta en noviembre empezara por el mes 11, continuara por el mes 12 y tiene que pasar al mes 1, Pero siempre un total de 12 meses.

Esta seria la consulta sql:

Muchas gracias de antemano

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
SELECT clientes.codigo, clientes.denominacionFiscal,
 (SEC_TO_TIME(SUM(TIME_TO_SEC(mes082019.horaFacturables)))) AS 'FAC082019',
(SEC_TO_TIME(SUM(TIME_TO_SEC(mes082019.horaReportadas)))) AS 'REP082019',
 
 (SEC_TO_TIME(SUM(TIME_TO_SEC(mes092019.horaFacturables)))) AS 'FAC092019',
(SEC_TO_TIME(SUM(TIME_TO_SEC(mes092019.horaReportadas)))) AS 'REP092019',
 
 (SEC_TO_TIME(SUM(TIME_TO_SEC(mes102019.horaFacturables)))) AS 'FAC102019',
(SEC_TO_TIME(SUM(TIME_TO_SEC(mes102019.horaReportadas)))) AS 'REP102019',
 
 (SEC_TO_TIME(SUM(TIME_TO_SEC(mes112019.horaFacturables)))) AS 'FAC112019',
(SEC_TO_TIME(SUM(TIME_TO_SEC(mes112019.horaReportadas)))) AS 'REP112019',
 
 (SEC_TO_TIME(SUM(TIME_TO_SEC(mes122019.horaFacturables)))) AS 'FAC122019',
(SEC_TO_TIME(SUM(TIME_TO_SEC(mes122019.horaReportadas)))) AS 'REP122019',
 
 (SEC_TO_TIME(SUM(TIME_TO_SEC(mes012020.horaFacturables)))) AS 'FAC012020',
(SEC_TO_TIME(SUM(TIME_TO_SEC(mes012020.horaReportadas)))) AS 'REP012020',
 
 (SEC_TO_TIME(SUM(TIME_TO_SEC(mes022020.horaFacturables)))) AS 'FAC022020',
(SEC_TO_TIME(SUM(TIME_TO_SEC(mes022020.horaReportadas)))) AS 'REP022020',
 
 (SEC_TO_TIME(SUM(TIME_TO_SEC(mes032020.horaFacturables)))) AS 'FAC032020',
(SEC_TO_TIME(SUM(TIME_TO_SEC(mes032020.horaReportadas)))) AS 'REP032020',
 
 (SEC_TO_TIME(SUM(TIME_TO_SEC(mes042020.horaFacturables)))) AS 'FAC042020',
(SEC_TO_TIME(SUM(TIME_TO_SEC(mes042020.horaReportadas)))) AS 'REP042020',
 
 (SEC_TO_TIME(SUM(TIME_TO_SEC(mes052020.horaFacturables)))) AS 'FAC052020',
(SEC_TO_TIME(SUM(TIME_TO_SEC(mes052020.horaReportadas)))) AS 'REP052020',
 
 (SEC_TO_TIME(SUM(TIME_TO_SEC(mes062020.horaFacturables)))) AS 'FAC062020',
(SEC_TO_TIME(SUM(TIME_TO_SEC(mes062020.horaReportadas)))) AS 'REP062020',
 (SEC_TO_TIME(SUM(TIME_TO_SEC(mes072020.horaFacturables)))) AS 'FAC072020',
(SEC_TO_TIME(SUM(TIME_TO_SEC(mes072020.horaReportadas)))) AS 'REP072020',
 
 usuarios.nombreCompleto AS nombreCompleto, clientes.localidad AS localidad, clientes.idprovincias AS provincia
FROM clientes
INNER JOIN usuarios ON usuarios.id= clientes.idConsultor
LEFT JOIN (
SELECT id_cust,customer.cliente,horaFacturables, horaReportadas
FROM customer
WHERE gender in (1,2) AND MONTH(fecha) = 08 AND YEAR(fecha)= 2019) mes082019 ON clientes.codigo = mes082019.cliente
LEFT JOIN (
SELECT id_cust,customer.cliente,horaFacturables, horaReportadas
FROM customer
WHERE gender in (1,2) AND MONTH(fecha) = 09 AND YEAR(fecha)= 2019) mes092019 ON clientes.codigo = mes082019.cliente
LEFT JOIN (
SELECT id_cust,customer.cliente,horaFacturables, horaReportadas
FROM customer
WHERE gender in (1,2) AND MONTH(fecha) = 10 AND YEAR(fecha)= 2019) mes102019 ON clientes.codigo = mes102019.cliente
LEFT JOIN (
SELECT id_cust,customer.cliente,horaFacturables, horaReportadas
FROM customer
WHERE gender in (1,2) AND MONTH(fecha) = 11 AND YEAR(fecha)= 2019) mes112019 ON clientes.codigo = mes112019.cliente
LEFT JOIN (
SELECT id_cust,customer.cliente,horaFacturables, horaReportadas
FROM customer
WHERE gender in (1,2) AND MONTH(fecha) = 12 AND YEAR(fecha)= 2019) mes122019 ON clientes.codigo = mes122019.cliente
LEFT JOIN (
SELECT id_cust,customer.cliente,horaFacturables, horaReportadas
FROM customer
WHERE gender in (1,2) AND MONTH(fecha) = 01 AND YEAR(fecha)= 2020) mes012020 ON clientes.codigo = mes012020.cliente
LEFT JOIN (
SELECT id_cust,customer.cliente,horaFacturables, horaReportadas
FROM customer
WHERE gender in (1,2) AND MONTH(fecha) = 02 AND YEAR(fecha)= 2020) mes022020 ON clientes.codigo = mes022020.cliente
LEFT JOIN (
SELECT id_cust,customer.cliente,horaFacturables, horaReportadas
FROM customer
WHERE gender in (1,2) AND MONTH(fecha) = 03 AND YEAR(fecha)= 2020) mes032020 ON clientes.codigo = mes032020.cliente
LEFT JOIN (
SELECT id_cust,customer.cliente,horaFacturables, horaReportadas
FROM customer
WHERE gender in (1,2) AND MONTH(fecha) = 04 AND YEAR(fecha)= 2020) mes042020 ON clientes.codigo = mes042020.cliente
LEFT JOIN (
SELECT id_cust,customer.cliente,horaFacturables, horaReportadas
FROM customer
WHERE gender in (1,2) AND MONTH(fecha) = 05 AND YEAR(fecha)= 2020) mes052020 ON clientes.codigo = mes052020.cliente
LEFT JOIN (
SELECT id_cust,customer.cliente,horaFacturables, horaReportadas
FROM customer
WHERE gender in (1,2) AND MONTH(fecha) = 06 AND YEAR(fecha)= 2020) mes062020 ON clientes.codigo = mes062020.cliente
LEFT JOIN (
SELECT id_cust,customer.cliente,horaFacturables, horaReportadas
FROM customer
WHERE gender in (1,2) AND MONTH(fecha) = 07 AND YEAR(fecha)= 2020) mes072020 ON clientes.codigo = mes072020.cliente
WHERE clientes.estatus='Alta'
GROUP BY clientes.codigo
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder