Précédent   Forum des professionnels en informatique > Bases de données > Langage SQL
Langage SQL Forum d'entraide sur le langage SQL et sur les questions liées à la conception de schéma (DDL). Cours SQL
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse Proposer ce sujet en actualité
 
Outils de la discussion
Publicité
'
Vieux 14/03/2011, 14h31   #1
Candidat au titre de Membre du Club
 
Inscription : octobre 2006
Messages : 468
Détails du profil
Informations forums :
Inscription : octobre 2006
Messages : 468
Points : 11
Points : 11
Par défaut Requete SQL pour réaliser une SUM

Bonjour à tous,

Je dispose d'une requête SQL qui affiche un ensemble d'informations :

Code :
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
 
SELECT
    tgTaller.descrip AS Atelier,
    tgEmpresa.razon AS Société,
    ttMecanico.Nombre AS Mécanicien,
    (SELECT Descrip FROM tyUsuarioEquipo
        WHERE tyUsuarioEquipo.Emp    = ttOTFac.Emp AND
                 tyUsuarioEquipo.Grupo  = ttMecanico.PresenciaGrupo AND
                 tyUsuarioEquipo.Equipo = ttMecanico.PresenciaEquipo
   ) AS descrip_equipo,
 
    ttOTCab.NumInterno,
    ttOTCab.AnoOT,
    ttOTCab.NumOT,
    ttOTAsigTiempoFac.Intervencion,
    substring(ttOTIntervencion.Descrip,1,100) AS TextoIntervencion,
    ttSeccion.Descrip,
 
    (ROUND(
        COALESCE ((SELECT SUM(TiempoPasado)
                   FROM ttOTFichado
                  WHERE ttOTFichado.Emp          = ttOTFac.Emp AND 
                              ttOTFichado.NumInterno   = ttOTFac.NumInterno AND 
                                ttOTFichado.Mecanico     = ttOTAsigTiempoFac.Mecanico AND 
                                ttOTFichado.Intervencion = ttOTCargoInt.NumIntIntervencion AND 
                            (NOT (ttOTFichado.RepartoPendiente = 0 AND ttOTFichado.FichajeContraOT > 0)  OR ttOTFichado.RepartoPendiente IS NULL) AND
                                ttOTFichado.TiempoPasado > 0)
                    ,0) *
        case ttOTCargoInt.TipoReparto
           when 0 then ttOTCargoInt.Reparto
             when 1 then ttOTCargoInt.RepartoMO
        end * 
        (1 - 2 * ttOTFac.Abono) / 100,2)
    ) AS "Temps passé",
 
    (CASE ttOTAsigTiempoFac.Mecanico
        WHEN '*' THEN
            CASE ttOTAsigTiempoFac.RepartoMecanico
                WHEN 100 THEN
                  ROUND ( ttOTAsigTiempoFac.TiempoFacturado * 
                             case ttOTCargoInt.TipoReparto
                                 when 0 then ttOTCargoInt.Reparto
                                 when 1 then ttOTCargoInt.RepartoMO
                             end *
                               (1 - 2 * ttOTFac.Abono) / 100, 4)
                ELSE 0
            END
        ELSE
          ROUND (ttOTAsigTiempoFac.TiempoMecanico  * 
                    case ttOTCargoInt.TipoReparto
                         when 0 then ttOTCargoInt.Reparto
                         when 1 then ttOTCargoInt.RepartoMO
                    end *
                    (1 - 2 * ttOTFac.Abono) / 100, 4)
     END 
    ) AS "Temps Facturé" ,
 
        ((CASE ttOTAsigTiempoFac.Mecanico
        WHEN '*' THEN
            CASE ttOTAsigTiempoFac.RepartoMecanico
                WHEN 100 THEN
                  ROUND ( ttOTAsigTiempoFac.TiempoFacturado * 
                             case ttOTCargoInt.TipoReparto
                                 when 0 then ttOTCargoInt.Reparto
                                 when 1 then ttOTCargoInt.RepartoMO
                             end *
                               (1 - 2 * ttOTFac.Abono) / 100, 4)
                ELSE 0
            END
        ELSE
          ROUND (ttOTAsigTiempoFac.TiempoMecanico  * 
                    case ttOTCargoInt.TipoReparto
                         when 0 then ttOTCargoInt.Reparto
                         when 1 then ttOTCargoInt.RepartoMO
                    end *
                    (1 - 2 * ttOTFac.Abono) / 100, 4)
     END 
    ) -     (ROUND(
        COALESCE ((SELECT SUM(TiempoPasado)
                   FROM ttOTFichado
                  WHERE ttOTFichado.Emp          = ttOTFac.Emp AND 
                              ttOTFichado.NumInterno   = ttOTFac.NumInterno AND 
                                ttOTFichado.Mecanico     = ttOTAsigTiempoFac.Mecanico AND 
                                ttOTFichado.Intervencion = ttOTCargoInt.NumIntIntervencion AND 
                            (NOT (ttOTFichado.RepartoPendiente = 0 AND ttOTFichado.FichajeContraOT > 0)  OR ttOTFichado.RepartoPendiente IS NULL) AND
                                ttOTFichado.TiempoPasado > 0)
                    ,0) *
        case ttOTCargoInt.TipoReparto
           when 0 then ttOTCargoInt.Reparto
             when 1 then ttOTCargoInt.RepartoMO
        end * 
        (1 - 2 * ttOTFac.Abono) / 100,2)
    ))AS Ecart,
 
        (((CASE ttOTAsigTiempoFac.Mecanico
        WHEN '*' THEN
            CASE ttOTAsigTiempoFac.RepartoMecanico
                WHEN 100 THEN
                  ROUND ( ttOTAsigTiempoFac.TiempoFacturado * 
                             case ttOTCargoInt.TipoReparto
                                 when 0 then ttOTCargoInt.Reparto
                                 when 1 then ttOTCargoInt.RepartoMO
                             end *
                               (1 - 2 * ttOTFac.Abono) / 100, 4)
                ELSE 0
            END
        ELSE
          ROUND (ttOTAsigTiempoFac.TiempoMecanico  * 
                    case ttOTCargoInt.TipoReparto
                         when 0 then ttOTCargoInt.Reparto
                         when 1 then ttOTCargoInt.RepartoMO
                    end *
                    (1 - 2 * ttOTFac.Abono) / 100, 4)
     END 
    ) -     (ROUND(
        COALESCE ((SELECT SUM(TiempoPasado)
                   FROM ttOTFichado
                  WHERE ttOTFichado.Emp          = ttOTFac.Emp AND 
                              ttOTFichado.NumInterno   = ttOTFac.NumInterno AND 
                                ttOTFichado.Mecanico     = ttOTAsigTiempoFac.Mecanico AND 
                                ttOTFichado.Intervencion = ttOTCargoInt.NumIntIntervencion AND 
                            (NOT (ttOTFichado.RepartoPendiente = 0 AND ttOTFichado.FichajeContraOT > 0)  OR ttOTFichado.RepartoPendiente IS NULL) AND
                                ttOTFichado.TiempoPasado > 0)
                    ,0) *
        case ttOTCargoInt.TipoReparto
           when 0 then ttOTCargoInt.Reparto
             when 1 then ttOTCargoInt.RepartoMO
        end * 
        (1 - 2 * ttOTFac.Abono) / 100,2)
    ))/ NULLIF((ROUND(
        COALESCE ((SELECT SUM(TiempoPasado)
                   FROM ttOTFichado
                  WHERE ttOTFichado.Emp          = ttOTFac.Emp AND 
                              ttOTFichado.NumInterno   = ttOTFac.NumInterno AND 
                                ttOTFichado.Mecanico     = ttOTAsigTiempoFac.Mecanico AND 
                                ttOTFichado.Intervencion = ttOTCargoInt.NumIntIntervencion AND 
                            (NOT (ttOTFichado.RepartoPendiente = 0 AND ttOTFichado.FichajeContraOT > 0)  OR ttOTFichado.RepartoPendiente IS NULL) AND
                                ttOTFichado.TiempoPasado > 0)
                    ,0) *
        case ttOTCargoInt.TipoReparto
           when 0 then ttOTCargoInt.Reparto
             when 1 then ttOTCargoInt.RepartoMO
        end * 
        (1 - 2 * ttOTFac.Abono) / 100,2)
    ),0.00))AS TEST,
 
 
    (CASE ttOTAsigTiempoFac.Mecanico
        WHEN '*' THEN
            CASE ttOTAsigTiempoFac.RepartoMecanico
                WHEN 100 THEN
                    ROUND (ttOTAsigTiempoFac.TiempoFacturado * 
                             case ttOTCargoInt.TipoReparto
                                 when 0 then ttOTCargoInt.Reparto
                                 when 1 then ttOTCargoInt.RepartoMO
                             end *
                             (1 - 2 * ttOTFac.Abono) / 100, 2)
                ELSE 0
            END
        ELSE
            ROUND (ttOTAsigTiempoFac.TiempoFacturado * 
                     case ttOTCargoInt.TipoReparto
                          when 0 then ttOTCargoInt.Reparto
                          when 1 then ttOTCargoInt.RepartoMO
                     end *
                     (1 - 2 * ttOTFac.Abono) / 100, 2)
     END 
    ) AS TiempoFacturado,
 
        ttCodigoTiempo.Descrip,
 
    ttOTAsigTiempoFac.RepartoMecanico,
    ttOTCargoInt.Reparto,
    dbo.fn_icardms_convertdatetime('1-2-2011 0:0:0.000') AS ad_desde,
    dbo.fn_icardms_convertdatetime('28-2-2011 23:59:59.000') AS ad_hasta
 
    FROM  
    ttOTFac,
    ttOTCab
        LEFT OUTER JOIN tgCortesia ON tgCortesia.Codigo = ttOTCab.Cortesia,
    ttOTIntervencion
        LEFT OUTER JOIN ttSeccion  ON ttSeccion.Seccion = ttOTIntervencion.Seccion,
    ttOTCargo
        LEFT OUTER JOIN ttCodigoTiempo  ON ttCodigoTiempo.Codigo = ttOTCargo.CodigoTiempo,
    ttOTCargoInt,
    ttOTAsigTiempoFac
        LEFT OUTER JOIN ttMecanico ON ttOTAsigTiempoFac.Emp      = ttMecanico.Emp AND
                                                ttOTAsigTiempoFac.Mecanico = ttMecanico.Mecanico,
    tgCliente
        LEFT OUTER JOIN tgCortesia tgCortesiaCargo ON tgCortesiaCargo.Codigo = tgCliente.Cortesia,
    tgEmpresa,
    tgTaller
 
