IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Langage SQL Discussion :

Requete SQL pour réaliser une SUM


Sujet :

Langage SQL

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre éclairé
    Profil pro
    Inscrit en
    Octobre 2006
    Messages
    829
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2006
    Messages : 829
    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 : Sélectionner tout - Visualiser dans une fenêtre à part
    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

  2. #2
    Rédacteur

    Avatar de SQLpro
    Homme Profil pro
    Expert bases de données / SQL / MS SQL Server / Postgresql
    Inscrit en
    Mai 2002
    Messages
    22 010
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Var (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Expert bases de données / SQL / MS SQL Server / Postgresql
    Secteur : Conseil

    Informations forums :
    Inscription : Mai 2002
    Messages : 22 010
    Billets dans le blog
    6
    Par défaut
    1) faites en une vue :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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 : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    Le site sur les SGBD relationnels et le langage SQL: http://sqlpro.developpez.com/
    Blog SQL, SQL Server, SGBDR : http://blog.developpez.com/sqlpro
    Expert Microsoft SQL Server - M.V.P. (Most valuable Professional) MS Corp.
    Entreprise SQL SPOT : modélisation, conseils, audit, optimisation, formation...
    * * * * * Expertise SQL Server : http://mssqlserver.fr/ * * * * *

  3. #3
    Membre éclairé
    Profil pro
    Inscrit en
    Octobre 2006
    Messages
    829
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2006
    Messages : 829
    Par défaut
    Merci pour votre aide, concernant les noms des colonnes c'était pour tester.

    Je regarde ça et je reviens vers vous.

  4. #4
    Membre éclairé
    Profil pro
    Inscrit en
    Octobre 2006
    Messages
    829
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2006
    Messages : 829
    Par défaut
    Peux tu m'expliquer ceci "passez par une sous requête en table dérivée.
    "?

    Merci

    guigui69

  5. #5
    Rédacteur

    Avatar de SQLpro
    Homme Profil pro
    Expert bases de données / SQL / MS SQL Server / Postgresql
    Inscrit en
    Mai 2002
    Messages
    22 010
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Var (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Expert bases de données / SQL / MS SQL Server / Postgresql
    Secteur : Conseil

    Informations forums :
    Inscription : Mai 2002
    Messages : 22 010
    Billets dans le blog
    6
    Par défaut
    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
    Le site sur les SGBD relationnels et le langage SQL: http://sqlpro.developpez.com/
    Blog SQL, SQL Server, SGBDR : http://blog.developpez.com/sqlpro
    Expert Microsoft SQL Server - M.V.P. (Most valuable Professional) MS Corp.
    Entreprise SQL SPOT : modélisation, conseils, audit, optimisation, formation...
    * * * * * Expertise SQL Server : http://mssqlserver.fr/ * * * * *

Discussions similaires

  1. Syntaxe requete SQL pour UPDATE une date
    Par fusee2013 dans le forum VBA Access
    Réponses: 4
    Dernier message: 08/08/2013, 10h50
  2. [MySQL] Requete SQL pour faire une recherche multi critères
    Par nassing dans le forum PHP & Base de données
    Réponses: 7
    Dernier message: 19/05/2011, 17h01
  3. REQUETE SQL pour faire une division
    Par slorgue dans le forum Requêtes et SQL.
    Réponses: 8
    Dernier message: 19/11/2008, 11h27
  4. Requete sql pour création de table dans une base access
    Par Ben156 dans le forum Bases de données
    Réponses: 1
    Dernier message: 17/01/2006, 22h12

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo