Buongiorno, ho bisogno di aiuto.

Semplifico notevolmente la mia situazione in modo da ragionare in modo semplice.

Ho 3 tabelle:

1) "Anagrafica" - composta dal "Codice"
2) "Movimenti" - composta dal "Codice", "DataMov", "Valore"
3) "Parametri" - composta da "Var", "Mese1", "Mese2", "Mese3"

ad oggi ho una cosa del genere:



codice:
SELECT a.Codice,
		ISNULL((ROUND((cast((select count(b.Codice)
		from dbo.Movimenti b
		WHERE b.Valore < (select Var from Parametri) 
			and a.Codice = b.Codice and
			(month(b.DataMov) = (select Mese1 from Parametri) Or
		 	 month(b.DataMov) = (select Mese2 from Parametri) Or
			 month(b.DataMov) = (select Mese3 from Parametri))
			) as decimal(10,2)) /
		cast((select count(b.id_collaborateur) 
		from dbo._Vista_f_events_all b
		where a.Codice = b.Codice and
			(month(b.DataMov) = (select Mese1 from Parametri) Or
		 	 month(b.DataMov) = (select Mese2 from Parametri) Or
			 month(b.DataMov) = (select Mese3 from Parametri))
			) as decimal(10,2))),4)), 0) AS ValoreTrimestre
	FROM dbo.Anagrafica a
	ORDER BY a.Codice
La cosa che porta via una enormità di tempo all'elaborazione è il pezzo dove reperisce i mesi "select Mese1 from Parametri" per la selezione.

Pensavo ad una soluzione tipo


codice:
SELECT a.Codice, 
           (select Mese1 from Parametri) AS Mese1,
           (select Mese2 from Parametri) AS Mese2,
           (select Mese3 from Parametri) AS Mese3,
		ISNULL((ROUND((cast((select count(b.Codice)
		from dbo.Movimenti b
		WHERE b.Valore < (select Var from Parametri) 
			and a.Codice = b.Codice and
			(month(b.DataMov) = Mese1 Or
		 	 month(b.DataMov) = Mese2 Or
			 month(b.DataMov) = Mese3)
			) as decimal(10,2)) /
		cast((select count(b.id_collaborateur) 
		from dbo._Vista_f_events_all b
		where a.Codice = b.Codice and
			(month(b.DataMov) = Mese1 Or
		 	 month(b.DataMov) = Mese2 Or
			 month(b.DataMov) = Mese3)
			) as decimal(10,2))),4)), 0) AS ValoreTrimestre
	FROM dbo.Anagrafica a
	ORDER BY a.Codice
ma purtroppo non mi fa usare le colonne Mese1, Mese2, mese3



AIUTO!

Earthquake