WHERE 
    ttOTFac.NumInterno             = ttOTCab.NumInterno AND 
    ttOTFac.FechaFactura BETWEEN '1-2-2011 0:0:0.000' AND '28-2-2011 23:59:59.000' AND 
    ttOTCab.Emp                    = ttOTFac.Emp AND 
    ttOTCargoInt.Emp               = ttOTFac.Emp AND 
    ttOTCargoInt.NumIntOT          = ttOTFac.NumInterno AND 
    ttOTCargoInt.NumIntCargo       = ttOTFac.NumIntCargo AND 
    ttOTCargoInt.Gratuita          = 0 AND 
    ttOTAsigTiempoFac.Emp          = ttOTCab.Emp AND 
    ttOTAsigTiempoFac.NumIntOT     = ttOTCab.NumInterno AND 
    ttOTAsigTiempoFac.Intervencion = ttOTCargoInt.NumIntIntervencion AND 
    ttOTIntervencion.Emp           = ttOTFac.Emp AND 
    ttOTIntervencion.NumInterno    = ttOTFac.NumInterno AND 
    ttOTIntervencion.Intervencion  = ttOTAsigTiempoFac.Intervencion AND 
    ttOTCargo.Emp                  = ttOTFac.Emp AND 
    ttOTCargo.NumInterno           = ttOTFac.NumInterno AND 
    ttOTCargo.Cargo                = ttOTFac.NumIntCargo AND 
    tgEmpresa.Emp                  = ttOTCab.Emp AND 
    tgTaller.Emp                   = ttOTCab.Emp AND 
    tgTaller.Taller                = ttOTCab.Taller AND 
    tgCliente.Codigo               = ttOTCargo.Cliente AND
  (ttOTAsigTiempoFac.TiempoFacturado <> 0 OR
    (SELECT SUM(TiempoPasado)
      FROM ttOTFichado
     WHERE ttOTFichado.Emp          = ttOTFac.Emp AND 
              ttOTFichado.NumInterno   = ttOTFac.NumInterno AND 
              ttOTFichado.Mecanico     = ttOTAsigTiempoFac.Mecanico AND 
              ttOTFichado.Intervencion = ttOTCargoInt.NumIntIntervencion AND 
           (NOT (ttOTFichado.RepartoPendiente = 0 AND ttOTFichado.FichajeContraOT > 0)  OR ttOTFichado.RepartoPendiente IS NULL) AND
              ttOTFichado.TiempoPasado > 0) <> 0)
