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 :

Requête qui remonte la première ligne de chaque rupture d'article


Sujet :

Langage SQL

  1. #1
    Membre régulier
    Profil pro
    Ingenieur developpement
    Inscrit en
    Septembre 2002
    Messages
    173
    Détails du profil
    Informations personnelles :
    Âge : 54
    Localisation : France, Indre et Loire (Centre)

    Informations professionnelles :
    Activité : Ingenieur developpement

    Informations forums :
    Inscription : Septembre 2002
    Messages : 173
    Points : 104
    Points
    104
    Par défaut Requête qui remonte la première ligne de chaque rupture d'article
    Bonjour,

    J'ai une requête qui consiste a me ramener l'ensemble des articles d'une commande avec cet ordonnancement:
    - le poids inverse de l'article (du plus lourd au plus leger)
    - la famille de l'article
    - type d'emplacement prioritaire
    - la DLUO (Date Limite d'Utilisation Optimale)
    - Date d'entrée en stock

    Le paramètre unique est le n° de commande

    Ma requête actuelle me ramène l'ensemble des palettes aux emplacements pour un article.
    J'aimerai ne remonté que le premier de chaque article
    Voici la requête

    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
    select LIGNEPIECEVENTE.N_NUMLGN NoLg
      , LIGNEPIECEVENTE.N_IDARTICLE IdArt
      , LIGNEPIECEVENTE.N_QTECMDEE QteCmd
      , A.S_LBLSIMPLIFIE Designation
      , A.N_POIDSARTICLE PdsArt
      , EPLSTOCK.N_IDEPLSTOCK IdPalette
      , V_CODEEPLSTOCK.S_CODEEPLSTOCK CodeEmplacementPhysique
      , dbo.EPLSTOCK.N_QUANTITE QtePal
      , (select SUM(UM1.N_QUANTITE) -- somme qté disponible sur palette 
         from EPLSTOCK as UM1
           join ENTITESTOCKAGE ES1 on ES1.N_IDENTITESTOCKAGE = UM1.N_IDENTITESTOCKAGE
         where UM1.N_IDARTICLE = A.N_IDARTICLE
           and ES1.N_IDTYPESTOCKAGE = ES.N_IDTYPESTOCKAGE
           and ES1.N_IDSITE = 1
           and ES1.S_IDBLOCAGE <> 'SOR' and ES1.S_IDBLOCAGE <> 'ES') as QteStT
      , ES.N_IDTYPESTOCKAGE IdTEmpl
      , case when ES.N_IDTYPESTOCKAGE = 10 then 1
             when ES.N_IDTYPESTOCKAGE = 2 then 2
             when ES.N_IDTYPESTOCKAGE = 3 then 3
             else 4 end OrdreEmpl -- ordre de prélèvement
    from LIGNEPIECEVENTE
      join ENTETEPIECEVENTE on (ENTETEPIECEVENTE.N_IDENTETEPIECEVENTE = LIGNEPIECEVENTE.N_IDENTETEPIECEVENTE  And ENTETEPIECEVENTE.N_IDTYPEPIECE = 1)
      join ARTICLE as A on A.N_IDARTICLE = LIGNEPIECEVENTE.N_IDARTICLE
      join EPLSTOCK on A.N_IDARTICLE = EPLSTOCK.N_IDARTICLE
      join ENTITESTOCKAGE ES on ES.N_IDENTITESTOCKAGE = EPLSTOCK.N_IDENTITESTOCKAGE
      join V_CODEEPLSTOCK on V_CODEEPLSTOCK.N_IDEPLSTOCK = EPLSTOCK.N_IDEPLSTOCK
    where LIGNEPIECEVENTE.N_IDENTETEPIECEVENTE = 688034
      and S_STATUTLIGNE <> 'E'
      and S_STATUTLIGNE <> 'A'
      and S_STATUTLIGNE <> 'S'
      and A.B_PREPADIRECTEARTICLE <> 1
      and LIGNEPIECEVENTE.B_KIT = 0
      and ES.N_IDSITE = 1
      and ES.S_IDBLOCAGE <> 'SOR' and ES.S_IDBLOCAGE <> 'ES'
      and EPLSTOCK.N_QUANTITE > 0
    order by A.N_POIDSARTICLE desc, A.S_IDFAMILLEARTICLE, OrdreEmpl, EPLSTOCK.D_DLUO, EPLSTOCK.D_ENTREESTOCK, EPLSTOCK.N_IDEPLSTOCK
    Voici le résultat
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    NoLg	IdArt	QteCmd	Designation	PdsArt	IdPalette	CodeEmplacementPhysique	QtePal	QteStT	IdTEmpl	OrdreEmpl
    2	4587	1	CUTLXH5960	57,6	76244286	E-CA-RG02-RI-N0-003-01	4	4	1	4
    1	190	1	EMUFX1560	56,4	76238306	E-CA-RG06-RM-N3-004-01	6	24	1	4
    1	190	1	EMUFX1560	56,4	76254412	E-CA-RG05-RK-N1-006-01	6	24	1	4
    1	190	1	EMUFX1560	56,4	76254413	E-CA-RG05-RP-N1-002-01	6	24	1	4
    1	190	1	EMUFX1560	56,4	76267884	E-CA-RG05-RO-N1-001-01	6	24	1	4
    3	2322	2	PROTSHX1225	22,475	76259637	E-CA-RG08-RC-N1-001-01	1	1	1	4
    5	2065	12	LINEGTS1	0,9	76152666	E-CA-RG16-RN-N1-003-01	41	159	1	4
    5	2065	12	LINEGTS1	0,9	76208320	E-CA-RG16-RN-N2-004-01	36	159	1	4
    5	2065	12	LINEGTS1	0,9	76259248	E-CA-RG16-RL-N1-002-01	82	159	1	4
    4	1640	12	CUPROMIX05	0,5	76274230	E-CA-RG16-RN-N0-001-01	176	176	2	2
    4	1640	12	CUPROMIX05	0,5	280757	E-CA-RG16-RN-N2-003-01	93	93	1	4
    Ce que je voudrais comme résultat
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    NoLg	IdArt	QteCmd	Designation	PdsArt	IdPalette	CodeEmplacementPhysique	QtePal	QteStT	IdTEmpl	OrdreEmpl
    2	4587	1	CUTLXH5960	57,6	76244286	E-CA-RG02-RI-N0-003-01	4	4	1	4
    1	190	1	EMUFX1560	56,4	76238306	E-CA-RG06-RM-N3-004-01	6	24	1	4
    3	2322	2	PROTSHX1225	22,475	76259637	E-CA-RG08-RC-N1-001-01	1	1	1	4
    5	2065	12	LINEGTS1	0,9	76152666	E-CA-RG16-RN-N1-003-01	41	159	1	4
    4	1640	12	CUPROMIX05	0,5	76274230	E-CA-RG16-RN-N0-001-01	176	176	2	2
    Je suis en SQL Server 2008
    merci de vos réponses

  2. #2
    Expert confirmé
    Homme Profil pro
    Inscrit en
    Mai 2002
    Messages
    3 173
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 41
    Localisation : France, Rhône (Rhône Alpes)

    Informations forums :
    Inscription : Mai 2002
    Messages : 3 173
    Points : 5 345
    Points
    5 345
    Par défaut
    bonjour,

    utilisez une fonction de fenetrage : http://sqlpro.developpez.com/article...clause-window/


    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
     
    with tmp as (
    SELECT LIGNEPIECEVENTE.N_NUMLGN NoLg
      , LIGNEPIECEVENTE.N_IDARTICLE IdArt
      , LIGNEPIECEVENTE.N_QTECMDEE QteCmd
      , A.S_LBLSIMPLIFIE Designation
      , A.N_POIDSARTICLE PdsArt
      , EPLSTOCK.N_IDEPLSTOCK IdPalette
      , V_CODEEPLSTOCK.S_CODEEPLSTOCK CodeEmplacementPhysique
      , dbo.EPLSTOCK.N_QUANTITE QtePal
      , ES.N_IDTYPESTOCKAGE IdTEmpl
      , case when ES.N_IDTYPESTOCKAGE = 10 then 1
             when ES.N_IDTYPESTOCKAGE = 2 then 2
             when ES.N_IDTYPESTOCKAGE = 3 then 3
             else 4 end OrdreEmpl -- ordre de prélèvement
      , row_number() over(partition by LIGNEPIECEVENTE.N_IDARTICLE order by A.N_POIDSARTICLE DESC, A.S_IDFAMILLEARTICLE, OrdreEmpl, EPLSTOCK.D_DLUO, EPLSTOCK.D_ENTREESTOCK, EPLSTOCK.N_IDEPLSTOCK) as rnk
    FROM LIGNEPIECEVENTE
      JOIN ENTETEPIECEVENTE ON (ENTETEPIECEVENTE.N_IDENTETEPIECEVENTE = LIGNEPIECEVENTE.N_IDENTETEPIECEVENTE  AND ENTETEPIECEVENTE.N_IDTYPEPIECE = 1)
      JOIN ARTICLE AS A ON A.N_IDARTICLE = LIGNEPIECEVENTE.N_IDARTICLE
      JOIN EPLSTOCK ON A.N_IDARTICLE = EPLSTOCK.N_IDARTICLE
      JOIN ENTITESTOCKAGE ES ON ES.N_IDENTITESTOCKAGE = EPLSTOCK.N_IDENTITESTOCKAGE
      JOIN V_CODEEPLSTOCK ON V_CODEEPLSTOCK.N_IDEPLSTOCK = EPLSTOCK.N_IDEPLSTOCK
    WHERE LIGNEPIECEVENTE.N_IDENTETEPIECEVENTE = 688034
      AND S_STATUTLIGNE NOT IN  ('E', 'A', 'S')
      AND A.B_PREPADIRECTEARTICLE <> 1
      AND LIGNEPIECEVENTE.B_KIT = 0
      AND ES.N_IDSITE = 1
      AND ES.S_IDBLOCAGE NOT IN ('SOR', 'ES')
      AND EPLSTOCK.N_QUANTITE > 0)
     
     
    SELECT 
      TMP.*, (SELECT SUM(UM1.N_QUANTITE) -- somme qté disponible sur palette 
         FROM EPLSTOCK AS UM1
           JOIN ENTITESTOCKAGE ES1 ON ES1.N_IDENTITESTOCKAGE = UM1.N_IDENTITESTOCKAGE
         WHERE UM1.N_IDARTICLE = TMP.N_IDARTICLE
           AND ES1.N_IDTYPESTOCKAGE = TMP.N_IDTYPESTOCKAGE
           AND ES1.N_IDSITE = 1
           AND ES1.S_IDBLOCAGE <> 'SOR' AND ES1.S_IDBLOCAGE <> 'ES') AS QteStT
    FROM TMP
    WHERE RNK = 1

  3. #3
    Membre régulier
    Profil pro
    Ingenieur developpement
    Inscrit en
    Septembre 2002
    Messages
    173
    Détails du profil
    Informations personnelles :
    Âge : 54
    Localisation : France, Indre et Loire (Centre)

    Informations professionnelles :
    Activité : Ingenieur developpement

    Informations forums :
    Inscription : Septembre 2002
    Messages : 173
    Points : 104
    Points
    104
    Par défaut
    La colonne OrdreEmpl est dans le select ce qui me remonte un message d'erreur

    Lookup Error - SQL Server Database Error: Nom de colonne non valide : 'OrdreEmpl'.

  4. #4
    Expert confirmé
    Homme Profil pro
    Inscrit en
    Mai 2002
    Messages
    3 173
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 41
    Localisation : France, Rhône (Rhône Alpes)

    Informations forums :
    Inscription : Mai 2002
    Messages : 3 173
    Points : 5 345
    Points
    5 345
    Par défaut
    ah, oui, les alias sont créés après le select.

  5. #5
    Membre régulier
    Profil pro
    Ingenieur developpement
    Inscrit en
    Septembre 2002
    Messages
    173
    Détails du profil
    Informations personnelles :
    Âge : 54
    Localisation : France, Indre et Loire (Centre)

    Informations professionnelles :
    Activité : Ingenieur developpement

    Informations forums :
    Inscription : Septembre 2002
    Messages : 173
    Points : 104
    Points
    104
    Par défaut
    Merci à Punkoff pour sa réponse.

    J'ai un peu modifié pour avoir le résultat

    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
    WITH TMP AS (
    SELECT *
      , row_number() OVER(PARTITION BY IdArt ORDER BY PdsArt DESC, IdFam, OrdreEmpl, D_DLUO, D_ENTREESTOCK, IdPalette) AS rnk
    FROM (
    SELECT LIGNEPIECEVENTE.N_NUMLGN NoLg
      , LIGNEPIECEVENTE.N_IDARTICLE IdArt
      , LIGNEPIECEVENTE.N_QTECMDEE QteCmd
      , A.S_LBLSIMPLIFIE Designation
      , A.N_POIDSARTICLE PdsArt
      , EPLSTOCK.N_IDEPLSTOCK IdPalette
      , V_CODEEPLSTOCK.S_CODEEPLSTOCK CodeEmplacementPhysique
      , dbo.EPLSTOCK.N_QUANTITE QtePal
      , ES.N_IDTYPESTOCKAGE IdTEmpl
      , case when ES.N_IDTYPESTOCKAGE = 10 then 1
             when ES.N_IDTYPESTOCKAGE = 2 then 2
             when ES.N_IDTYPESTOCKAGE = 3 then 3
             else 4 end OrdreEmpl -- ordre de prélèvement
      , A.S_IDFAMILLEARTICLE IdFam
      , EPLSTOCK.D_DLUO
      , EPLSTOCK.D_ENTREESTOCK
    FROM LIGNEPIECEVENTE
      JOIN ENTETEPIECEVENTE ON (ENTETEPIECEVENTE.N_IDENTETEPIECEVENTE = LIGNEPIECEVENTE.N_IDENTETEPIECEVENTE  AND ENTETEPIECEVENTE.N_IDTYPEPIECE = 1)
      JOIN ARTICLE AS A ON A.N_IDARTICLE = LIGNEPIECEVENTE.N_IDARTICLE
      JOIN EPLSTOCK ON A.N_IDARTICLE = EPLSTOCK.N_IDARTICLE
      JOIN ENTITESTOCKAGE ES ON ES.N_IDENTITESTOCKAGE = EPLSTOCK.N_IDENTITESTOCKAGE
      JOIN V_CODEEPLSTOCK ON V_CODEEPLSTOCK.N_IDEPLSTOCK = EPLSTOCK.N_IDEPLSTOCK
    WHERE LIGNEPIECEVENTE.N_IDENTETEPIECEVENTE = 688034
      AND S_STATUTLIGNE NOT IN  ('E', 'A', 'S')
      AND A.B_PREPADIRECTEARTICLE <> 1
      AND LIGNEPIECEVENTE.B_KIT = 0
      AND ES.N_IDSITE = 1
      AND ES.S_IDBLOCAGE NOT IN ('SOR', 'ES')
      AND EPLSTOCK.N_QUANTITE > 0) AS TOUT
      )  
     
     
    SELECT 
      TMP.NoLg
      , TMP.IdArt
      , TMP.QteCmd
      , TMP.Designation
      , TMP.PdsArt
      , TMP.IdPalette
      , TMP.CodeEmplacementPhysique
      , TMP.QtePal
      , (SELECT SUM(UM1.N_QUANTITE) -- somme qté disponible sur palette 
         FROM EPLSTOCK AS UM1
           JOIN ENTITESTOCKAGE ES1 ON ES1.N_IDENTITESTOCKAGE = UM1.N_IDENTITESTOCKAGE
         WHERE UM1.N_IDARTICLE = TMP.IdArt
           AND ES1.N_IDTYPESTOCKAGE = TMP.IdTEmpl
           AND ES1.N_IDSITE = 1
           AND ES1.S_IDBLOCAGE NOT IN ('SOR', 'ES')) AS QteStT
    FROM TMP
    WHERE RNK = 1
    ORDER BY PdsArt desc, IdFam

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

Discussions similaires

  1. fonction qui retourne les première lettres de chaque mot en PHP
    Par simonlaurent5000 dans le forum Langage
    Réponses: 5
    Dernier message: 22/02/2012, 20h50
  2. [MySQL] Optimiser le temps de traitement d'une simple requête qui retourne 800 000 lignes.
    Par kamnouz dans le forum PHP & Base de données
    Réponses: 8
    Dernier message: 17/06/2011, 18h37
  3. requète qui retourne la derniere ligne d'un ensemble de tuple
    Par Stouille33 dans le forum Développement
    Réponses: 4
    Dernier message: 14/11/2008, 09h15
  4. Réponses: 2
    Dernier message: 25/06/2008, 14h58
  5. Récupération première ligne de chaque groupe
    Par iza76 dans le forum Langage SQL
    Réponses: 2
    Dernier message: 14/01/2008, 10h07

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