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 :

Faire un test avec les données créées avec une fonction CASE


Sujet :

Langage SQL

  1. #1
    Membre à l'essai
    Homme Profil pro
    Technicien méthodes
    Inscrit en
    Décembre 2014
    Messages
    19
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 42
    Localisation : France, Calvados (Basse Normandie)

    Informations professionnelles :
    Activité : Technicien méthodes
    Secteur : Agroalimentaire - Agriculture

    Informations forums :
    Inscription : Décembre 2014
    Messages : 19
    Points : 14
    Points
    14
    Par défaut Faire un test avec les données créées avec une fonction CASE
    Bonjour,

    J'ai crée une requête dans laquelle il y a un CASE que je renomme avec un AS : la voici :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     case when datepart(weekday,ETEMPS.DateTravail)='7' and (((ETEMPS.HeureDebutExecuto*60)+ETEMPS.MinuteDebutExecuto)>1245 OR ((ETEMPS.HeureDebutCalage*60)+ETEMPS.MinuteDebutCalage)>1245) AND (datepart(iso_week,ETEMPS.DateTravail)='52' OR datepart(iso_week,ETEMPS.DateTravail)='53') then '1' 
    when datepart(weekday,ETEMPS.DateTravail)='7' and (((ETEMPS.HeureDebutExecuto*60)+ETEMPS.MinuteDebutExecuto)>1245 OR ((ETEMPS.HeureDebutCalage*60)+ETEMPS.MinuteDebutCalage)>1245) AND (datepart(iso_week,ETEMPS.DateTravail)<>'52' OR datepart(iso_week,ETEMPS.DateTravail)<>'53') then datepart(iso_week,ETEMPS.DateTravail)+1 Else datepart(iso_week,ETEMPS.DateTravail) END as Semaine_modifie
    Je voudrais faire un test sur la colonne qu'elle me ressort, mais cela ne fonctionne pas quand j'écris :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    case when datepart(MONTH,ETEMPS.DateTravail)='01' and 'Semaine_modifie'='53' then datepart(year,ETEMPS.DateTravail)-1 else datepart(year,ETEMPS.DateTravail) end as année
    Pourriez-vous m'aider?

    Merci d'avance pour vos réponses.
    Mathieu

  2. #2
    Rédacteur/Modérateur

    Avatar de Jean-Philippe André
    Homme Profil pro
    Développeur VBA/C#/VB.Net/Power Platform
    Inscrit en
    Juillet 2007
    Messages
    14 586
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : Canada

    Informations professionnelles :
    Activité : Développeur VBA/C#/VB.Net/Power Platform
    Secteur : Finance

    Informations forums :
    Inscription : Juillet 2007
    Messages : 14 586
    Points : 34 255
    Points
    34 255
    Par défaut
    Bonjour,

    es-tu certain que ta demande concerne Access ?
    Cycle de vie d'un bon programme :
    1/ ça fonctionne 2/ ça s'optimise 3/ ça se refactorise

    Pas de question technique par MP, je ne réponds pas

    Mes ouvrages :
    Apprendre à programmer avec Access 2016, Access 2019 et 2021

    Apprendre à programmer avec VBA Excel
    Prise en main de Dynamics 365 Business Central

    Pensez à consulter la FAQ Excel et la FAQ Access

    Derniers tutos
    Excel et les paramètres régionaux
    Les fichiers Excel binaires : xlsb,

    Autres tutos

  3. #3
    Membre à l'essai
    Homme Profil pro
    Technicien méthodes
    Inscrit en
    Décembre 2014
    Messages
    19
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 42
    Localisation : France, Calvados (Basse Normandie)

    Informations professionnelles :
    Activité : Technicien méthodes
    Secteur : Agroalimentaire - Agriculture

    Informations forums :
    Inscription : Décembre 2014
    Messages : 19
    Points : 14
    Points
    14
    Par défaut
    Ah oui , que NON, je viens juste de voir, non c'est du pur SQL

  4. #4
    Modérateur

    Profil pro
    dba
    Inscrit en
    Janvier 2010
    Messages
    5 643
    Détails du profil
    Informations personnelles :
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : dba

    Informations forums :
    Inscription : Janvier 2010
    Messages : 5 643
    Points : 13 092
    Points
    13 092
    Par défaut
    Bonjour,

    La clause WHERE est évaluée avant la clause SELECT, votre alias n'existe donc pas.

    Dans l'absolu, il vous faut recopier la formule dans la clause WHERE.

    En pratique, vous pouvez utiliser une CTE ou une sous requete afin de factoriser la formule. Vous pourrez alors utiliser le nom que vous aurez donné à la colonne en question dans la SELECT et dans le WHERE.

    Vous pouvez aussi éventuellement passer par une fonction.

    [edit] il semble que ce ne soit pas dans la clause WHERE mais à un autre endroit de la clause SELECT que vous voulez utiliser l'alias !? ce n'est pas possible non plus, les solutions sont les mêmes que proposées au dessus...[/edit]

  5. #5
    Membre à l'essai
    Homme Profil pro
    Technicien méthodes
    Inscrit en
    Décembre 2014
    Messages
    19
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 42
    Localisation : France, Calvados (Basse Normandie)

    Informations professionnelles :
    Activité : Technicien méthodes
    Secteur : Agroalimentaire - Agriculture

    Informations forums :
    Inscription : Décembre 2014
    Messages : 19
    Points : 14
    Points
    14
    Par défaut
    Merci de m'avoir répondu, pour que cela soit plus clair je vous met ma requête entièrement:
    car je n'ai pas tout a fait compris votre réponse, je suis novice et j'apprends au fur et à mesure .

    en vert c'est le rappel et en vert pale c'est le case.

    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
    SELECT ETEMPS.NoEnregistrement, ETEMPS.DateTravail,
    case when datepart(MONTH,ETEMPS.DateTravail)='01' and 'Semaine_modifie'='53' then datepart(year,ETEMPS.DateTravail)-1 else datepart(year,ETEMPS.DateTravail) end as année,
     datepart(weekday,ETEMPS.DateTravail) Jours_C, datename(weekday,ETEMPS.DateTravail) Jours_N, datepart(iso_week,ETEMPS.DateTravail) Semaine,
     case when datepart(weekday,ETEMPS.DateTravail)='7' and (((ETEMPS.HeureDebutExecuto*60)+ETEMPS.MinuteDebutExecuto)>1245 OR ((ETEMPS.HeureDebutCalage*60)+ETEMPS.MinuteDebutCalage)>1245) AND (datepart(iso_week,ETEMPS.DateTravail)='52' OR datepart(iso_week,ETEMPS.DateTravail)='53') then '1' 
    when datepart(weekday,ETEMPS.DateTravail)='7' and (((ETEMPS.HeureDebutExecuto*60)+ETEMPS.MinuteDebutExecuto)>1245 OR ((ETEMPS.HeureDebutCalage*60)+ETEMPS.MinuteDebutCalage)>1245) AND (datepart(iso_week,ETEMPS.DateTravail)<>'52' OR datepart(iso_week,ETEMPS.DateTravail)<>'53') then datepart(iso_week,ETEMPS.DateTravail)+1 Else datepart(iso_week,ETEMPS.DateTravail) END as Semaine_modifie
    , case when SUBSTRING(ARTICLE.CodeArticle,1,1)='Z' then SUBSTRING(ARTICLE.CodeArticle,2,12) Else ARTICLE.CodeArticle END as CodeArticle_modifié, case
    when SUBSTRING((case when SUBSTRING(ARTICLE.CodeArticle,1,1)='Z' then SUBSTRING(ARTICLE.CodeArticle,2,12) Else ARTICLE.CodeArticle END),1,1)='N' then '3' 
    when SUBSTRING((case when SUBSTRING(ARTICLE.CodeArticle,1,1)='Z' then SUBSTRING(ARTICLE.CodeArticle,2,12) Else ARTICLE.CodeArticle END),1,2)='XS' then '1' 
    when SUBSTRING((case when SUBSTRING(ARTICLE.CodeArticle,1,1)='Z' then SUBSTRING(ARTICLE.CodeArticle,2,12) Else ARTICLE.CodeArticle END),1,2)='XF' then '1' 
    when SUBSTRING((case when SUBSTRING(ARTICLE.CodeArticle,1,1)='Z' then SUBSTRING(ARTICLE.CodeArticle,2,12) Else ARTICLE.CodeArticle END),1,2)='XM' then '1' 
    when SUBSTRING((case when SUBSTRING(ARTICLE.CodeArticle,1,1)='Z' then SUBSTRING(ARTICLE.CodeArticle,2,12) Else ARTICLE.CodeArticle END),1,3)='XL5' then '5' 
    when SUBSTRING((case when SUBSTRING(ARTICLE.CodeArticle,1,1)='Z' then SUBSTRING(ARTICLE.CodeArticle,2,12) Else ARTICLE.CodeArticle END),1,4)='LNEI' then '1' 
    when SUBSTRING((case when SUBSTRING(ARTICLE.CodeArticle,1,1)='Z' then SUBSTRING(ARTICLE.CodeArticle,2,12) Else ARTICLE.CodeArticle END),1,3)='XLZ' then '12' 
    when SUBSTRING((case when SUBSTRING(ARTICLE.CodeArticle,1,1)='Z' then SUBSTRING(ARTICLE.CodeArticle,2,12) Else ARTICLE.CodeArticle END),1,3)='FPZ' then '1' 
    when SUBSTRING((case when SUBSTRING(ARTICLE.CodeArticle,1,1)='Z' then SUBSTRING(ARTICLE.CodeArticle,2,12) Else ARTICLE.CodeArticle END),1,1)='E' then '1' 
    when SUBSTRING((case when SUBSTRING(ARTICLE.CodeArticle,1,1)='Z' then SUBSTRING(ARTICLE.CodeArticle,2,12) Else ARTICLE.CodeArticle END),1,1)='S' then '1' 
    when SUBSTRING((case when SUBSTRING(ARTICLE.CodeArticle,1,1)='Z' then SUBSTRING(ARTICLE.CodeArticle,2,12) Else ARTICLE.CodeArticle END),1,1)='F' then '1' 
    when SUBSTRING((case when SUBSTRING(ARTICLE.CodeArticle,1,1)='Z' then SUBSTRING(ARTICLE.CodeArticle,2,12) Else ARTICLE.CodeArticle END),1,1)='M' then '1' 
    when SUBSTRING((case when SUBSTRING(ARTICLE.CodeArticle,1,1)='Z' then SUBSTRING(ARTICLE.CodeArticle,2,12) Else ARTICLE.CodeArticle END),10,1)='3' then '3' 
    when SUBSTRING((case when SUBSTRING(ARTICLE.CodeArticle,1,1)='Z' then SUBSTRING(ARTICLE.CodeArticle,2,12) Else ARTICLE.CodeArticle END),10,1)='4' then '4' 
    when SUBSTRING((case when SUBSTRING(ARTICLE.CodeArticle,1,1)='Z' then SUBSTRING(ARTICLE.CodeArticle,2,12) Else ARTICLE.CodeArticle END),10,1)='Y' then '32' 
    when SUBSTRING((case when SUBSTRING(ARTICLE.CodeArticle,1,1)='Z' then SUBSTRING(ARTICLE.CodeArticle,2,12) Else ARTICLE.CodeArticle END),10,1)='N' then '16' 
    when SUBSTRING((case when SUBSTRING(ARTICLE.CodeArticle,1,1)='Z' then SUBSTRING(ARTICLE.CodeArticle,2,12) Else ARTICLE.CodeArticle END),10,1)='Z' then '12' 
    when SUBSTRING((case when SUBSTRING(ARTICLE.CodeArticle,1,1)='Z' then SUBSTRING(ARTICLE.CodeArticle,2,12) Else ARTICLE.CodeArticle END),10,1)='X' then '18' 
    when SUBSTRING((case when SUBSTRING(ARTICLE.CodeArticle,1,1)='Z' then SUBSTRING(ARTICLE.CodeArticle,2,12) Else ARTICLE.CodeArticle END),10,1)='M' then '9' 
    when SUBSTRING((case when SUBSTRING(ARTICLE.CodeArticle,1,1)='Z' then SUBSTRING(ARTICLE.CodeArticle,2,12) Else ARTICLE.CodeArticle END),10,1)='W' then '48' 
    when SUBSTRING((case when SUBSTRING(ARTICLE.CodeArticle,1,1)='Z' then SUBSTRING(ARTICLE.CodeArticle,2,12) Else ARTICLE.CodeArticle END),10,1)='V' then '24' 
    when SUBSTRING((case when SUBSTRING(ARTICLE.CodeArticle,1,1)='Z' then SUBSTRING(ARTICLE.CodeArticle,2,12) Else ARTICLE.CodeArticle END),10,1)='S' then '20' 
    when SUBSTRING((case when SUBSTRING(ARTICLE.CodeArticle,1,1)='Z' then SUBSTRING(ARTICLE.CodeArticle,2,12) Else ARTICLE.CodeArticle END),10,1)='T' then '40' 
    when SUBSTRING((case when SUBSTRING(ARTICLE.CodeArticle,1,1)='Z' then SUBSTRING(ARTICLE.CodeArticle,2,12) Else ARTICLE.CodeArticle END),10,1)='L' then '21' 
    when SUBSTRING((case when SUBSTRING(ARTICLE.CodeArticle,1,1)='Z' then SUBSTRING(ARTICLE.CodeArticle,2,12) Else ARTICLE.CodeArticle END),10,1)='R' then '46' 
    when SUBSTRING((case when SUBSTRING(ARTICLE.CodeArticle,1,1)='Z' then SUBSTRING(ARTICLE.CodeArticle,2,12) Else ARTICLE.CodeArticle END),10,1)='Q' then '30' 
    when SUBSTRING((case when SUBSTRING(ARTICLE.CodeArticle,1,1)='Z' then SUBSTRING(ARTICLE.CodeArticle,2,12) Else ARTICLE.CodeArticle END),10,1)='5' then '5' 
    when SUBSTRING((case when SUBSTRING(ARTICLE.CodeArticle,1,1)='Z' then SUBSTRING(ARTICLE.CodeArticle,2,12) Else ARTICLE.CodeArticle END),10,1)='6' then '6' 
    when SUBSTRING((case when SUBSTRING(ARTICLE.CodeArticle,1,1)='Z' then SUBSTRING(ARTICLE.CodeArticle,2,12) Else ARTICLE.CodeArticle END),10,1)='8' then '8' 
    when SUBSTRING((case when SUBSTRING(ARTICLE.CodeArticle,1,1)='Z' then SUBSTRING(ARTICLE.CodeArticle,2,12) Else ARTICLE.CodeArticle END),10,1)='9' then '9' 
    when SUBSTRING((case when SUBSTRING(ARTICLE.CodeArticle,1,1)='Z' then SUBSTRING(ARTICLE.CodeArticle,2,12) Else ARTICLE.CodeArticle END),10,1)='K' then '10' 
    when SUBSTRING((case when SUBSTRING(ARTICLE.CodeArticle,1,1)='Z' then SUBSTRING(ARTICLE.CodeArticle,2,12) Else ARTICLE.CodeArticle END),10,1)='P' then '22' 
    when SUBSTRING((case when SUBSTRING(ARTICLE.CodeArticle,1,1)='Z' then SUBSTRING(ARTICLE.CodeArticle,2,12) Else ARTICLE.CodeArticle END),10,1)='J' then '52' 
    Else '99999' END as Multiple,
    CASE when SUBSTRING((case when SUBSTRING(ARTICLE.CodeArticle,1,1)='Z' then SUBSTRING(ARTICLE.CodeArticle,2,12) Else ARTICLE.CodeArticle END),1,2)='SP' then 'Fab Pochons'
    when SUBSTRING((case when SUBSTRING(ARTICLE.CodeArticle,1,1)='Z' then SUBSTRING(ARTICLE.CodeArticle,2,12) Else ARTICLE.CodeArticle END),1,1)='X' then 'Croquettes'
    when SUBSTRING((case when SUBSTRING(ARTICLE.CodeArticle,1,1)='Z' then SUBSTRING(ARTICLE.CodeArticle,2,12) Else ARTICLE.CodeArticle END),1,2)='LP' then 'Emb Pochons'
    when SUBSTRING((case when SUBSTRING(ARTICLE.CodeArticle,1,1)='Z' then SUBSTRING(ARTICLE.CodeArticle,2,12) Else ARTICLE.CodeArticle END),1,2)='FP' then 'Emb Pochons'
    when SUBSTRING((case when SUBSTRING(ARTICLE.CodeArticle,1,1)='Z' then SUBSTRING(ARTICLE.CodeArticle,2,12) Else ARTICLE.CodeArticle END),1,2)='MP' then 'Emb Pochons'
    when SUBSTRING((case when SUBSTRING(ARTICLE.CodeArticle,1,1)='Z' then SUBSTRING(ARTICLE.CodeArticle,2,12) Else ARTICLE.CodeArticle END),1,2)='F0' then 'Etiquettage'
    when SUBSTRING((case when SUBSTRING(ARTICLE.CodeArticle,1,1)='Z' then SUBSTRING(ARTICLE.CodeArticle,2,12) Else ARTICLE.CodeArticle END),1,2)='F1' then 'Etiquettage'
    when SUBSTRING((case when SUBSTRING(ARTICLE.CodeArticle,1,1)='Z' then SUBSTRING(ARTICLE.CodeArticle,2,12) Else ARTICLE.CodeArticle END),1,2)='L0' then 'Etiquettage'
    when SUBSTRING((case when SUBSTRING(ARTICLE.CodeArticle,1,1)='Z' then SUBSTRING(ARTICLE.CodeArticle,2,12) Else ARTICLE.CodeArticle END),1,2)='L1' then 'Etiquettage'
    when SUBSTRING((case when SUBSTRING(ARTICLE.CodeArticle,1,1)='Z' then SUBSTRING(ARTICLE.CodeArticle,2,12) Else ARTICLE.CodeArticle END),1,2)='M0' then 'Etiquettage'
    when SUBSTRING((case when SUBSTRING(ARTICLE.CodeArticle,1,1)='Z' then SUBSTRING(ARTICLE.CodeArticle,2,12) Else ARTICLE.CodeArticle END),1,2)='M1' then 'Etiquettage'
    when SUBSTRING((case when SUBSTRING(ARTICLE.CodeArticle,1,1)='Z' then SUBSTRING(ARTICLE.CodeArticle,2,12) Else ARTICLE.CodeArticle END),1,2)='S0' then 'Fab Barquettes'
    when SUBSTRING((case when SUBSTRING(ARTICLE.CodeArticle,1,1)='Z' then SUBSTRING(ARTICLE.CodeArticle,2,12) Else ARTICLE.CodeArticle END),1,2)='S1' then 'Fab Barquettes'
    when SUBSTRING((case when SUBSTRING(ARTICLE.CodeArticle,1,1)='Z' then SUBSTRING(ARTICLE.CodeArticle,2,12) Else ARTICLE.CodeArticle END),1,2)='SB' then 'Fab Barquettes'
    when SUBSTRING((case when SUBSTRING(ARTICLE.CodeArticle,1,1)='Z' then SUBSTRING(ARTICLE.CodeArticle,2,12) Else ARTICLE.CodeArticle END),1,2)='SR' then 'Fab Barquettes'
    when SUBSTRING((case when SUBSTRING(ARTICLE.CodeArticle,1,1)='Z' then SUBSTRING(ARTICLE.CodeArticle,2,12) Else ARTICLE.CodeArticle END),1,2)='SG' then 'Fab Barquettes'
    ELSE '99999' END as Secteur, ETEMPS.TypeLctimprod AS 'Type', ETEMPS.CodeLanctimprod AS 'Code_Lct', ARTICLE.CodeArticle, ARTICLE.NbrePiecesColis, ETEMPS.CodePoste AS 'Poste', ETEMPS.CodeOperateur AS 'Ligne', OPERGAM.Designation1 AS 'Désignation_Opération', LCTC.CodeOperation AS 'Code_Op', OPERGAM.VarNumUtil4 AS 'Cadence_SILOG', ETEMPS.DureeExecution, ETEMPS.DureeReglage, ETEMPS.DureeCalage, ETEMPS.HeureDebutExecuto, ETEMPS.MinuteDebutExecuto, ETEMPS.HeureFinExecuto, ETEMPS.MinuteFinExecuto, ETEMPS.HeureDebutReglage, ETEMPS.MinuteDebutReglage, ETEMPS.HeureFinReglage, ETEMPS.MinuteFinReglage, ETEMPS.HeureDebutCalage, ETEMPS.MinuteDebutCalage, ETEMPS.HeureFinCalage, ETEMPS.MinuteFinCalage, ETEMPS.HeuresExecuto, ETEMPS.MinutesExecuto, ETEMPS.HeuresReglage, ETEMPS.MinutesReglage, ETEMPS.HeuresCalage, ETEMPS.MinutesCalage, ETEMPS.VarAlphaUtil AS 'Equipe', ETEMPS.VarNumUtil, ETEMPS.VarNumUtil2, ETEMPS.VarNumUtil3, ETEMPS.VarNumUtil4, ETEMPS.VarNumUtil8, ETEMPS.VarNumUtil9, ETEMPS.Remarque1, ETEMPS.Remarque2
    FROM LCTE
    inner join LCTC on LCTE.CodeLancement = LCTC.CodeLancement
    inner join OPERGAM on LCTC.CodeOperation = OPERGAM.CodeOperation
    inner join ETEMPS on ETEMPS.CodeLanctimprod = LCTE.CodeLancement
    inner join ARTICLE on LCTE.CodeArticle = ARTICLE.CodeArticle
    where ETEMPS.DateTravail like '%2016%'
    order by ETEMPS.DateTravail

  6. #6
    Modérateur

    Profil pro
    dba
    Inscrit en
    Janvier 2010
    Messages
    5 643
    Détails du profil
    Informations personnelles :
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : dba

    Informations forums :
    Inscription : Janvier 2010
    Messages : 5 643
    Points : 13 092
    Points
    13 092
    Par défaut
    Comment dire...

    L'idéal serait de revoir la modélisation.

    sinon, vous pouvez comme je disais crée une vue, une CTE, ou une sous requete qui calculerait les différents éléments qui reviennent régulièrement dans votre requete, en leur donnant un petit nom. Dans la requete principale, vous réutilisez ce petit nom plutôt que la même formule maintes fois. ça rendrait la requête déjà beaucoup plus lisible

  7. #7
    Membre à l'essai
    Homme Profil pro
    Technicien méthodes
    Inscrit en
    Décembre 2014
    Messages
    19
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 42
    Localisation : France, Calvados (Basse Normandie)

    Informations professionnelles :
    Activité : Technicien méthodes
    Secteur : Agroalimentaire - Agriculture

    Informations forums :
    Inscription : Décembre 2014
    Messages : 19
    Points : 14
    Points
    14
    Par défaut
    Je comprends ce que vous me dites, mais comme je vous l'ai dis je suis novice en SQL et je ne connais pas les CTE, sous requêtes...

    Par contre quand vous dites :

    La clause WHERE est évaluée avant la clause SELECT, votre alias n'existe donc pas. = Ici je comprends le sens de fonctionnement de la lecture.

    Dans l'absolu, il vous recopier la formule dans la clause WHERE. = Par contre ici je ne comprends pas pourquoi ce que cela changera si je met ma fonction case dans le where.

    Merci

  8. #8
    Modérateur

    Profil pro
    dba
    Inscrit en
    Janvier 2010
    Messages
    5 643
    Détails du profil
    Informations personnelles :
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : dba

    Informations forums :
    Inscription : Janvier 2010
    Messages : 5 643
    Points : 13 092
    Points
    13 092
    Par défaut
    Je me suis mal exprimé, et ma confusion initiale n'a rien arrangé...

    Je ne suggérais pas de mettre vos formule dans la clause WHERE à la place de la clause [s]SELECT[/c], mais en plus :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
    SELECT CASE WHEN ...
    FROM ...
    WHERE CASE WHEN ...
    bref, ce n'est visiblement pas ce que vous cherchez à faire finalement.

    Si on reprend :
    1/ pour une requete "simple" (non imbriquée), un alias ne peut être réutilisé ni
    - dans une clause WHERE;
    - dans une clause SELECT;
    - dans les conditions de jointure (ON ...);
    - dans les clause GROUP BY, ou HAVING.

    En clair, vous ne pouvez le réutiliser que dans la clause ORDER BY.
    Il existe toutefois pour certains SGBD quelques exceptions, comme postgreSQL qui accepte les alias dans la clause GROUP BY.


    Pour les requêtes imbriquées, en plus des règles ci-dessus qui s'imposent pour un même niveau d'imbrication :
    - Vous ne pouvez jamais utiliser dans une requete imbriquée un alias définit dans un niveau supérieur
    - vous pouvez toujours utiliser un alias définit dans une requete imbriquée

    Dans votre cas, vous pourriez donc commencer par une requete qui effectue tous vos calculs intermédiaires, et l'imbriquer pour simplifier la requete globale :

    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
     
    SELECT 
        NoEnregistrement, 
        DateTravail,
        case when MoisTravail = 01 and Semaine_modifie = 53 then AnneeTravail  - 1 ELSE AnneeTravail END as Annee,
        Semaine_modifie ,
        ...
    FROM (
        SELECT 
            ETEMPS.NoEnregistrement, 
            ETEMPS.DateTravail,
            datepart(MONTH,ETEMPS.DateTravail)='01' as MoisTravail,
           datepart(year,ETEMPS.DateTravail) AS AnneeTravail ,
           case when datepart(weekday,ETEMPS.DateTravail)='7' and (((ETEMPS.HeureDebutExecuto*60)+ETEMPS.MinuteDebutExecuto)>1245 OR ((ETEMPS.HeureDebutCalage*60)+ETEMPS.MinuteDebutCalage)>1245) AND (datepart(iso_week,ETEMPS.DateTravail)='52' OR datepart(iso_week,ETEMPS.DateTravail)='53') then '1' 
    when datepart(weekday,ETEMPS.DateTravail)='7' and (((ETEMPS.HeureDebutExecuto*60)+ETEMPS.MinuteDebutExecuto)>1245 OR ((ETEMPS.HeureDebutCalage*60)+ETEMPS.MinuteDebutCalage)>1245) AND (datepart(iso_week,ETEMPS.DateTravail)<>'52' OR datepart(iso_week,ETEMPS.DateTravail)<>'53') then datepart(iso_week,ETEMPS.DateTravail)+1 Else datepart(iso_week,ETEMPS.DateTravail) END as Semaine_modifie
        ...
    ) AS T
    Vous pourriez sur le même principe embarquer la clalcul SUBSTRING(ARTICLE.CodeArticle,1,1) dans la sous requete afin de le réutiliser dans la requete principale.

    Vous pourriez même remplacer
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
     
    ...
    when SUBSTRING((case when SUBSTRING(ARTICLE.CodeArticle,1,1)='Z' then SUBSTRING(ARTICLE.CodeArticle,2,12) Else ARTICLE.CodeArticle END),1,1)='N' then '3' 
    when SUBSTRING((case when SUBSTRING(ARTICLE.CodeArticle,1,1)='Z' then SUBSTRING(ARTICLE.CodeArticle,2,12) Else ARTICLE.CodeArticle END),1,2)='XS' then '1' 
    ...
    FROM ...
    par :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
     
    ...
    when SUBSTRING(COALESCE(ArticleZ, ARTICLE.CodeArticle),1,1)='N' then '3' 
    when SUBSTRING((COALESCE(ArticleZ, ARTICLE.CodeArticle),1,2)='XS' then '1'
    ...
    FROM (
         SELECT ...
            CodeArticle
           , case when SUBSTRING(ARTICLE.CodeArticle,1,1)='Z' then SUBSTRING(ARTICLE.CodeArticle,2,12) end  AS ArticleZ--donnera null si la condition n'est pas vraie 
        FROM ...
    ) T
    Vous pourriez aussi mettre plusieurs niveaux d'imbrication pour factoriser encore plus... après c'est une question de gout pour que votre requete vous semble le plus lisible possible.

  9. #9
    Modérateur
    Avatar de al1_24
    Homme Profil pro
    Retraité
    Inscrit en
    Mai 2002
    Messages
    9 080
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 63
    Localisation : France, Val de Marne (Île de France)

    Informations professionnelles :
    Activité : Retraité
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mai 2002
    Messages : 9 080
    Points : 30 778
    Points
    30 778
    Par défaut
    Un exemple de factorisation :
    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
    with    src
        as  (   select  ETEMPS.NoEnregistrement
                    ,   ETEMPS.DateTravail
                    ,   datepart(weekday,ETEMPS.DateTravail)    as  Jours_C
                    ,   datename(weekday, ETEMPS.DateTravail)   as  Jours_N
                    ,   datepart(iso_week,ETEMPS.DateTravail)   as  Semaine
                    ,   (ETEMPS.HeureDebutExecuto * 60) + ETEMPS.MinuteDebutExecuto as  DebutExecuto
                    ,   (ETEMPS.HeureDebutCalage * 60) + ETEMPS.MinuteDebutCalage   as  DebutCalage
                    ,   case when substring(ARTICLE.CodeArticle, 1, 1) = 'Z' 
                                then substring(ARTICLE.CodeArticle, 2, 12) 
                            else ARTICLE.CodeArticle 
                        end as CodeArticle_modifié
                    ,   ETEMPS.TypeLctimprod    AS 'Type'
                    ,   ETEMPS.CodeLanctimprod  AS 'Code_Lct'
                    ,   ARTICLE.CodeArticle
                    ,   ARTICLE.NbrePiecesColis
                    ,   ETEMPS.CodePoste        AS 'Poste'
                    ,   ETEMPS.CodeOperateur    AS 'Ligne'
                    ,   OPERGAM.Designation1    AS 'Désignation_Opération'
                    ,   LCTC.CodeOperation      AS 'Code_Op'
                    ,   OPERGAM.VarNumUtil4     AS 'Cadence_SILOG'
                    ,   ETEMPS.DureeExecution
                    ,   ETEMPS.DureeReglage
                    ,   ETEMPS.DureeCalage
                    ,   ETEMPS.HeureDebutExecuto
                    ,   ETEMPS.MinuteDebutExecuto
                    ,   ETEMPS.HeureFinExecuto
                    ,   ETEMPS.MinuteFinExecuto
                    ,   ETEMPS.HeureDebutReglage
                    ,   ETEMPS.MinuteDebutReglage
                    ,   ETEMPS.HeureFinReglage
                    ,   ETEMPS.MinuteFinReglage
                    ,   ETEMPS.HeureDebutCalage
                    ,   ETEMPS.MinuteDebutCalage
                    ,   ETEMPS.HeureFinCalage
                    ,   ETEMPS.MinuteFinCalage
                    ,   ETEMPS.HeuresExecuto
                    ,   ETEMPS.MinutesExecuto
                    ,   ETEMPS.HeuresReglage
                    ,   ETEMPS.MinutesReglage
                    ,   ETEMPS.HeuresCalage
                    ,   ETEMPS.MinutesCalage
                    ,   ETEMPS.VarAlphaUtil     AS 'Equipe'
                    ,   ETEMPS.VarNumUtil
                    ,   ETEMPS.VarNumUtil2
                    ,   ETEMPS.VarNumUtil3
                    ,   ETEMPS.VarNumUtil4
                    ,   ETEMPS.VarNumUtil8
                    ,   ETEMPS.VarNumUtil9
                    ,   ETEMPS.Remarque1
                    ,   ETEMPS.Remarque2
                from    LCTE
                    inner join 
                        LCTC 
                        on  LCTE.CodeLancement  = LCTC.CodeLancement
                    inner join 
                        OPERGAM 
                        on  LCTC.CodeOperation  = OPERGAM.CodeOperation
                    inner join 
                        ETEMPS 
                        on  LCTE.CodeLancement  = ETEMPS.CodeLanctimprod
                    inner join 
                        ARTICLE 
                        on  LCTE.CodeArticle    = ARTICLE.CodeArticle
            )
    SELECT  src.NoEnregistrement
        ,   src.DateTravail
        ,   case when datepart(month, ETEMPS.DateTravail) = 1 
                    and case when src.Jours_C = 7
                                  and (   src.DebutExecuto > 1245
                                      or  src.DebutCalage) > 1245
                                      )
                                  and src.Semaine in (52, 53)
                              then 1
                            when src.Jours_C = 7
                                    and (   src.DebutExecuto > 1245
                                        or  src.DebutCalage) > 1245
                                        )
                                    and src.Semaine not in (52, 53)
                                then src.Semaine + 1
                            else src.Semaine
                        end  = 53 
                then datepart(year, ETEMPS.DateTravail) - 1 
                else datepart(year, ETEMPS.DateTravail) 
            end as  Année
        ,   src.Jours_C
        ,   src.Jours_N
        ,   src.Semaine
        ,   case when src.Jours_C = 7
                        and (   src.DebutExecuto > 1245
                            or  src.DebutCalage) > 1245
                            )
                        and src.Semaine in (52, 53)
                    then 1
                when src.Jours_C = 7
                        and (   src.DebutExecuto > 1245
                            or  src.DebutCalage) > 1245
                            )
                        and src.Semaine not in (52, 53)
                    then src.Semaine + 1
                else src.Semaine
            end as Semaine_modifie
        ,   src.CodeArticle_modifié
        ,   case 
                when substring(src.CodeArticle_modifié, 1, 1) in ('E', 'S', 'F', 'M')   then '1'
                when substring(src.CodeArticle_modifié, 1, 2) in ('XS', 'XF', 'XM')     then '1'
                when substring(src.CodeArticle_modifié, 1, 3) = 'FPZ'   then '1'
                when substring(src.CodeArticle_modifié, 1, 4) = 'LNEI'  then '1'
                when substring(src.CodeArticle_modifié, 1, 1) = 'N'     then '3'
                when substring(src.CodeArticle_modifié, 1, 3) = 'XL5'   then '5'
                when substring(src.CodeArticle_modifié, 1, 3) = 'XLZ'   then '12'
                when substring(src.CodeArticle_modifié, 10, 1) = '3' then '3'
                when substring(src.CodeArticle_modifié, 10, 1) = '4' then '4'
                when substring(src.CodeArticle_modifié, 10, 1) = '5' then '5'
                when substring(src.CodeArticle_modifié, 10, 1) = '6' then '6'
                when substring(src.CodeArticle_modifié, 10, 1) = '8' then '8' 
                when substring(src.CodeArticle_modifié, 10, 1) = '9' then '9' 
                when substring(src.CodeArticle_modifié, 10, 1) = 'M' then '9'
                when substring(src.CodeArticle_modifié, 10, 1) = 'K' then '10'
                when substring(src.CodeArticle_modifié, 10, 1) = 'N' then '16'
                when substring(src.CodeArticle_modifié, 10, 1) = 'Z' then '12'
                when substring(src.CodeArticle_modifié, 10, 1) = 'X' then '18'
                when substring(src.CodeArticle_modifié, 10, 1) = 'S' then '20'
                when substring(src.CodeArticle_modifié, 10, 1) = 'L' then '21'
                when substring(src.CodeArticle_modifié, 10, 1) = 'P' then '22'
                when substring(src.CodeArticle_modifié, 10, 1) = 'V' then '24'
                when substring(src.CodeArticle_modifié, 10, 1) = 'Q' then '30'
                when substring(src.CodeArticle_modifié, 10, 1) = 'Y' then '32'
                when substring(src.CodeArticle_modifié, 10, 1) = 'T' then '40'
                when substring(src.CodeArticle_modifié, 10, 1) = 'R' then '46'
                when substring(src.CodeArticle_modifié, 10, 1) = 'W' then '48'
                when substring(src.CodeArticle_modifié, 10, 1) = 'J' then '52' 
                else '99999' 
            end as Multiple
        ,   case when substring(src.CodeArticle_modifié, 1 ,1) = 'X'                                    then 'Croquettes'
                when substring(src.CodeArticle_modifié, 1, 2) = 'SP'                                    then 'Fab Pochons'
                when substring(src.CodeArticle_modifié, 1, 2) in ('LP', 'FP', 'MP')                     then 'Emb Pochons'
                when substring(src.CodeArticle_modifié, 1, 2) in ('F0', 'F1', 'L0', 'L1', 'M0', 'M1')   then 'Etiquettage'
                when substring(src.CodeArticle_modifié, 1, 2) in ('S0', 'S1', 'SB', 'SR', 'SG')         then 'Fab Barquettes'
                else '99999' 
            end as Secteur
        ,   src.'Type'
        ,   src.Code_Lct
        ,   src.CodeArticle
        ,   src.NbrePiecesColis
        ,   src.Poste
        ,   src.Ligne
        ,   src.'Désignation_Opération'
        ,   src.'Code_Op'
        ,   src.'Cadence_SILOG'
        ,   src.DureeExecution
        ,   src.DureeReglage
        ,   src.DureeCalage
        ,   src.HeureDebutExecuto
        ,   src.MinuteDebutExecuto
        ,   src.HeureFinExecuto
        ,   src.MinuteFinExecuto
        ,   src.HeureDebutReglage
        ,   src.MinuteDebutReglage
        ,   src.HeureFinReglage
        ,   src.MinuteFinReglage
        ,   src.HeureDebutCalage
        ,   src.MinuteDebutCalage
        ,   src.HeureFinCalage
        ,   src.MinuteFinCalage
        ,   src.HeuresExecuto
        ,   src.MinutesExecuto
        ,   src.HeuresReglage
        ,   src.MinutesReglage
        ,   src.HeuresCalage
        ,   src.MinutesCalage
        ,   src.'Equipe'
        ,   src.VarNumUtil
        ,   src.VarNumUtil2
        ,   src.VarNumUtil3
        ,   src.VarNumUtil4
        ,   src.VarNumUtil8
        ,   src.VarNumUtil9
        ,   src.Remarque1
        ,   src.Remarque2
    from    src
    where   src.DateTravail like '%2016%'
    order by src.DateTravail
    Modérateur Langage SQL
    Règles du forum Langage SQL à lire par tous, N'hésitez pas à consulter les cours SQL
    N'oubliez pas le bouton et pensez aux balises
    [code]
    Si une réponse vous a aidé à résoudre votre problème, n'oubliez pas de voter pour elle en cliquant sur
    Aide-toi et le forum t'aidera : Un problème exposé sans mentionner les tentatives de résolution infructueuses peut laisser supposer que le posteur attend qu'on fasse son travail à sa place... et ne donne pas envie d'y répondre.

  10. #10
    Membre à l'essai
    Homme Profil pro
    Technicien méthodes
    Inscrit en
    Décembre 2014
    Messages
    19
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 42
    Localisation : France, Calvados (Basse Normandie)

    Informations professionnelles :
    Activité : Technicien méthodes
    Secteur : Agroalimentaire - Agriculture

    Informations forums :
    Inscription : Décembre 2014
    Messages : 19
    Points : 14
    Points
    14
    Par défaut
    J'ai essayé de faire votre requete mais cela ne fonctionne pas, par contre, pourriez-vous me dire ce qu'il cloche sur mon essai

    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
    SELECT 
    	NoEnregistrement,
    	DateTravail,
    	case when Moistravail='01' and Semaine_modifie='53' then AnnéeTravail-1 else AnnéeTravail end as année,
    	Semaine_modifie,
    from (
    	SELECT 
    		ETEMPS.NoEnregistrement,
    		ETEMPS.DateTravail,
    		datepart(MONTH,ETEMPS.DateTravail)='01' as Moistravail,
    		datepart(year,ETEMPS.DateTravail) end as AnnéeTravail,
    		case when datepart(weekday,ETEMPS.DateTravail)='7' and (((ETEMPS.HeureDebutExecuto*60)+ETEMPS.MinuteDebutExecuto)>1245 OR ((ETEMPS.HeureDebutCalage*60)+ETEMPS.MinuteDebutCalage)>1245) AND (datepart(iso_week,ETEMPS.DateTravail)='52' OR datepart(iso_week,ETEMPS.DateTravail)='53') then '1'
    		when datepart(weekday,ETEMPS.DateTravail)='7' and (((ETEMPS.HeureDebutExecuto*60)+ETEMPS.MinuteDebutExecuto)>1245 OR ((ETEMPS.HeureDebutCalage*60)+ETEMPS.MinuteDebutCalage)>1245) AND (datepart(iso_week,ETEMPS.DateTravail)<>'52' OR datepart(iso_week,ETEMPS.DateTravail)<>'53') then datepart(iso_week,ETEMPS.DateTravail)+1 Else datepart(iso_week,ETEMPS.DateTravail) END as Semaine_modifie,
    		) As T
    where ETEMPS.DateTravail like '%2016%'
    order by ETEMPS.DateTravail
    Message d'erreur :
    Msg*156, Niveau*15, État*1, Ligne*6
    Syntaxe incorrecte vers le mot clé 'from'.
    Msg*102, Niveau*15, État*1, Ligne*10
    Syntaxe incorrecte vers '='.
    Msg*102, Niveau*15, État*1, Ligne*12
    Syntaxe incorrecte vers 'ETEMPS'.
    Msg*102, Niveau*15, État*1, Ligne*13
    Syntaxe incorrecte vers 'ETEMPS'.

  11. #11
    Modérateur

    Profil pro
    dba
    Inscrit en
    Janvier 2010
    Messages
    5 643
    Détails du profil
    Informations personnelles :
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : dba

    Informations forums :
    Inscription : Janvier 2010
    Messages : 5 643
    Points : 13 092
    Points
    13 092
    Par défaut
    Ma requete n'était qu'un exemple, non fonctionnel et non testé, juste pour illustrer

    En l’occurrence, vous avez laissé trainer une virgule avant le FROM.
    J'avais également laissé trainer un ='01 en ligne 10 qui n'a rien à faire là.
    Et plus globalement, les ... sont à compléter, notamment pas la clause FROM dans la sous requete...

    Mais al1_24 ayant pris la peine de "convertir" toute votre requete (joli travail au passage !), partez de la sienne !

  12. #12
    Membre à l'essai
    Homme Profil pro
    Technicien méthodes
    Inscrit en
    Décembre 2014
    Messages
    19
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 42
    Localisation : France, Calvados (Basse Normandie)

    Informations professionnelles :
    Activité : Technicien méthodes
    Secteur : Agroalimentaire - Agriculture

    Informations forums :
    Inscription : Décembre 2014
    Messages : 19
    Points : 14
    Points
    14
    Par défaut
    Je l'ai essayé et pour être franc je ne comprends pas tout,

    Je l'ai recopié

    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
    with    src
        as  (   select  ETEMPS.NoEnregistrement
                    ,   ETEMPS.DateTravail
                    ,   datepart(weekday,ETEMPS.DateTravail)    as  Jours_C
                    ,   datename(weekday, ETEMPS.DateTravail)   as  Jours_N
                    ,   datepart(iso_week,ETEMPS.DateTravail)   as  Semaine
                    ,   (ETEMPS.HeureDebutExecuto * 60) + ETEMPS.MinuteDebutExecuto as  DebutExecuto
                    ,   (ETEMPS.HeureDebutCalage * 60) + ETEMPS.MinuteDebutCalage   as  DebutCalage
                    ,   case when substring(ARTICLE.CodeArticle, 1, 1) = 'Z' 
                                then substring(ARTICLE.CodeArticle, 2, 12) 
                            else ARTICLE.CodeArticle 
                        end as CodeArticle_modifié
                    ,   ETEMPS.TypeLctimprod    AS 'Type'
                    ,   ETEMPS.CodeLanctimprod  AS 'Code_Lct'
                    ,   ARTICLE.CodeArticle
                    ,   ARTICLE.NbrePiecesColis
                    ,   ETEMPS.CodePoste        AS 'Poste'
                    ,   ETEMPS.CodeOperateur    AS 'Ligne'
                    ,   OPERGAM.Designation1    AS 'Désignation_Opération'
                    ,   LCTC.CodeOperation      AS 'Code_Op'
                    ,   OPERGAM.VarNumUtil4     AS 'Cadence_SILOG'
                    ,   ETEMPS.DureeExecution
                    ,   ETEMPS.DureeReglage
                    ,   ETEMPS.DureeCalage
                    ,   ETEMPS.HeureDebutExecuto
                    ,   ETEMPS.MinuteDebutExecuto
                    ,   ETEMPS.HeureFinExecuto
                    ,   ETEMPS.MinuteFinExecuto
                    ,   ETEMPS.HeureDebutReglage
                    ,   ETEMPS.MinuteDebutReglage
                    ,   ETEMPS.HeureFinReglage
                    ,   ETEMPS.MinuteFinReglage
                    ,   ETEMPS.HeureDebutCalage
                    ,   ETEMPS.MinuteDebutCalage
                    ,   ETEMPS.HeureFinCalage
                    ,   ETEMPS.MinuteFinCalage
                    ,   ETEMPS.HeuresExecuto
                    ,   ETEMPS.MinutesExecuto
                    ,   ETEMPS.HeuresReglage
                    ,   ETEMPS.MinutesReglage
                    ,   ETEMPS.HeuresCalage
                    ,   ETEMPS.MinutesCalage
                    ,   ETEMPS.VarAlphaUtil     AS 'Equipe'
                    ,   ETEMPS.VarNumUtil
                    ,   ETEMPS.VarNumUtil2
                    ,   ETEMPS.VarNumUtil3
                    ,   ETEMPS.VarNumUtil4
                    ,   ETEMPS.VarNumUtil8
                    ,   ETEMPS.VarNumUtil9
                    ,   ETEMPS.Remarque1
                    ,   ETEMPS.Remarque2
                from    LCTE
                    inner join 
                        LCTC 
                        on  LCTE.CodeLancement  = LCTC.CodeLancement
                    inner join 
                        OPERGAM 
                        on  LCTC.CodeOperation  = OPERGAM.CodeOperation
                    inner join 
                        ETEMPS 
                        on  LCTE.CodeLancement  = ETEMPS.CodeLanctimprod
                    inner join 
                        ARTICLE 
                        on  LCTE.CodeArticle    = ARTICLE.CodeArticle
            )
    SELECT  src.NoEnregistrement
        ,   src.DateTravail
        ,   case when datepart(month,ETEMPS.DateTravail) = 1 
                    and case when src.Jours_C = 7
                                  and (   src.DebutExecuto > 1245
                                      or  src.DebutCalage > 1245
                                      )
                                  and src.Semaine in (52, 53)
                              then 1
                            when src.Jours_C = 7
                                    and (   src.DebutExecuto > 1245
                                        or  src.DebutCalage > 1245
                                        )
                                    and src.Semaine not in (52, 53)
                                then src.Semaine + 1
                            else src.Semaine
                        end  = 53 
                then datepart(year, ETEMPS.DateTravail) - 1 
                else datepart(year, ETEMPS.DateTravail) 
            end as  Année
        ,   src.Jours_C
        ,   src.Jours_N
        ,   src.Semaine
        ,   case when src.Jours_C = 7
                        and (   src.DebutExecuto > 1245
                            or  src.DebutCalage > 1245
                            )
                        and src.Semaine in (52, 53)
                    then 1
                when src.Jours_C = 7
                        and (   src.DebutExecuto > 1245
                            or  src.DebutCalage > 1245
                            )
                        and src.Semaine not in (52, 53)
                    then src.Semaine + 1
                else src.Semaine
            end as Semaine_modifie
        ,   src.CodeArticle_modifié
        ,   case 
                when substring(src.CodeArticle_modifié, 1, 1) in ('E', 'S', 'F', 'M')   then '1'
                when substring(src.CodeArticle_modifié, 1, 2) in ('XS', 'XF', 'XM')     then '1'
                when substring(src.CodeArticle_modifié, 1, 3) = 'FPZ'   then '1'
                when substring(src.CodeArticle_modifié, 1, 4) = 'LNEI'  then '1'
                when substring(src.CodeArticle_modifié, 1, 1) = 'N'     then '3'
                when substring(src.CodeArticle_modifié, 1, 3) = 'XL5'   then '5'
                when substring(src.CodeArticle_modifié, 1, 3) = 'XLZ'   then '12'
                when substring(src.CodeArticle_modifié, 10, 1) = '3' then '3'
                when substring(src.CodeArticle_modifié, 10, 1) = '4' then '4'
                when substring(src.CodeArticle_modifié, 10, 1) = '5' then '5'
                when substring(src.CodeArticle_modifié, 10, 1) = '6' then '6'
                when substring(src.CodeArticle_modifié, 10, 1) = '8' then '8' 
                when substring(src.CodeArticle_modifié, 10, 1) = '9' then '9' 
                when substring(src.CodeArticle_modifié, 10, 1) = 'M' then '9'
                when substring(src.CodeArticle_modifié, 10, 1) = 'K' then '10'
                when substring(src.CodeArticle_modifié, 10, 1) = 'N' then '16'
                when substring(src.CodeArticle_modifié, 10, 1) = 'Z' then '12'
                when substring(src.CodeArticle_modifié, 10, 1) = 'X' then '18'
                when substring(src.CodeArticle_modifié, 10, 1) = 'S' then '20'
                when substring(src.CodeArticle_modifié, 10, 1) = 'L' then '21'
                when substring(src.CodeArticle_modifié, 10, 1) = 'P' then '22'
                when substring(src.CodeArticle_modifié, 10, 1) = 'V' then '24'
                when substring(src.CodeArticle_modifié, 10, 1) = 'Q' then '30'
                when substring(src.CodeArticle_modifié, 10, 1) = 'Y' then '32'
                when substring(src.CodeArticle_modifié, 10, 1) = 'T' then '40'
                when substring(src.CodeArticle_modifié, 10, 1) = 'R' then '46'
                when substring(src.CodeArticle_modifié, 10, 1) = 'W' then '48'
                when substring(src.CodeArticle_modifié, 10, 1) = 'J' then '52' 
                else '99999' 
            end as Multiple
        ,   case when substring(src.CodeArticle_modifié, 1 ,1) = 'X'                                    then 'Croquettes'
                when substring(src.CodeArticle_modifié, 1, 2) = 'SP'                                    then 'Fab Pochons'
                when substring(src.CodeArticle_modifié, 1, 2) in ('LP', 'FP', 'MP')                     then 'Emb Pochons'
                when substring(src.CodeArticle_modifié, 1, 2) in ('F0', 'F1', 'L0', 'L1', 'M0', 'M1')   then 'Etiquettage'
                when substring(src.CodeArticle_modifié, 1, 2) in ('S0', 'S1', 'SB', 'SR', 'SG')         then 'Fab Barquettes'
                else '99999' 
            end as Secteur
        ,   src.Type
        ,   src.Code_Lct
        ,   src.CodeArticle
        ,   src.NbrePiecesColis
        ,   src.Poste
        ,   src.Ligne
        ,   src.Désignation_Opération
        ,   src.Code_Op
        ,   src.Cadence_SILOG
        ,   src.DureeExecution
        ,   src.DureeReglage
        ,   src.DureeCalage
        ,   src.HeureDebutExecuto
        ,   src.MinuteDebutExecuto
        ,   src.HeureFinExecuto
        ,   src.MinuteFinExecuto
        ,   src.HeureDebutReglage
        ,   src.MinuteDebutReglage
        ,   src.HeureFinReglage
        ,   src.MinuteFinReglage
        ,   src.HeureDebutCalage
        ,   src.MinuteDebutCalage
        ,   src.HeureFinCalage
        ,   src.MinuteFinCalage
        ,   src.HeuresExecuto
        ,   src.MinutesExecuto
        ,   src.HeuresReglage
        ,   src.MinutesReglage
        ,   src.HeuresCalage
        ,   src.MinutesCalage
        ,   src.Equipe
        ,   src.VarNumUtil
        ,   src.VarNumUtil2
        ,   src.VarNumUtil3
        ,   src.VarNumUtil4
        ,   src.VarNumUtil8
        ,   src.VarNumUtil9
        ,   src.Remarque1
        ,   src.Remarque2
    from    src
    where   src.DateTravail like '%2016%'
    order by src.DateTravail
    et voici les message d’erreur :
    Msg*4104, Niveau*16, État*1, Ligne*68
    L'identificateur en plusieurs parties "ETEMPS.DateTravail" ne peut pas être lié.
    Msg*4104, Niveau*16, État*1, Ligne*83
    L'identificateur en plusieurs parties "ETEMPS.DateTravail" ne peut pas être lié.
    Msg*4104, Niveau*16, État*1, Ligne*84
    L'identificateur en plusieurs parties "ETEMPS.DateTravail" ne peut pas être lié.


    Merci encore à vous deux de m'aider, je sais j'ai la tête dure mais j'ai l'impression que ce que je veux faire et super complexe.

  13. #13
    Modérateur
    Avatar de al1_24
    Homme Profil pro
    Retraité
    Inscrit en
    Mai 2002
    Messages
    9 080
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 63
    Localisation : France, Val de Marne (Île de France)

    Informations professionnelles :
    Activité : Retraité
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mai 2002
    Messages : 9 080
    Points : 30 778
    Points
    30 778
    Par défaut
    En effet, une petite erreur de copier-coller

    L'alias de table ETEMPS n'est pas visible en dehors de l'expression de table src.
    Il suffit donc de remplacer ETEMPS par src dans les lignes mentionnées.
    Modérateur Langage SQL
    Règles du forum Langage SQL à lire par tous, N'hésitez pas à consulter les cours SQL
    N'oubliez pas le bouton et pensez aux balises
    [code]
    Si une réponse vous a aidé à résoudre votre problème, n'oubliez pas de voter pour elle en cliquant sur
    Aide-toi et le forum t'aidera : Un problème exposé sans mentionner les tentatives de résolution infructueuses peut laisser supposer que le posteur attend qu'on fasse son travail à sa place... et ne donne pas envie d'y répondre.

  14. #14
    Membre à l'essai
    Homme Profil pro
    Technicien méthodes
    Inscrit en
    Décembre 2014
    Messages
    19
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 42
    Localisation : France, Calvados (Basse Normandie)

    Informations professionnelles :
    Activité : Technicien méthodes
    Secteur : Agroalimentaire - Agriculture

    Informations forums :
    Inscription : Décembre 2014
    Messages : 19
    Points : 14
    Points
    14
    Par défaut
    Parfait cela fonctionne, par contre va falloir que j'arrive à comprendre maintenant

    Cela sert à quoi la fonction With et SRC??

    Merci encore

  15. #15
    Modérateur
    Avatar de al1_24
    Homme Profil pro
    Retraité
    Inscrit en
    Mai 2002
    Messages
    9 080
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 63
    Localisation : France, Val de Marne (Île de France)

    Informations professionnelles :
    Activité : Retraité
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mai 2002
    Messages : 9 080
    Points : 30 778
    Points
    30 778
    Par défaut
    with <alias> as (<requête>) select ... from <alias> peut être lu ici comme un équivalent de select ... from (<requête>) as <alias>
    Modérateur Langage SQL
    Règles du forum Langage SQL à lire par tous, N'hésitez pas à consulter les cours SQL
    N'oubliez pas le bouton et pensez aux balises
    [code]
    Si une réponse vous a aidé à résoudre votre problème, n'oubliez pas de voter pour elle en cliquant sur
    Aide-toi et le forum t'aidera : Un problème exposé sans mentionner les tentatives de résolution infructueuses peut laisser supposer que le posteur attend qu'on fasse son travail à sa place... et ne donne pas envie d'y répondre.

  16. #16
    Rédacteur

    Avatar de SQLpro
    Homme Profil pro
    Expert bases de données / SQL / MS SQL Server / Postgresql
    Inscrit en
    Mai 2002
    Messages
    21 736
    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 : 21 736
    Points : 52 447
    Points
    52 447
    Billets dans le blog
    5
    Par défaut
    Citation Envoyé par Mathieu A Voir le message
    Parfait cela fonctionne, par contre va falloir que j'arrive à comprendre maintenant

    Cela sert à quoi la fonction With et SRC??

    Merci encore
    Lisez l'article que j'ai écrit à ce sujet :
    http://sqlpro.developpez.com/cours/s...te-recursives/

    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/ * * * * *

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 4
    Dernier message: 29/11/2013, 08h56
  2. problème avec les données issues d'une kinect
    Par takfa2008 dans le forum Traitement d'images
    Réponses: 0
    Dernier message: 05/03/2013, 10h20
  3. Réponses: 2
    Dernier message: 30/09/2009, 17h43
  4. Réponses: 10
    Dernier message: 13/08/2007, 17h12
  5. Réponses: 13
    Dernier message: 14/10/2003, 15h31

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