je voudrais arriver à adapter cette requête pour avoir la somme temps passé et temps facturé.

Car actuellement avec cette requête j'ai

Société |Atelier|Utisateur|t passé|t facturé|description
société1;A1;u1;3h;4h;test;client1
société1,A1; u1;4h;5h;tttt;client2

Je voudrais arriver a

Socité1|atelier1|utilisateur| Somme Tpassé | Somme Facturé.
Société1;A1;U1 | 7h ;9h.


Est-ce réalisable?

Les informations (de notre SI) sont récupérées depuis ms SQL server 2000

Merci

guigui69
guigui69 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 14/03/2011, 15h27   #2
Rédacteur/Modérateur

 
Avatar de SQLpro
 
Homme Frédéric BROUARD
Expert SGBDR & SQL
Inscription : mai 2002
Messages : 10 953
Détails du profil
Informations personnelles :
Nom : Homme Frédéric BROUARD
Localisation : France

Informations professionnelles :
Activité : Expert SGBDR & SQL
Secteur : Conseil

Informations forums :
Inscription : mai 2002
Messages : 10 953
Points : 17 773
Points : 17 773
1) faites en une vue :
Code :
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
CREATE VIEW dbo.V_temps
AS
SELECT
	tgTaller.descrip AS Atelier,
	tgEmpresa.razon AS Société,
	ttMecanico.Nombre AS Mécanicien,
	(SELECT Descrip FROM tyUsuarioEquipo
		WHERE tyUsuarioEquipo.Emp    = ttOTFac.Emp AND
		 		tyUsuarioEquipo.Grupo  = ttMecanico.PresenciaGrupo AND
		 		tyUsuarioEquipo.Equipo = ttMecanico.PresenciaEquipo
   ) AS descrip_equipo,
 
	ttOTCab.NumInterno,
	ttOTCab.AnoOT,
	ttOTCab.NumOT,
	ttOTAsigTiempoFac.Intervencion,
	substring(ttOTIntervencion.Descrip,1,100) AS TextoIntervencion,
	ttSeccion.Descrip,
 
	(ROUND(
		COALESCE ((SELECT SUM(TiempoPasado)
                   FROM ttOTFichado
                  WHERE ttOTFichado.Emp          = ttOTFac.Emp AND 
					      	ttOTFichado.NumInterno   = ttOTFac.NumInterno AND 
								ttOTFichado.Mecanico     = ttOTAsigTiempoFac.Mecanico AND 
								ttOTFichado.Intervencion = ttOTCargoInt.NumIntIntervencion AND 
				            (NOT (ttOTFichado.RepartoPendiente = 0 AND ttOTFichado.FichajeContraOT > 0)  OR ttOTFichado.RepartoPendiente IS NULL) AND
								ttOTFichado.TiempoPasado > 0)
				    ,0) *
		case ttOTCargoInt.TipoReparto
	   	when 0 then ttOTCargoInt.Reparto
	     	when 1 then ttOTCargoInt.RepartoMO
		end * 
		(1 - 2 * ttOTFac.Abono) / 100,2)
	) AS "Temps passé",
 
	(CASE ttOTAsigTiempoFac.Mecanico
		WHEN '*' THEN
			CASE ttOTAsigTiempoFac.RepartoMecanico
				WHEN 100 THEN
				  ROUND ( ttOTAsigTiempoFac.TiempoFacturado * 
							 case ttOTCargoInt.TipoReparto
	     						when 0 then ttOTCargoInt.Reparto
	     						when 1 then ttOTCargoInt.RepartoMO
							 end *
					  		 (1 - 2 * ttOTFac.Abono) / 100, 4)
				ELSE 0
			END
		ELSE
		  ROUND (ttOTAsigTiempoFac.TiempoMecanico  * 
					case ttOTCargoInt.TipoReparto
	     				when 0 then ttOTCargoInt.Reparto
	     				when 1 then ttOTCargoInt.RepartoMO
					end *
					(1 - 2 * ttOTFac.Abono) / 100, 4)
	 END 
	) AS "Temps Facturé" ,
 
		((CASE ttOTAsigTiempoFac.Mecanico
		WHEN '*' THEN
			CASE ttOTAsigTiempoFac.RepartoMecanico
				WHEN 100 THEN
				  ROUND ( ttOTAsigTiempoFac.TiempoFacturado * 
							 case ttOTCargoInt.TipoReparto
	     						when 0 then ttOTCargoInt.Reparto
	     						when 1 then ttOTCargoInt.RepartoMO
							 end *
					  		 (1 - 2 * ttOTFac.Abono) / 100, 4)
				ELSE 0
			END
		ELSE
		  ROUND (ttOTAsigTiempoFac.TiempoMecanico  * 
					case ttOTCargoInt.TipoReparto
	     				when 0 then ttOTCargoInt.Reparto
	     				when 1 then ttOTCargoInt.RepartoMO
					end *
					(1 - 2 * ttOTFac.Abono) / 100, 4)
	 END 
	) - 	(ROUND(
		COALESCE ((SELECT SUM(TiempoPasado)
                   FROM ttOTFichado
                  WHERE ttOTFichado.Emp          = ttOTFac.Emp AND 
					      	ttOTFichado.NumInterno   = ttOTFac.NumInterno AND 
								ttOTFichado.Mecanico     = ttOTAsigTiempoFac.Mecanico AND 
								ttOTFichado.Intervencion = ttOTCargoInt.NumIntIntervencion AND 
				            (NOT (ttOTFichado.RepartoPendiente = 0 AND ttOTFichado.FichajeContraOT > 0)  OR ttOTFichado.RepartoPendiente IS NULL) AND
								ttOTFichado.TiempoPasado > 0)
				    ,0) *
		case ttOTCargoInt.TipoReparto
	   	when 0 then ttOTCargoInt.Reparto
	     	when 1 then ttOTCargoInt.RepartoMO
		end * 
		(1 - 2 * ttOTFac.Abono) / 100,2)
	))AS Ecart,
 
		(((CASE ttOTAsigTiempoFac.Mecanico
		WHEN '*' THEN
			CASE ttOTAsigTiempoFac.RepartoMecanico
				WHEN 100 THEN
				  ROUND ( ttOTAsigTiempoFac.TiempoFacturado * 
							 case ttOTCargoInt.TipoReparto
	     						when 0 then ttOTCargoInt.Reparto
	     						when 1 then ttOTCargoInt.RepartoMO
							 end *
					  		 (1 - 2 * ttOTFac.Abono) / 100, 4)
				ELSE 0
			END
		ELSE
		  ROUND (ttOTAsigTiempoFac.TiempoMecanico  * 
					case ttOTCargoInt.TipoReparto
	     				when 0 then ttOTCargoInt.Reparto
	     				when 1 then ttOTCargoInt.RepartoMO
					end *
					(1 - 2 * ttOTFac.Abono) / 100, 4)
	 END 
	) - 	(ROUND(
		COALESCE ((SELECT SUM(TiempoPasado)
                   FROM ttOTFichado
                  WHERE ttOTFichado.Emp          = ttOTFac.Emp AND 
					      	ttOTFichado.NumInterno   = ttOTFac.NumInterno AND 
								ttOTFichado.Mecanico     = ttOTAsigTiempoFac.Mecanico AND 
								ttOTFichado.Intervencion = ttOTCargoInt.NumIntIntervencion AND 
				            (NOT (ttOTFichado.RepartoPendiente = 0 AND ttOTFichado.FichajeContraOT > 0)  OR ttOTFichado.RepartoPendiente IS NULL) AND
								ttOTFichado.TiempoPasado > 0)
				    ,0) *
		case ttOTCargoInt.TipoReparto
	   	when 0 then ttOTCargoInt.Reparto
	     	when 1 then ttOTCargoInt.RepartoMO
		end * 
		(1 - 2 * ttOTFac.Abono) / 100,2)
	))/ NULLIF((ROUND(
		COALESCE ((SELECT SUM(TiempoPasado)
                   FROM ttOTFichado
                  WHERE ttOTFichado.Emp          = ttOTFac.Emp AND 
					      	ttOTFichado.NumInterno   = ttOTFac.NumInterno AND 
								ttOTFichado.Mecanico     = ttOTAsigTiempoFac.Mecanico AND 
								ttOTFichado.Intervencion = ttOTCargoInt.NumIntIntervencion AND 
				            (NOT (ttOTFichado.RepartoPendiente = 0 AND ttOTFichado.FichajeContraOT > 0)  OR ttOTFichado.RepartoPendiente IS NULL) AND
								ttOTFichado.TiempoPasado > 0)
				    ,0) *
		case ttOTCargoInt.TipoReparto
	   	when 0 then ttOTCargoInt.Reparto
	     	when 1 then ttOTCargoInt.RepartoMO
		end * 
		(1 - 2 * ttOTFac.Abono) / 100,2)
	),0.00))AS TEST,
 
 
	(CASE ttOTAsigTiempoFac.Mecanico
		WHEN '*' THEN
			CASE ttOTAsigTiempoFac.RepartoMecanico
				WHEN 100 THEN
					ROUND (ttOTAsigTiempoFac.TiempoFacturado * 
							 case ttOTCargoInt.TipoReparto
	     						when 0 then ttOTCargoInt.Reparto
	     						when 1 then ttOTCargoInt.RepartoMO
							 end *
							 (1 - 2 * ttOTFac.Abono) / 100, 2)
				ELSE 0
			END
		ELSE
			ROUND (ttOTAsigTiempoFac.TiempoFacturado * 
					 case ttOTCargoInt.TipoReparto
	     			 	when 0 then ttOTCargoInt.Reparto
	     			 	when 1 then ttOTCargoInt.RepartoMO
					 end *
					 (1 - 2 * ttOTFac.Abono) / 100, 2)
	 END 
	) AS TiempoFacturado,
 
		ttCodigoTiempo.Descrip,
 
	ttOTAsigTiempoFac.RepartoMecanico,
    ttOTCargoInt.Reparto,
	dbo.fn_icardms_convertdatetime('1-2-2011 0:0:0.000') AS ad_desde,
	dbo.fn_icardms_convertdatetime('28-2-2011 23:59:59.000') AS ad_hasta
 
	FROM  
	ttOTFac,
	ttOTCab
		LEFT OUTER JOIN tgCortesia ON tgCortesia.Codigo = ttOTCab.Cortesia,
	ttOTIntervencion
		LEFT OUTER JOIN ttSeccion  ON ttSeccion.Seccion = ttOTIntervencion.Seccion,
	ttOTCargo
		LEFT OUTER JOIN ttCodigoTiempo  ON ttCodigoTiempo.Codigo = ttOTCargo.CodigoTiempo,
	ttOTCargoInt,
	ttOTAsigTiempoFac
		LEFT OUTER JOIN ttMecanico ON ttOTAsigTiempoFac.Emp      = ttMecanico.Emp AND
												ttOTAsigTiempoFac.Mecanico = ttMecanico.Mecanico,
	tgCliente
		LEFT OUTER JOIN tgCortesia tgCortesiaCargo ON tgCortesiaCargo.Codigo = tgCliente.Cortesia,
	tgEmpresa,
	tgTaller
 
