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

Requêtes MySQL Discussion :

Extraire des données sous une forme particulière de GROUP BY (?)


Sujet :

Requêtes MySQL

  1. #1
    Membre du Club
    Profil pro
    Inscrit en
    Octobre 2010
    Messages
    163
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Octobre 2010
    Messages : 163
    Points : 59
    Points
    59
    Par défaut Extraire des données sous une forme particulière de GROUP BY (?)
    Bonjour,

    Dans la table "maTable", j'ai des données dans ce genre là :
    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
    entreprise | option | chef de service | personne
    entr1 | option1 | chef 1 | personne 1
    entr1 | option1 | chef 1 | personne 2
    entr1 | option1 | chef 1 | personne 3
    entr1 | option1 | chef 2 | personne 4
    entr1 | option1 | chef 2 | personne 5
    entr1 | option2 | chef 3 | personne 6
    entr1 | option2 | chef 3 | personne 7
    entr2 | option1 | chef 4 | personne 8
    entr2 | option1 | chef 4 | personne 9
    entr2 | option1 | chef 4 | personne 10
    entr2 | option1 | chef 5 | personne 11
    entr2 | option1 | chef 5 | personne 12
    entr2 | option2 | chef 6 | personne 13
    entr2 | option2 | chef 6 | personne 14
    et idem pour entr3 etc...
    J'aimerais ressortir les données sous la forme :
    entr1 :
    option 1 :
    chef 1 > personne 1, personne 2, personne 3
    chef 2 > personne 4, personne 5
    option 2 :
    chef 3 > personne 6, personne 7
    entr2 : idem...
    Je sais faire le select qui va me renvoyer le tableau de données ci-dessus, mais pas la mise en forme comme je le souhaite... Vous auriez un conseil ?

    Mille mercis...

    T.

  2. #2
    Membre émérite Avatar de vttman
    Homme Profil pro
    Développeur "couteau mosellan"
    Inscrit en
    Décembre 2002
    Messages
    1 140
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Moselle (Lorraine)

    Informations professionnelles :
    Activité : Développeur "couteau mosellan"
    Secteur : Industrie

    Informations forums :
    Inscription : Décembre 2002
    Messages : 1 140
    Points : 2 286
    Points
    2 286
    Par défaut
    Regarder du coté group_concat ?

    Ce genre
    =>
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
    SELECT `entreprise`, ` option`, ` chefservice` , GROUP_CONCAT(` personne` ) 
    FROM `tbl_name` 
    GROUP BY `entreprise`, ` option`,` chefservice`
    Emérite, émérite je ne pense pas ... plutôt dans le développement depuis FORT FORT longtemps, c'est mon job, ça oui
    A part ça ... Il ne pleut jamais en Moselle !

  3. #3
    Membre du Club
    Profil pro
    Inscrit en
    Octobre 2010
    Messages
    163
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Octobre 2010
    Messages : 163
    Points : 59
    Points
    59
    Par défaut
    Hmmm... Peut-être bien, oui... En fait ma difficulté, ici, est alors causée parce qu'il y a plusieurs groupes sur des colonnes différentes... Du coup je viens de faire une petite recherche et j'ai trouvé ce genre de code, qui semble montrer comment faire des regroupements de colonnes différents (mais je dois approfondir la question et faire des tests, encore) :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    SELECT
        p.id,
        images_id,
        images_title,
        facets_id,
        ...
    FROM PRODUCT p
    JOIN (SELECT product.id, GROUP_CONCAT(image.id) AS images_id
          FROM PRODUCT GROUP BY product.id) a on a.id = p.id
    JOIN (SELECT product.id, GROUP_CONCAT(image.title) AS images_title
          FROM PRODUCT GROUP BY product.id) b on b.id = p.id
    JOIN (SELECT product.id, GROUP_CONCAT(facet.id) AS facets_id
          FROM PRODUCT GROUP BY product.id) b on c.id = p.id
    ...

  4. #4
    Expert éminent sénior Avatar de Artemus24
    Homme Profil pro
    Agent secret au service du président Ulysses S. Grant !
    Inscrit en
    Février 2011
    Messages
    6 381
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Agent secret au service du président Ulysses S. Grant !
    Secteur : Finance

    Informations forums :
    Inscription : Février 2011
    Messages : 6 381
    Points : 19 065
    Points
    19 065
    Par défaut
    Salut trucmuche2005.

    Je réponds au premier message. Pour ce genre de ligne :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    chef 1 > personne 1, personne 2, personne 3
    en effet, cela se fait avec la fonction "group_concat()" et l'exemple donné par "vttman" est parfait.

    Pour les autres autres lignes, il faut gérer les ruptures de séquence.
    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
    --------------
    SET AUTOCOMMIT = 0
    --------------
     
    --------------
    START TRANSACTION
    --------------
     
    --------------
    SET collation_connection = latin1_general_ci
    --------------
     
    --------------
    DROP DATABASE IF EXISTS `base`
    --------------
     
    --------------
    CREATE DATABASE `base`
        DEFAULT CHARACTER SET `latin1`
        DEFAULT COLLATE       `latin1_general_ci`
    --------------
     
    --------------
    DROP TABLE IF EXISTS `test`
    --------------
     
    --------------
    CREATE TABLE `test`
    ( `id`           integer unsigned not null auto_increment primary key,
      `entreprise`   varchar(255)     not null,
      `option`       varchar(255)     not null,
      `chef`         varchar(255)     not null,
      `personne`     varchar(255)     not null
    ) ENGINE=InnoDB
      DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
      ROW_FORMAT=COMPRESSED
    --------------
     
    --------------
    insert into `test` (`entreprise`,`option`,`chef`,`personne`) values
      ('entr1', 'option1', 'chef 1', 'personne  1'),
      ('entr1', 'option1', 'chef 1', 'personne  2'),
      ('entr1', 'option1', 'chef 1', 'personne  3'),
      ('entr1', 'option1', 'chef 2', 'personne  4'),
      ('entr1', 'option1', 'chef 2', 'personne  5'),
      ('entr1', 'option1', 'chef 2', 'personne  6'),
      ('entr1', 'option2', 'chef 3', 'personne  7'),
      ('entr1', 'option2', 'chef 3', 'personne  8'),
      ('entr1', 'option2', 'chef 3', 'personne  9'),
      ('entr1', 'option2', 'chef 4', 'personne 10'),
      ('entr1', 'option2', 'chef 4', 'personne 11'),
      ('entr1', 'option2', 'chef 4', 'personne 12'),
      ('entr2', 'option3', 'chef 5', 'personne 13'),
      ('entr2', 'option3', 'chef 5', 'personne 14'),
      ('entr2', 'option3', 'chef 5', 'personne 15'),
      ('entr2', 'option3', 'chef 6', 'personne 16'),
      ('entr2', 'option3', 'chef 6', 'personne 17'),
      ('entr2', 'option3', 'chef 6', 'personne 18'),
      ('entr2', 'option4', 'chef 7', 'personne 19'),
      ('entr2', 'option4', 'chef 7', 'personne 20'),
      ('entr2', 'option4', 'chef 7', 'personne 21'),
      ('entr2', 'option4', 'chef 8', 'personne 22'),
      ('entr2', 'option4', 'chef 8', 'personne 23'),
      ('entr2', 'option4', 'chef 8', 'personne 24')
    --------------
     
    --------------
    select * from `test`
    --------------
     
    +----+------------+---------+--------+-------------+
    | id | entreprise | option  | chef   | personne    |
    +----+------------+---------+--------+-------------+
    |  1 | entr1      | option1 | chef 1 | personne  1 |
    |  2 | entr1      | option1 | chef 1 | personne  2 |
    |  3 | entr1      | option1 | chef 1 | personne  3 |
    |  4 | entr1      | option1 | chef 2 | personne  4 |
    |  5 | entr1      | option1 | chef 2 | personne  5 |
    |  6 | entr1      | option1 | chef 2 | personne  6 |
    |  7 | entr1      | option2 | chef 3 | personne  7 |
    |  8 | entr1      | option2 | chef 3 | personne  8 |
    |  9 | entr1      | option2 | chef 3 | personne  9 |
    | 10 | entr1      | option2 | chef 4 | personne 10 |
    | 11 | entr1      | option2 | chef 4 | personne 11 |
    | 12 | entr1      | option2 | chef 4 | personne 12 |
    | 13 | entr2      | option3 | chef 5 | personne 13 |
    | 14 | entr2      | option3 | chef 5 | personne 14 |
    | 15 | entr2      | option3 | chef 5 | personne 15 |
    | 16 | entr2      | option3 | chef 6 | personne 16 |
    | 17 | entr2      | option3 | chef 6 | personne 17 |
    | 18 | entr2      | option3 | chef 6 | personne 18 |
    | 19 | entr2      | option4 | chef 7 | personne 19 |
    | 20 | entr2      | option4 | chef 7 | personne 20 |
    | 21 | entr2      | option4 | chef 7 | personne 21 |
    | 22 | entr2      | option4 | chef 8 | personne 22 |
    | 23 | entr2      | option4 | chef 8 | personne 23 |
    | 24 | entr2      | option4 | chef 8 | personne 24 |
    +----+------------+---------+--------+-------------+
    --------------
    drop view if exists `vue`
    --------------
     
    --------------
    create view `vue` as
      select    concat(`entreprise`, ':')                       as col1,
                concat(`option`, ':')                           as col2,
                concat(`chef`, ' > ', group_concat(`personne`)) as col3
          from  `test`
      group by  `entreprise`,`option`,`chef`
    --------------
     
    --------------
    select * from vue
    --------------
     
    +--------+----------+----------------------------------------------+
    | col1   | col2     | col3                                         |
    +--------+----------+----------------------------------------------+
    | entr1: | option1: | chef 1 > personne  1,personne  2,personne  3 |
    | entr1: | option1: | chef 2 > personne  4,personne  5,personne  6 |
    | entr1: | option2: | chef 3 > personne  7,personne  8,personne  9 |
    | entr1: | option2: | chef 4 > personne 10,personne 11,personne 12 |
    | entr2: | option3: | chef 5 > personne 13,personne 14,personne 15 |
    | entr2: | option3: | chef 6 > personne 16,personne 17,personne 18 |
    | entr2: | option4: | chef 7 > personne 19,personne 20,personne 21 |
    | entr2: | option4: | chef 8 > personne 22,personne 23,personne 24 |
    +--------+----------+----------------------------------------------+
    --------------
    select  col1, col2, col3
      from  (  select      if(col1=@p1,'',col1)  as col1,
                           if(col2=@p2,'',col2)  as col2,
                           if(col3=@p3,'',col3)  as col3,
                           @p1:=col1,@p2:=col2,@p3:=col3
                     from  vue
               cross join  (select @p1:='',@p2:='',@p3:='') as x
            ) as y
    --------------
     
    +--------+----------+----------------------------------------------+
    | col1   | col2     | col3                                         |
    +--------+----------+----------------------------------------------+
    | entr1: | option1: | chef 1 > personne  1,personne  2,personne  3 |
    |        |          | chef 2 > personne  4,personne  5,personne  6 |
    |        | option2: | chef 3 > personne  7,personne  8,personne  9 |
    |        |          | chef 4 > personne 10,personne 11,personne 12 |
    | entr2: | option3: | chef 5 > personne 13,personne 14,personne 15 |
    |        |          | chef 6 > personne 16,personne 17,personne 18 |
    |        | option4: | chef 7 > personne 19,personne 20,personne 21 |
    |        |          | chef 8 > personne 22,personne 23,personne 24 |
    +--------+----------+----------------------------------------------+
    --------------
    COMMIT
    --------------
     
    --------------
    SET AUTOCOMMIT = 1
    --------------
     
    Appuyez sur une touche pour continuer...
    Normalement, cela se fait en php !

    @+
    Si vous êtes de mon aide, vous pouvez cliquer sur .
    Mon site : http://www.jcz.fr

  5. #5
    Membre du Club
    Profil pro
    Inscrit en
    Octobre 2010
    Messages
    163
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Octobre 2010
    Messages : 163
    Points : 59
    Points
    59
    Par défaut
    Salut !

    Merci pour ta réponse, je vais apprendre encore des choses ici...
    Cela dit, comme le nombre de personnes par chef n'est pas toujours le même (il peut y en avoir 1 comme il peut y en avoir 100), il me semble que ta solution se transpose difficilement. Je devrais donc sans doute me tourner vers la solution PHP au final mais je vais quand même essayer apprendre ce dont tu parles :-)
    J'aimais bien la solution MySQL car je pouvais directement exporter en CSV avec into outfile... En PHP, je vais devoir faire tout cela à la main mais ce n'est pas forcément un problème, c'est juste un peu plus de taf il me semble.

    Merci encore !!

    T.

  6. #6
    Expert éminent sénior Avatar de Artemus24
    Homme Profil pro
    Agent secret au service du président Ulysses S. Grant !
    Inscrit en
    Février 2011
    Messages
    6 381
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Agent secret au service du président Ulysses S. Grant !
    Secteur : Finance

    Informations forums :
    Inscription : Février 2011
    Messages : 6 381
    Points : 19 065
    Points
    19 065
    Par défaut
    Salut trucmuche2005.

    Et quelque chose dans ce genre là ne vous conviendrait-il pas ?
    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
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    --------------
    SET AUTOCOMMIT = 0
    --------------
     
    --------------
    START TRANSACTION
    --------------
     
    --------------
    SET collation_connection = latin1_general_ci
    --------------
     
    --------------
    DROP DATABASE IF EXISTS `base`
    --------------
     
    --------------
    CREATE DATABASE `base`
        DEFAULT CHARACTER SET `latin1`
        DEFAULT COLLATE       `latin1_general_ci`
    --------------
     
    --------------
    DROP TABLE IF EXISTS `test`
    --------------
     
    --------------
    CREATE TABLE `test`
    ( `id`           integer unsigned not null auto_increment primary key,
      `entreprise`   varchar(255)     not null,
      `option`       varchar(255)     not null,
      `chef`         varchar(255)     not null,
      `personne`     varchar(255)     not null
    ) ENGINE=InnoDB
      DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
      ROW_FORMAT=COMPRESSED
    --------------
     
    --------------
    insert into `test` (`entreprise`,`option`,`chef`,`personne`) values
      ('entr1', 'option1', 'chef 1', 'pers  1'),  ('entr1', 'option1', 'chef 1', 'pers  2'),  ('entr1', 'option1', 'chef 1', 'pers  3'),
      ('entr1', 'option1', 'chef 1', 'pers  4'),  ('entr1', 'option1', 'chef 1', 'pers  5'),  ('entr1', 'option1', 'chef 1', 'pers  6'),
      ('entr1', 'option1', 'chef 1', 'pers  7'),  ('entr1', 'option1', 'chef 1', 'pers  8'),
     
      ('entr1', 'option1', 'chef 2', 'pers  9'),  ('entr1', 'option1', 'chef 2', 'pers 10'),  ('entr1', 'option1', 'chef 2', 'pers 11'),
      ('entr1', 'option1', 'chef 2', 'pers 12'),  ('entr1', 'option1', 'chef 2', 'pers 13'),  ('entr1', 'option1', 'chef 2', 'pers 14'),
      ('entr1', 'option1', 'chef 2', 'pers 15'),  ('entr1', 'option1', 'chef 2', 'pers 16'),
     
      ('entr1', 'option2', 'chef 3', 'pers 17'),  ('entr1', 'option2', 'chef 3', 'pers 18'),  ('entr1', 'option2', 'chef 3', 'pers 19'),
      ('entr1', 'option2', 'chef 3', 'pers 20'),  ('entr1', 'option2', 'chef 3', 'pers 21'),  ('entr1', 'option2', 'chef 3', 'pers 22'),
      ('entr1', 'option2', 'chef 3', 'pers 23'),  ('entr1', 'option2', 'chef 3', 'pers 24'),
     
      ('entr1', 'option2', 'chef 4', 'pers 25'),  ('entr1', 'option2', 'chef 4', 'pers 26'),  ('entr1', 'option2', 'chef 4', 'pers 27'),
      ('entr1', 'option2', 'chef 4', 'pers 28'),  ('entr1', 'option2', 'chef 4', 'pers 29'),  ('entr1', 'option2', 'chef 4', 'pers 30'),
      ('entr1', 'option2', 'chef 4', 'pers 31'),  ('entr1', 'option2', 'chef 4', 'pers 32'),
     
      ('entr2', 'option3', 'chef 5', 'pers 33'),  ('entr2', 'option3', 'chef 5', 'pers 34'),  ('entr2', 'option3', 'chef 5', 'pers 35'),
      ('entr2', 'option3', 'chef 5', 'pers 36'),  ('entr2', 'option3', 'chef 5', 'pers 37'),  ('entr2', 'option3', 'chef 5', 'pers 38'),
      ('entr2', 'option3', 'chef 5', 'pers 39'),  ('entr2', 'option3', 'chef 5', 'pers 40'),
     
      ('entr2', 'option3', 'chef 6', 'pers 41'),  ('entr2', 'option3', 'chef 6', 'pers 42'),  ('entr2', 'option3', 'chef 6', 'pers 43'),
      ('entr2', 'option3', 'chef 6', 'pers 44'),  ('entr2', 'option3', 'chef 6', 'pers 45'),  ('entr2', 'option3', 'chef 6', 'pers 46'),
      ('entr2', 'option3', 'chef 6', 'pers 47'),  ('entr2', 'option3', 'chef 6', 'pers 48'),
     
      ('entr2', 'option4', 'chef 7', 'pers 49'),  ('entr2', 'option4', 'chef 7', 'pers 50'),  ('entr2', 'option4', 'chef 7', 'pers 51'),
      ('entr2', 'option4', 'chef 7', 'pers 52'),  ('entr2', 'option4', 'chef 7', 'pers 53'),  ('entr2', 'option4', 'chef 7', 'pers 54'),
      ('entr2', 'option4', 'chef 7', 'pers 55'),  ('entr2', 'option4', 'chef 7', 'pers 56'),
     
      ('entr2', 'option4', 'chef 8', 'pers 57'),  ('entr2', 'option4', 'chef 8', 'pers 58'),  ('entr2', 'option4', 'chef 8', 'pers 59'),
      ('entr2', 'option4', 'chef 8', 'pers 60'),  ('entr2', 'option4', 'chef 8', 'pers 61'),  ('entr2', 'option4', 'chef 8', 'pers 62'),
      ('entr2', 'option4', 'chef 8', 'pers 63'),  ('entr2', 'option4', 'chef 8', 'pers 64')
    --------------
     
    --------------
    select * from `test`
    --------------
     
    +----+------------+---------+--------+----------+
    | id | entreprise | option  | chef   | personne |
    +----+------------+---------+--------+----------+
    |  1 | entr1      | option1 | chef 1 | pers  1  |
    |  2 | entr1      | option1 | chef 1 | pers  2  |
    |  3 | entr1      | option1 | chef 1 | pers  3  |
    |  4 | entr1      | option1 | chef 1 | pers  4  |
    |  5 | entr1      | option1 | chef 1 | pers  5  |
    |  6 | entr1      | option1 | chef 1 | pers  6  |
    |  7 | entr1      | option1 | chef 1 | pers  7  |
    |  8 | entr1      | option1 | chef 1 | pers  8  |
    |  9 | entr1      | option1 | chef 2 | pers  9  |
    | 10 | entr1      | option1 | chef 2 | pers 10  |
    | 11 | entr1      | option1 | chef 2 | pers 11  |
    | 12 | entr1      | option1 | chef 2 | pers 12  |
    | 13 | entr1      | option1 | chef 2 | pers 13  |
    | 14 | entr1      | option1 | chef 2 | pers 14  |
    | 15 | entr1      | option1 | chef 2 | pers 15  |
    | 16 | entr1      | option1 | chef 2 | pers 16  |
    | 17 | entr1      | option2 | chef 3 | pers 17  |
    | 18 | entr1      | option2 | chef 3 | pers 18  |
    | 19 | entr1      | option2 | chef 3 | pers 19  |
    | 20 | entr1      | option2 | chef 3 | pers 20  |
    | 21 | entr1      | option2 | chef 3 | pers 21  |
    | 22 | entr1      | option2 | chef 3 | pers 22  |
    | 23 | entr1      | option2 | chef 3 | pers 23  |
    | 24 | entr1      | option2 | chef 3 | pers 24  |
    | 25 | entr1      | option2 | chef 4 | pers 25  |
    | 26 | entr1      | option2 | chef 4 | pers 26  |
    | 27 | entr1      | option2 | chef 4 | pers 27  |
    | 28 | entr1      | option2 | chef 4 | pers 28  |
    | 29 | entr1      | option2 | chef 4 | pers 29  |
    | 30 | entr1      | option2 | chef 4 | pers 30  |
    | 31 | entr1      | option2 | chef 4 | pers 31  |
    | 32 | entr1      | option2 | chef 4 | pers 32  |
    | 33 | entr2      | option3 | chef 5 | pers 33  |
    | 34 | entr2      | option3 | chef 5 | pers 34  |
    | 35 | entr2      | option3 | chef 5 | pers 35  |
    | 36 | entr2      | option3 | chef 5 | pers 36  |
    | 37 | entr2      | option3 | chef 5 | pers 37  |
    | 38 | entr2      | option3 | chef 5 | pers 38  |
    | 39 | entr2      | option3 | chef 5 | pers 39  |
    | 40 | entr2      | option3 | chef 5 | pers 40  |
    | 41 | entr2      | option3 | chef 6 | pers 41  |
    | 42 | entr2      | option3 | chef 6 | pers 42  |
    | 43 | entr2      | option3 | chef 6 | pers 43  |
    | 44 | entr2      | option3 | chef 6 | pers 44  |
    | 45 | entr2      | option3 | chef 6 | pers 45  |
    | 46 | entr2      | option3 | chef 6 | pers 46  |
    | 47 | entr2      | option3 | chef 6 | pers 47  |
    | 48 | entr2      | option3 | chef 6 | pers 48  |
    | 49 | entr2      | option4 | chef 7 | pers 49  |
    | 50 | entr2      | option4 | chef 7 | pers 50  |
    | 51 | entr2      | option4 | chef 7 | pers 51  |
    | 52 | entr2      | option4 | chef 7 | pers 52  |
    | 53 | entr2      | option4 | chef 7 | pers 53  |
    | 54 | entr2      | option4 | chef 7 | pers 54  |
    | 55 | entr2      | option4 | chef 7 | pers 55  |
    | 56 | entr2      | option4 | chef 7 | pers 56  |
    | 57 | entr2      | option4 | chef 8 | pers 57  |
    | 58 | entr2      | option4 | chef 8 | pers 58  |
    | 59 | entr2      | option4 | chef 8 | pers 59  |
    | 60 | entr2      | option4 | chef 8 | pers 60  |
    | 61 | entr2      | option4 | chef 8 | pers 61  |
    | 62 | entr2      | option4 | chef 8 | pers 62  |
    | 63 | entr2      | option4 | chef 8 | pers 63  |
    | 64 | entr2      | option4 | chef 8 | pers 64  |
    +----+------------+---------+--------+----------+
    --------------
    drop view if exists `vue`
    --------------
     
    --------------
    create view `vue` as
      select  col1, col2, col3, group_concat(col4) as col4
     
        from  (    select  concat(t1.`entreprise`, ':')                                     as col1,
                           concat(t1.`option`, ':')                                         as col2,
                           concat(t1.`chef`, ':')                                           as col3,
                                  t1.`personne`                                             as col4,
                           ((count(case when t1.id < t2.id then 1 else 0 end) -1) div 3) +1 as rang
     
                     from  test as t1
     
          left outer join  test as t2
                       on  t2.entreprise  = t1.entreprise
                      and  t2.option      = t1.option
                      and  t2.chef        = t1.chef
                      and  t2.id         <= t1.id
     
                 group by  t1.`entreprise`, t1.`option`, t1.`chef`, t1.`personne`
     
              ) as x
    group by  col1,col2,col3,rang
    --------------
     
    --------------
    select * from vue
    --------------
     
    +--------+----------+---------+-------------------------+
    | col1   | col2     | col3    | col4                    |
    +--------+----------+---------+-------------------------+
    | entr1: | option1: | chef 1: | pers  1,pers  2,pers  3 |
    | entr1: | option1: | chef 1: | pers  4,pers  5,pers  6 |
    | entr1: | option1: | chef 1: | pers  7,pers  8         |
    | entr1: | option1: | chef 2: | pers 11,pers  9,pers 10 |
    | entr1: | option1: | chef 2: | pers 12,pers 13,pers 14 |
    | entr1: | option1: | chef 2: | pers 15,pers 16         |
    | entr1: | option2: | chef 3: | pers 17,pers 18,pers 19 |
    | entr1: | option2: | chef 3: | pers 20,pers 21,pers 22 |
    | entr1: | option2: | chef 3: | pers 23,pers 24         |
    | entr1: | option2: | chef 4: | pers 27,pers 25,pers 26 |
    | entr1: | option2: | chef 4: | pers 28,pers 29,pers 30 |
    | entr1: | option2: | chef 4: | pers 31,pers 32         |
    | entr2: | option3: | chef 5: | pers 33,pers 34,pers 35 |
    | entr2: | option3: | chef 5: | pers 36,pers 37,pers 38 |
    | entr2: | option3: | chef 5: | pers 39,pers 40         |
    | entr2: | option3: | chef 6: | pers 43,pers 41,pers 42 |
    | entr2: | option3: | chef 6: | pers 44,pers 45,pers 46 |
    | entr2: | option3: | chef 6: | pers 47,pers 48         |
    | entr2: | option4: | chef 7: | pers 49,pers 50,pers 51 |
    | entr2: | option4: | chef 7: | pers 52,pers 53,pers 54 |
    | entr2: | option4: | chef 7: | pers 55,pers 56         |
    | entr2: | option4: | chef 8: | pers 59,pers 57,pers 58 |
    | entr2: | option4: | chef 8: | pers 60,pers 61,pers 62 |
    | entr2: | option4: | chef 8: | pers 63,pers 64         |
    +--------+----------+---------+-------------------------+
    --------------
    select  col1, col2, col3, col4
      from  (  select      if(col1=@p1,'',col1)  as col1,
                           if(col2=@p2,'',col2)  as col2,
                           if(col3=@p3,'',col3)  as col3,
                           col4,
                           @p1:=col1,@p2:=col2,@p3:=col3
                     from  vue
               cross join  (select @p1:='',@p2:='',@p3:='') as x
            ) as y
    --------------
     
    +--------+----------+---------+-------------------------+
    | col1   | col2     | col3    | col4                    |
    +--------+----------+---------+-------------------------+
    | entr1: | option1: | chef 1: | pers  1,pers  2,pers  3 |
    |        |          |         | pers  4,pers  5,pers  6 |
    |        |          |         | pers  7,pers  8         |
    |        |          | chef 2: | pers 11,pers  9,pers 10 |
    |        |          |         | pers 12,pers 13,pers 14 |
    |        |          |         | pers 15,pers 16         |
    |        | option2: | chef 3: | pers 17,pers 18,pers 19 |
    |        |          |         | pers 20,pers 21,pers 22 |
    |        |          |         | pers 23,pers 24         |
    |        |          | chef 4: | pers 27,pers 25,pers 26 |
    |        |          |         | pers 28,pers 29,pers 30 |
    |        |          |         | pers 31,pers 32         |
    | entr2: | option3: | chef 5: | pers 33,pers 34,pers 35 |
    |        |          |         | pers 36,pers 37,pers 38 |
    |        |          |         | pers 39,pers 40         |
    |        |          | chef 6: | pers 43,pers 41,pers 42 |
    |        |          |         | pers 44,pers 45,pers 46 |
    |        |          |         | pers 47,pers 48         |
    |        | option4: | chef 7: | pers 49,pers 50,pers 51 |
    |        |          |         | pers 52,pers 53,pers 54 |
    |        |          |         | pers 55,pers 56         |
    |        |          | chef 8: | pers 59,pers 57,pers 58 |
    |        |          |         | pers 60,pers 61,pers 62 |
    |        |          |         | pers 63,pers 64         |
    +--------+----------+---------+-------------------------+
    --------------
    COMMIT
    --------------
     
    --------------
    SET AUTOCOMMIT = 1
    --------------
     
    Appuyez sur une touche pour continuer...
    Soit trois personnes par ligne !

    Sinon, il faudrait être plus précis dans la présentation du résultat final.

    @+
    Si vous êtes de mon aide, vous pouvez cliquer sur .
    Mon site : http://www.jcz.fr

  7. #7
    Membre du Club
    Profil pro
    Inscrit en
    Octobre 2010
    Messages
    163
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Octobre 2010
    Messages : 163
    Points : 59
    Points
    59
    Par défaut
    Waouw... Impressionnant... <:-) Merci ! Je vais regarder tout cela attentivement !

  8. #8
    Expert éminent sénior Avatar de Artemus24
    Homme Profil pro
    Agent secret au service du président Ulysses S. Grant !
    Inscrit en
    Février 2011
    Messages
    6 381
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Agent secret au service du président Ulysses S. Grant !
    Secteur : Finance

    Informations forums :
    Inscription : Février 2011
    Messages : 6 381
    Points : 19 065
    Points
    19 065
    Par défaut
    Salut trucmuche2005.

    Citation Envoyé par trucmuche2005
    il me semble que ta solution se transpose difficilement.
    Déjà, il ne faut pas être défaitiste ! Ensuite, un problème, ça se travaille, jusqu'à obtenir quelque chose qui peut convenir.

    La vue, je l'ai modifié afin de préparer d'une part les quatre colonnes et d'autre part de faire un group_concat sur la quatrième colonne selon une périodicité.
    Dans mon exemple, j'ai pris huit personnes avec une périodicité de trois personnes par lignes.
    Le count numérote de 0 à N chaque triplé (entreprise ; option ; chef).
    Ensuite, je prendre la partie entière de cette numérotation en le divisant par 3.
    Ce qui donne : 0 --> 0, 1 --> 0, 2 --> 0, 3 --> 1, 4 --> 1, 5 --> 1, 6 --> 2, et ainsi de suite.
    Comme on le constate, ce calcul donne trois fois la même valeurs, d'où le regroupement qui permet de mettre trois personnes par ligne.

    Citation Envoyé par trucmuche2005
    comme le nombre de personnes par chef n'est pas toujours le même (il peut y en avoir 1 comme il peut y en avoir 100)
    Vous auriez dû commencer par détailler un peu plus votre problème, et non vous arrêter à une question de présentation.

    J'espère que cela répond à votre attente !
    Mettre le sujet à résolu, svp !

    @+
    Si vous êtes de mon aide, vous pouvez cliquer sur .
    Mon site : http://www.jcz.fr

Discussions similaires

  1. Extraire des données d'une page Web en VBA sous Excel
    Par BEMI dans le forum Macros et VBA Excel
    Réponses: 4
    Dernier message: 20/05/2009, 06h24
  2. [RegEx] Comment extraire des données d'une page HTML ?
    Par taliesin26 dans le forum Langage
    Réponses: 1
    Dernier message: 11/12/2006, 12h39
  3. [VB.Net] recuperer des données sous la forme 0001
    Par eown dans le forum Windows Forms
    Réponses: 4
    Dernier message: 17/05/2006, 12h32
  4. [Système] Extraire des données d'une page web
    Par nazoreen dans le forum Langage
    Réponses: 17
    Dernier message: 22/03/2006, 21h38
  5. Réponses: 1
    Dernier message: 28/09/2005, 15h35

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