WHERE 
	ttOTFac.NumInterno             = ttOTCab.NumInterno AND 
	ttOTFac.FechaFactura BETWEEN '1-2-2011 0:0:0.000' AND '28-2-2011 23:59:59.000' AND 
	ttOTCab.Emp                    = ttOTFac.Emp AND 
	ttOTCargoInt.Emp               = ttOTFac.Emp AND 
	ttOTCargoInt.NumIntOT          = ttOTFac.NumInterno AND 
	ttOTCargoInt.NumIntCargo       = ttOTFac.NumIntCargo AND 
	ttOTCargoInt.Gratuita          = 0 AND 
	ttOTAsigTiempoFac.Emp          = ttOTCab.Emp AND 
	ttOTAsigTiempoFac.NumIntOT     = ttOTCab.NumInterno AND 
	ttOTAsigTiempoFac.Intervencion = ttOTCargoInt.NumIntIntervencion AND 
	ttOTIntervencion.Emp           = ttOTFac.Emp AND 
	ttOTIntervencion.NumInterno    = ttOTFac.NumInterno AND 
	ttOTIntervencion.Intervencion  = ttOTAsigTiempoFac.Intervencion AND 
	ttOTCargo.Emp                  = ttOTFac.Emp AND 
	ttOTCargo.NumInterno           = ttOTFac.NumInterno AND 
	ttOTCargo.Cargo                = ttOTFac.NumIntCargo AND 
	tgEmpresa.Emp                  = ttOTCab.Emp AND 
	tgTaller.Emp                   = ttOTCab.Emp AND 
	tgTaller.Taller                = ttOTCab.Taller AND 
	tgCliente.Codigo               = ttOTCargo.Cliente AND
  (ttOTAsigTiempoFac.TiempoFacturado <> 0 OR
	(SELECT SUM(TiempoPasado)
      FROM ttOTFichado
     WHERE ttOTFichado.Emp          = ttOTFac.Emp AND 
			  ttOTFichado.NumInterno   = ttOTFac.NumInterno AND 
			  ttOTFichado.Mecanico     = ttOTAsigTiempoFac.Mecanico AND 
			  ttOTFichado.Intervencion = ttOTCargoInt.NumIntIntervencion AND 
           (NOT (ttOTFichado.RepartoPendiente = 0 AND ttOTFichado.FichajeContraOT > 0)  OR ttOTFichado.RepartoPendiente IS NULL) AND
			  ttOTFichado.TiempoPasado > 0) <> 0)
Et maintenant la requête finale :
Code :
1
2
3
4
5
SELECT Société, Atelier, Utisateur, 
       SUM("temps passé") AS Total_passe,
       SUM("temps facturé") AS Total_facture
FROM   dbo.V_temps
GROUP  BY Société, Atelier, Utisateur
Si vous ne voulez pas faire de vue passez par une sous requête en table dérivée.

Enfin, une remarque : vos nom d'objet (alias de de table et de colonne) avec des accents ou des blancs, sont une absurdité. Vous risquez de gros ennuis ultérieurement. Exemple : outil de reporting qui ne supporte pas ce genre de fantaisie....

A =
__________________
Frédéric Brouard - SQLpro - ARCHITECTE DE DONNÉES - expert SGBDR et langage SQL
Site sur les SGBD relationnels et le langage SQL: http://sqlpro.developpez.com/
Expert Microsoft SQL Server - M.V.P. (Most valuable Professional) MS Corp.
Blog SQL, SQL Server, modélisation données : http://blog.developpez.com/sqlpro
http://www.sqlspot.com : modélisation, conseils, audit, optimisation, formation
* * * * * Enseignant CNAM PACA - ISEN Toulon - CESI Aix en Provence * * * * *
SQLpro est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 14/03/2011, 15h58   #3
Candidat au titre de Membre du Club
 
Inscription : octobre 2006
Messages : 468
Détails du profil
Informations forums :
Inscription : octobre 2006
Messages : 468
Points : 11
Points : 11
Merci pour votre aide, concernant les noms des colonnes c'était pour tester.

Je regarde ça et je reviens vers vous.
guigui69 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 14/03/2011, 17h08   #4
Candidat au titre de Membre du Club
 
Inscription : octobre 2006
Messages : 468
Détails du profil
Informations forums :
Inscription : octobre 2006
Messages : 468
Points : 11
Points : 11
Peux tu m'expliquer ceci "passez par une sous requête en table dérivée.
"?

Merci

guigui69
guigui69 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 14/03/2011, 18h31   #5
Rédacteur/Modérateur

 
Avatar de SQLpro
 
Homme Frédéric BROUARD
Expert SGBDR & SQL
Inscription : mai 2002
Messages : 10 953
Détails du profil
Informations personnelles :
Nom : Homme Frédéric BROUARD
Localisation : France

Informations professionnelles :
Activité : Expert SGBDR & SQL
Secteur : Conseil

Informations forums :
Inscription : mai 2002
Messages : 10 953
Points : 17 773
Points : 17 773
SELECT *
FROM (SELECT ... ) AS T

Le contenu de la parenthèse est une sous requête en table dérivée.
Pour plus d'explication, lisez mon bouquin sur SQL :
http://sqlpro.developpez.com/booksql05/

A +
__________________
Frédéric Brouard - SQLpro - ARCHITECTE DE DONNÉES - expert SGBDR et langage SQL
Site sur les SGBD relationnels et le langage SQL: http://sqlpro.developpez.com/
Expert Microsoft SQL Server - M.V.P. (Most valuable Professional) MS Corp.
Blog SQL, SQL Server, modélisation données : http://blog.developpez.com/sqlpro
http://www.sqlspot.com : modélisation, conseils, audit, optimisation, formation
* * * * * Enseignant CNAM PACA - ISEN Toulon - CESI Aix en Provence * * * * *
SQLpro est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse Proposer ce sujet en actualité
Outils de la discussion



Fuseau horaire GMT +2. Il est actuellement 20h06.


 
 
 
 
Partenaires

Hébergement Web