<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Philben - Ms Access &#187; Transposer</title>
	<atom:link href="https://blog.developpez.com/philben/ptag/transposer/feed" rel="self" type="application/rss+xml" />
	<link>https://blog.developpez.com/philben</link>
	<description></description>
	<lastBuildDate>Thu, 26 Sep 2013 19:43:53 +0000</lastBuildDate>
	<language>fr-FR</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.1.42</generator>
	<item>
		<title>Transposer des lignes en une colonne</title>
		<link>https://blog.developpez.com/philben/p11320/vba-access/transposer-des-lignes-en-une-colonne</link>
		<comments>https://blog.developpez.com/philben/p11320/vba-access/transposer-des-lignes-en-une-colonne#comments</comments>
		<pubDate>Sun, 16 Sep 2012 08:34:58 +0000</pubDate>
		<dc:creator><![CDATA[philben]]></dc:creator>
				<category><![CDATA[SQL - Ms Access]]></category>
		<category><![CDATA[VBA - Ms Access]]></category>
		<category><![CDATA[Chaîne de caractères]]></category>
		<category><![CDATA[Code VBA]]></category>
		<category><![CDATA[Requête SQL]]></category>
		<category><![CDATA[Transposer]]></category>

		<guid isPermaLink="false">http://blog.developpez.com/philben/?p=193</guid>
		<description><![CDATA[On a parfois besoin de concaténer dans une colonne le contenu de plusieurs lignes d&#8217;une table ou d&#8217;une requête. Par exemple, On part de : pour arriver à : Je vous propose une fonction écrite en VBA qui réalise cette &#8230; <a href="https://blog.developpez.com/philben/p11320/vba-access/transposer-des-lignes-en-une-colonne">Lire la suite <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>On a parfois besoin de concaténer dans une colonne le contenu de plusieurs lignes d&rsquo;une table ou d&rsquo;une requête.<br />
<span id="more-193"></span><br />
<strong>Par exemple</strong>, On part de :<br />
<img src="http://philben.developpez.com/ConcatenationTable.png" alt="Concaténation de la table" /><br />
<br />
pour arriver à :<br />
<img src="http://philben.developpez.com/ConcatenationResultat.png" alt="Résultat de la concaténation" /><br />
<br />
Je vous propose une fonction écrite en VBA qui réalise cette transposition.</p>
<p><strong>Code de la fonction VBA</strong></p>
<div class="codecolorer-container vb blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:400px;"><div class="vb codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #008000;">'----------------------------------------------------------------------------------------------------------<br />
</span><span style="color: #008000;">' Procédure &nbsp; &nbsp;: ConcatColonne &nbsp; [Function]<br />
</span><span style="color: #008000;">' Retour &nbsp; &nbsp; &nbsp; : String<br />
</span><span style="color: #008000;">' Auteur &nbsp; &nbsp; &nbsp; : PhilBen - Free to use<br />
</span><span style="color: #008000;">' Version &nbsp; &nbsp; &nbsp;: 1.06<br />
</span><span style="color: #008000;">' Création/Maj : Samedi 15 septembre 2012<br />
</span><span style="color: #008000;">' Objet &nbsp; &nbsp; &nbsp; &nbsp;: Permet de concaténer en lignes les données d'une colonne en fonction d'un pivot<br />
</span><span style="color: #008000;">' Arguments &nbsp; &nbsp;: - ValeurPivot : La valeur de la colonne pivot<br />
</span><span style="color: #008000;">' &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: - NomColonnePivot : Nom de la colonne pivot<br />
</span><span style="color: #008000;">' &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: - NomColonneConcat : Nom de la colonne à concaténer<br />
</span><span style="color: #008000;">' &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: - NomDomaine : Table ou requête des colonnes<br />
</span><span style="color: #008000;">' &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: - Filtre : Permet d'appliquer un filtre supplémentaire sur le domaine (&quot;&quot; pour aucun)<br />
</span><span style="color: #008000;">' &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: - RegrouperCompter : Regrouper (=1 ou &gt;0 et 2) ET Compter (=2) ou pas de regroupement (=0) des éléments concaténés<br />
</span><span style="color: #008000;">' &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: - PasVideNULL : ne retourne pas les éléments concaténés vide &quot;&quot; et NULL (=True) sinon False<br />
</span><span style="color: #008000;">' &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: - TriAscendant : Tri Ascendant (=True), Descendant (=False)<br />
</span><span style="color: #008000;">' &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: - Separateur : String qui sépare les éléments concaténés (ex: &quot;,&quot;,&quot;---&quot;,...)<br />
</span><span style="color: #008000;">' Remarques &nbsp; &nbsp;: * Cette fonction ralentit sensiblement la requête :<br />
</span><span style="color: #008000;">' &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-&gt; Meilleure performance si au moins la colonne pivot est indexée avec doublons<br />
</span><span style="color: #008000;">' &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: * Le type de la colonne pivot peut être Date, numérique, string<br />
</span><span style="color: #008000;">' &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: * Pour des raisons de performance, éviter de faire un filtre, un tri, <br />
</span><span style="color: #008000;">' &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: &nbsp; un regroupement sur la colonne de concaténation, la déclarer si possible 'Expression' !<br />
</span><span style="color: #008000;">' Exemple &nbsp; &nbsp; &nbsp;: SELECT [MaColPivot],<br />
</span><span style="color: #008000;">' &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: ConcatColonne([MaColPivot],&quot;MaColPivot&quot;,&quot;MaColConcat&quot;,&quot;MaTable&quot;,&quot;&quot;,1,True,True,&quot;, &quot;)<br />
</span><span style="color: #008000;">' &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: FROM MaTable Group By [MaColPivot]<br />
</span><span style="color: #008000;">' Historique &nbsp; : 1.03 : Correction bug si le pivot est de type date<br />
</span><span style="color: #008000;">' &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: 1.04 : Complément d'information dans l'en-tête de la fonction<br />
</span><span style="color: #008000;">' &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: 1.05 : Correction bug si Pivot numérique avec décimales (, -&gt; .)<br />
</span><span style="color: #008000;">' &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: 1.06 : Ajout compte des données regroupées + modification des paramètres (type et nom)<br />
</span><span style="color: #008000;">'----------------------------------------------------------------------------------------------------------<br />
</span><span style="color: #E56717; font-weight: bold;">Public</span> <span style="color: #E56717; font-weight: bold;">Function</span> ConcatColonne(<span style="color: #151B8D; font-weight: bold;">ByVal</span> ValeurColonnePivot <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Variant</span>, _<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #151B8D; font-weight: bold;">ByVal</span> NomColonnePivot <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">String</span>, _<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #151B8D; font-weight: bold;">ByVal</span> NomColonneConcat <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">String</span>, _<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #151B8D; font-weight: bold;">ByVal</span> NomDomaine <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">String</span>, _<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #151B8D; font-weight: bold;">Optional</span> <span style="color: #151B8D; font-weight: bold;">ByVal</span> Filtre <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">String</span> = vbNullString, _<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #151B8D; font-weight: bold;">Optional</span> <span style="color: #151B8D; font-weight: bold;">ByVal</span> RegrouperCompter <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Integer</span> = 1, _<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #151B8D; font-weight: bold;">Optional</span> <span style="color: #151B8D; font-weight: bold;">ByVal</span> PasVideNULL <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Boolean</span> = <span style="color: #00C2FF; font-weight: bold;">True</span>, _<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #151B8D; font-weight: bold;">Optional</span> <span style="color: #151B8D; font-weight: bold;">ByVal</span> TriAscendant <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Boolean</span> = <span style="color: #00C2FF; font-weight: bold;">True</span>, _<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #151B8D; font-weight: bold;">Optional</span> <span style="color: #151B8D; font-weight: bold;">ByVal</span> Separateur <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">String</span> = <span style="color: #800000;">&quot;, &quot;</span>) <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">String</span><br />
&nbsp; &nbsp;<span style="color: #151B8D; font-weight: bold;">On</span> <span style="color: #151B8D; font-weight: bold;">Error</span> <span style="color: #8D38C9; font-weight: bold;">GoTo</span> Catch<br />
&nbsp; &nbsp;<span style="color: #151B8D; font-weight: bold;">Dim</span> oDb <span style="color: #151B8D; font-weight: bold;">As</span> DAO.Database, oRs <span style="color: #151B8D; font-weight: bold;">As</span> DAO.Recordset, sSQL <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">String</span><br />
<br />
&nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">If</span> <span style="color: #8D38C9; font-weight: bold;">Not</span> IsNull(ValeurColonnePivot) <span style="color: #8D38C9; font-weight: bold;">Then</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">If</span> RegrouperCompter = 2 <span style="color: #8D38C9; font-weight: bold;">Then</span> &nbsp; &nbsp; &nbsp;<span style="color: #008000;">'regrouper ET compter<br />
</span> &nbsp; &nbsp; &nbsp; &nbsp; sSQL = NomColonneConcat &amp; <span style="color: #800000;">&quot; &amp; &quot;</span><span style="color: #800000;">&quot; (&quot;</span><span style="color: #800000;">&quot; &amp; &quot;</span> &amp; <span style="color: #800000;">&quot;COUNT(*)&quot;</span> &amp; <span style="color: #800000;">&quot; &amp; &quot;</span><span style="color: #800000;">&quot;)&quot;</span><span style="color: #800000;">&quot; &amp; &quot;</span> &amp; <span style="color: #800000;">&quot;&quot;</span><span style="color: #800000;">&quot;&quot;</span> &amp; Separateur &amp; <span style="color: #800000;">&quot;&quot;</span><span style="color: #800000;">&quot; As C&quot;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">Else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sSQL = NomColonneConcat &amp; <span style="color: #800000;">&quot; &amp; &quot;</span><span style="color: #800000;">&quot;&quot;</span> &amp; Separateur &amp; <span style="color: #800000;">&quot;&quot;</span><span style="color: #800000;">&quot; As C&quot;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #8D38C9; font-weight: bold;">If</span><br />
&nbsp; &nbsp; &nbsp; sSQL = <span style="color: #800000;">&quot;SELECT &quot;</span> &amp; sSQL &amp; <span style="color: #800000;">&quot; FROM &quot;</span> &amp; NomDomaine &amp; <span style="color: #800000;">&quot; WHERE &quot;</span> &amp; NomColonnePivot &amp; <span style="color: #800000;">&quot;=&quot;</span><br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">Select</span> <span style="color: #8D38C9; font-weight: bold;">Case</span> VarType(ValeurColonnePivot)<br />
&nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">Case</span> vbString<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sSQL = sSQL &amp; <span style="color: #800000;">&quot;&quot;</span><span style="color: #800000;">&quot;&quot;</span> &amp; ValeurColonnePivot &amp; <span style="color: #800000;">&quot;&quot;</span><span style="color: #800000;">&quot;&quot;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">Case</span> vbDate<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sSQL = sSQL &amp; Format(ValeurColonnePivot, <span style="color: #800000;">&quot;\#m-d-yyyy h:n:s\#&quot;</span>)<br />
&nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">Case</span> <span style="color: #8D38C9; font-weight: bold;">Else</span> &nbsp; <span style="color: #008000;">'Numériques<br />
</span> &nbsp; &nbsp; &nbsp; &nbsp; sSQL = sSQL &amp; Replace(ValeurColonnePivot, <span style="color: #800000;">&quot;,&quot;</span>, <span style="color: #800000;">&quot;.&quot;</span>)<br />
&nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #8D38C9; font-weight: bold;">Select</span><br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">If</span> Filtre &lt;&gt; vbNullString <span style="color: #8D38C9; font-weight: bold;">Then</span> sSQL = sSQL &amp; <span style="color: #800000;">&quot; And &quot;</span> &amp; Filtre<br />
&nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">If</span> PasVideNULL = <span style="color: #00C2FF; font-weight: bold;">True</span> <span style="color: #8D38C9; font-weight: bold;">Then</span> sSQL = sSQL &amp; <span style="color: #800000;">&quot; AND LEN(NZ(&quot;</span> &amp; NomColonneConcat &amp; <span style="color: #800000;">&quot;))&gt;0&quot;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">If</span> RegrouperCompter &gt; 0 <span style="color: #8D38C9; font-weight: bold;">Then</span> sSQL = sSQL &amp; <span style="color: #800000;">&quot; GROUP BY &quot;</span> &amp; NomColonneConcat<br />
&nbsp; &nbsp; &nbsp; sSQL = sSQL &amp; <span style="color: #800000;">&quot; ORDER BY &quot;</span> &amp; NomColonneConcat<br />
&nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">If</span> <span style="color: #8D38C9; font-weight: bold;">Not</span> TriAscendant <span style="color: #8D38C9; font-weight: bold;">Then</span> sSQL = sSQL &amp; <span style="color: #800000;">&quot; DESC&quot;</span><br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color: #008000;">'Lance la requête et concatène les lignes<br />
</span> &nbsp; &nbsp; &nbsp;<span style="color: #151B8D; font-weight: bold;">Set</span> oDb = CurrentDb<br />
&nbsp; &nbsp; &nbsp; <span style="color: #151B8D; font-weight: bold;">Set</span> oRs = oDb.OpenRecordset(sSQL, dbOpenSnapshot)<br />
&nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">If</span> <span style="color: #8D38C9; font-weight: bold;">Not</span> oRs.EOF <span style="color: #8D38C9; font-weight: bold;">Then</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">Do</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ConcatColonne = ConcatColonne &amp; oRs(0)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; oRs.MoveNext<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">Loop</span> <span style="color: #8D38C9; font-weight: bold;">Until</span> oRs.EOF<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ConcatColonne = Left$(ConcatColonne, Len(ConcatColonne) - Len(Separateur))<br />
&nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #8D38C9; font-weight: bold;">If</span><br />
&nbsp; &nbsp; &nbsp; oRs.<span style="color: #8D38C9; font-weight: bold;">Close</span><br />
&nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #8D38C9; font-weight: bold;">If</span><br />
Finally:<br />
&nbsp; &nbsp;<span style="color: #151B8D; font-weight: bold;">Set</span> oRs = <span style="color: #00C2FF; font-weight: bold;">Nothing</span><br />
&nbsp; &nbsp;<span style="color: #151B8D; font-weight: bold;">Set</span> oDb = <span style="color: #00C2FF; font-weight: bold;">Nothing</span><br />
&nbsp; &nbsp;<span style="color: #E56717; font-weight: bold;">Exit</span> <span style="color: #E56717; font-weight: bold;">Function</span><br />
Catch:<br />
&nbsp; &nbsp;ConcatColonne = <span style="color: #800000;">&quot;Erreur !&quot;</span><br />
&nbsp; &nbsp;<span style="color: #151B8D; font-weight: bold;">Resume</span> Finally<br />
<span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #E56717; font-weight: bold;">Function</span></div></div>
<p>&nbsp;<br />
<strong>Exemples d&rsquo;utilisation</strong><br />
Un collectionneur de modèles réduits, a noté consciencieusement ses achats de lots de voitures sur une feuille quadrillée. Un ami, utilisateur débutant d&rsquo;Access, a porté ce document dans une table Access sans connaître les formes normales de Codd !</p>
<p>Voici le code pour créér et peupler partiellement la table dont la structure n&rsquo;est pas à suivre&#8230;</p>
<div class="codecolorer-container vb blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="vb codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #E56717; font-weight: bold;">Public</span> <span style="color: #E56717; font-weight: bold;">Function</span> CreationTableConcat()<br />
&nbsp; &nbsp;Const cInsert <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">String</span> = <span style="color: #800000;">&quot;INSERT INTO tConcat (Marque, Modele, DateAchat, Couleur, Nombre, Prix) VALUES &quot;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">With</span> DoCmd<br />
&nbsp; &nbsp; &nbsp; .SetWarnings <span style="color: #00C2FF; font-weight: bold;">False</span><br />
<br />
&nbsp; &nbsp; &nbsp; .RunSQL <span style="color: #800000;">&quot;CREATE TABLE tConcat (Marque VARCHAR, Modele VARCHAR, DateAchat DATE, Couleur VARCHAR, Nombre LONG, Prix DOUBLE);&quot;</span><br />
&nbsp; &nbsp; &nbsp; .RunSQL <span style="color: #800000;">&quot;CREATE INDEX IdxMarque ON tConcat (Marque);&quot;</span><br />
<br />
&nbsp; &nbsp; &nbsp; .RunSQL cInsert &amp; <span style="color: #800000;">&quot;('Citroën','DS',#6/3/1993#,'blanc',2,85.25);&quot;</span><br />
&nbsp; &nbsp; &nbsp; .RunSQL cInsert &amp; <span style="color: #800000;">&quot;('Citroën','DS',#2/13/1999#,'blanc',5,405.3);&quot;</span><br />
&nbsp; &nbsp; &nbsp; .RunSQL cInsert &amp; <span style="color: #800000;">&quot;('Peugeot','205',#11/18/1998#,'vert',1,31.2);&quot;</span><br />
&nbsp; &nbsp; &nbsp; .RunSQL cInsert &amp; <span style="color: #800000;">&quot;('Peugeot','404',#03/25/1978#,'noir',3,NULL);&quot;</span><br />
&nbsp; &nbsp; &nbsp; .RunSQL cInsert &amp; <span style="color: #800000;">&quot;('Peugeot','205',#12/07/1997#,'bleu',1,31.2);&quot;</span><br />
&nbsp; &nbsp; &nbsp; .RunSQL cInsert &amp; <span style="color: #800000;">&quot;('Peugeot','404',#03/25/1978#,'marron',1,42.1);&quot;</span><br />
&nbsp; &nbsp; &nbsp; .RunSQL cInsert &amp; <span style="color: #800000;">&quot;('Renault','R5',#01/28/1982#,'rouge',5,255.5);&quot;</span><br />
&nbsp; &nbsp; &nbsp; .RunSQL cInsert &amp; <span style="color: #800000;">&quot;('Renault','R4',#07/11/1991#,'bleu',3,159.9);&quot;</span><br />
&nbsp; &nbsp; &nbsp; .RunSQL cInsert &amp; <span style="color: #800000;">&quot;('Renault','R4',#07/11/1991#,'rouge',5,238.75);&quot;</span><br />
<br />
&nbsp; &nbsp; &nbsp; .SetWarnings <span style="color: #00C2FF; font-weight: bold;">True</span><br />
&nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #8D38C9; font-weight: bold;">With</span><br />
<br />
&nbsp; &nbsp;MsgBox <span style="color: #800000;">&quot;Création terminée&quot;</span><br />
<span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #E56717; font-weight: bold;">Function</span></div></div>
<p>Cette table contient 6 colonnes (Marque, Modèle, DateAchat, Couleur des voitures, Nombre de voiture du lot, Prix du lot).<br />
Chaque ligne de la table correspond à l&rsquo;achat d&rsquo;un lot de voitures et toutes les voitures d&rsquo;un lot ont la même couleur.</p>
<p>Le collectionneur souhaite obtenir des infos sur sa collection en concaténant l&rsquo;information dans une colonne car c&rsquo;est plus simple à lire&#8230;</p>
<p><strong>Quels sont les modèles achetés de la marque Peugeot ?</strong><br />
Comme la colonne pivot &lsquo;Marque&rsquo; doit être égale à &lsquo;Peugeot&rsquo;, on peut écrire la simple requête suivante :</p>
<div class="codecolorer-container sql blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #ff0000;">&quot;Peugeot&quot;</span> <span style="color: #993333; font-weight: bold;">AS</span> Marque<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;ConcatColonne<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;peugeot&quot;</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">&quot;marque&quot;</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #ff0000;">&quot;modele&quot;</span><span style="color: #66cc66;">,</span> &nbsp;<span style="color: #ff0000;">&quot;tConcat&quot;</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #ff0000;">&quot;&quot;</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">,</span><span style="color: #993333; font-weight: bold;">FALSE</span><span style="color: #66cc66;">,</span><span style="color: #993333; font-weight: bold;">TRUE</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">&quot;,&quot;</span><span style="color: #66cc66;">&#41;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #66cc66;">&#91;</span><span style="color: #993333; font-weight: bold;">MOD</span>èles<span style="color: #66cc66;">&#93;</span></div></div>
<p>La valeur pivot est &lsquo;Peugeot&rsquo;, la colonne pivot (ou de regroupement) est &lsquo;Marque&rsquo;, la colonne à concaténer est &lsquo;Modele&rsquo;, la table est &lsquo;tConcat&rsquo;, la clause Where est vide &laquo;&nbsp;&nbsp;&raquo;, pas de regroupement des lots ayant le même modèle d&rsquo;où la valeur 0, on retourne les modèles dont le nom est vide ou NULL (False), on souhaite trier par ordre Ascendant les modèles concaténés (True), et le séparateur entre les modèles est une virgule &lsquo;,&rsquo;.<br />
<br />
On obtient donc :</p>
<div class="codecolorer-container text geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Marque &nbsp;| Modèles &nbsp; <br />
Peugeot | 205,205,404,404</div></div>
<p>
Le collectionneur est satisfait mais il préfère regrouper les modèles communs et ajouter un espace après la &lsquo;,&rsquo;<br />
La requête devient :</p>
<div class="codecolorer-container sql blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #ff0000;">&quot;Peugeot&quot;</span> <span style="color: #993333; font-weight: bold;">AS</span> Marque<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;ConcatColonne<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;peugeot&quot;</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">&quot;marque&quot;</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #ff0000;">&quot;modele&quot;</span><span style="color: #66cc66;">,</span> &nbsp;<span style="color: #ff0000;">&quot;tConcat&quot;</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #ff0000;">&quot;&quot;</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span><span style="color: #993333; font-weight: bold;">FALSE</span><span style="color: #66cc66;">,</span><span style="color: #993333; font-weight: bold;">TRUE</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">&quot;, &quot;</span><span style="color: #66cc66;">&#41;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #66cc66;">&#91;</span><span style="color: #993333; font-weight: bold;">MOD</span>èles<span style="color: #66cc66;">&#93;</span></div></div>
<p>Pour regrouper les modèles, il suffit de passer à <strong>1</strong> au lieu de <strong>0</strong> le paramètre &lsquo;RegrouperCompter&rsquo;. De plus, le séparateur devient &lsquo;, &lsquo; au lieu de &lsquo;,&rsquo;.<br />
<br />
On obtient alors :</p>
<div class="codecolorer-container text geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Marque &nbsp;| Modèles &nbsp; <br />
Peugeot | 205, 404</div></div>
<p>
Il est satisfait mais il perd une information importante qui est le nombre de lots achetés et finalement il se demande si le séparateur &lsquo; / &lsquo; ne serait pas mieux&#8230;<br />
La solution devient :</p>
<div class="codecolorer-container sql blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #ff0000;">&quot;Peugeot&quot;</span> <span style="color: #993333; font-weight: bold;">AS</span> Marque<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;ConcatColonne<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;peugeot&quot;</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">&quot;marque&quot;</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #ff0000;">&quot;modele&quot;</span><span style="color: #66cc66;">,</span> &nbsp;<span style="color: #ff0000;">&quot;tConcat&quot;</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #ff0000;">&quot;&quot;</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">,</span><span style="color: #993333; font-weight: bold;">FALSE</span><span style="color: #66cc66;">,</span><span style="color: #993333; font-weight: bold;">TRUE</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">&quot; / &quot;</span><span style="color: #66cc66;">&#41;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #66cc66;">&#91;</span><span style="color: #993333; font-weight: bold;">MOD</span>èles<span style="color: #66cc66;">&#93;</span></div></div>
<p>Pour afficher le compte par modèle, on passe la valeur du paramètre &lsquo;RegrouperCompter&rsquo; à <strong>2</strong>, et le séparateur est modifié.<br />
<br />
Résultat :</p>
<div class="codecolorer-container text geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Marque &nbsp;| Modèles &nbsp; <br />
Peugeot | 205 (2) / 404 (2)</div></div>
<p>&nbsp;<br />
<strong>Qu&rsquo;elles sont les dates d&rsquo;achat de ces lots du plus récent au plus ancien ?</strong></p>
<div class="codecolorer-container sql blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #ff0000;">&quot;Peugeot&quot;</span> <span style="color: #993333; font-weight: bold;">AS</span> Marque<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;ConcatColonne<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;peugeot&quot;</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">&quot;marque&quot;</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #ff0000;">&quot;dateachat&quot;</span><span style="color: #66cc66;">,</span> &nbsp;<span style="color: #ff0000;">&quot;tConcat&quot;</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #ff0000;">&quot;&quot;</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">,</span><span style="color: #993333; font-weight: bold;">FALSE</span><span style="color: #66cc66;">,</span><span style="color: #993333; font-weight: bold;">FALSE</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">&quot; ; &quot;</span><span style="color: #66cc66;">&#41;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #66cc66;">&#91;</span>Dates achat<span style="color: #66cc66;">&#93;</span></div></div>
<p>La colonne à concaténer devient &lsquo;DateAchat&rsquo; et le paramètre &lsquo;TriAscendant&rsquo; devient &lsquo;False&rsquo;.<br />
<br />
Résultat :</p>
<div class="codecolorer-container text geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Marque &nbsp;| Dates achat<br />
Peugeot | 18/11/1998 (1) ; 07/12/1997 (1) ; 25/03/1978 (2)</div></div>
<p>&nbsp;<br />
<strong>Quels sont les prix d&rsquo;achat de ces lots ?</strong></p>
<div class="codecolorer-container sql blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #ff0000;">&quot;Peugeot&quot;</span> <span style="color: #993333; font-weight: bold;">AS</span> Marque<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;ConcatColonne<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;peugeot&quot;</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">&quot;marque&quot;</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #ff0000;">&quot;prix&quot;</span> &nbsp; <span style="color: #66cc66;">,</span> <span style="color: #ff0000;">&quot;tConcat&quot;</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #ff0000;">&quot;&quot;</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">,</span><span style="color: #993333; font-weight: bold;">FALSE</span><span style="color: #66cc66;">,</span><span style="color: #993333; font-weight: bold;">FALSE</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">&quot; ; &quot;</span><span style="color: #66cc66;">&#41;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #66cc66;">&#91;</span>Prix<span style="color: #66cc66;">&#93;</span></div></div>
<p>
On obtient :</p>
<div class="codecolorer-container text geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Marque &nbsp;| Prix<br />
Peugeot | 42,1 (1) ; 31,2 (2) ; &nbsp;(1)</div></div>
<p>On remarque que le dernier compte (1) est sans valeur. En effet, un prix n&rsquo;était pas renseigné dans le document&#8230;<br />
Le collectionneur me dit alors : &lsquo;Cachez-moi cette erreur que je ne saurais voir&#8230;&rsquo;<br />
<br />
La requête devient donc :</p>
<div class="codecolorer-container sql blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #ff0000;">&quot;Peugeot&quot;</span> <span style="color: #993333; font-weight: bold;">AS</span> Marque<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;ConcatColonne<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;peugeot&quot;</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">&quot;marque&quot;</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #ff0000;">&quot;prix&quot;</span> &nbsp; <span style="color: #66cc66;">,</span> <span style="color: #ff0000;">&quot;tConcat&quot;</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #ff0000;">&quot;&quot;</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">,</span><span style="color: #993333; font-weight: bold;">TRUE</span><span style="color: #66cc66;">,</span><span style="color: #993333; font-weight: bold;">FALSE</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">&quot; ; &quot;</span><span style="color: #66cc66;">&#41;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #66cc66;">&#91;</span>Prix<span style="color: #66cc66;">&#93;</span></div></div>
<p>Il suffit de mettre à &lsquo;True&rsquo; le paramètre &lsquo;PasVideNULL&rsquo;.<br />
<br />
On obtient finalement :</p>
<div class="codecolorer-container text geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Marque &nbsp;| Prix<br />
Peugeot | 42,1 (1) ; 31,2 (2)</div></div>
<p>&nbsp;<br />
<strong>Quels sont les prix d&rsquo;achat, toutes marques confondues ?</strong><br />
Il est normalement nécessaire de renseigner la valeur et la colonne du pivot&#8230;<br />
La solution consiste à mettre 0 pour &lsquo;ValeurColonnePivot&rsquo; et &lsquo;0&rsquo; pour &lsquo;NomColonnePivot&rsquo;<br />
La requête devient :</p>
<div class="codecolorer-container sql blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #ff0000;">&quot;Toutes&quot;</span> <span style="color: #993333; font-weight: bold;">AS</span> Marque<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;ConcatColonne<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">&quot;0&quot;</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">&quot;prix&quot;</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">&quot;tConcat&quot;</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #ff0000;">&quot;&quot;</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">,</span><span style="color: #993333; font-weight: bold;">TRUE</span><span style="color: #66cc66;">,</span><span style="color: #993333; font-weight: bold;">FALSE</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">&quot; ; &quot;</span><span style="color: #66cc66;">&#41;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #66cc66;">&#91;</span>Prix<span style="color: #66cc66;">&#93;</span></div></div>
<p>et le résultat :</p>
<div class="codecolorer-container text geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Marque &nbsp;| Prix<br />
Toutes &nbsp;| 405,3 (1) ; 255,5 (1) ; 238,75 (1) ; 159,9 (1) ; 85,25 (1) ; 42,1 (1) ; 31,2 (2)</div></div>
<p>
Ca serait possible d&rsquo;avoir tous les prix unitaires ?<br />
Requête :</p>
<div class="codecolorer-container sql blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #ff0000;">&quot;Toutes&quot;</span> <span style="color: #993333; font-weight: bold;">AS</span> Marque<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;ConcatColonne<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">&quot;0&quot;</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">&quot;[prix]/[nombre]&quot;</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">&quot;tConcat&quot;</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #ff0000;">&quot;&quot;</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">,</span><span style="color: #993333; font-weight: bold;">TRUE</span><span style="color: #66cc66;">,</span><span style="color: #993333; font-weight: bold;">FALSE</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">&quot; - &quot;</span><span style="color: #66cc66;">&#41;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #66cc66;">&#91;</span>Prix<span style="color: #66cc66;">&#93;</span></div></div>
<p>On déclare que la colonne concaténée est la division du prix sur le nombre de voiture du lot.<br />
Résultat :</p>
<div class="codecolorer-container text geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Marque &nbsp;| Prix<br />
Toutes &nbsp;| 81,06 - 53,3 - 51,1 - 47,75 - 42,625 - 42,1 - 31,2</div></div>
<p>&nbsp;<br />
<strong>Quels sont les modèles de chaque marque ?</strong><br />
Cette fois-ci, il faut utiliser la <strong>clause FROM </strong> de la requête pour passer en revue l&rsquo;ensemble des marques :</p>
<div class="codecolorer-container sql blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">SELECT</span> &nbsp; Marque<span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ConcatColonne<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span>marque<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">&quot;marque&quot;</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">&quot;modele&quot;</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">&quot;tConcat&quot;</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">&quot;&quot;</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span><span style="color: #993333; font-weight: bold;">TRUE</span><span style="color: #66cc66;">,</span><span style="color: #993333; font-weight: bold;">TRUE</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">&quot; - &quot;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #66cc66;">&#91;</span><span style="color: #993333; font-weight: bold;">MOD</span>èles par marque<span style="color: #66cc66;">&#93;</span> <br />
<span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; &nbsp; tconcat <br />
<span style="color: #993333; font-weight: bold;">GROUP</span> <span style="color: #993333; font-weight: bold;">BY</span> marque <br />
<span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> marque</div></div>
<p>Résultat :</p>
<div class="codecolorer-container text geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Marque &nbsp;| Modèles par marque<br />
Citroën | DS<br />
Peugeot | 205 - 404<br />
Renault | R4 - R5</div></div>
<p>&nbsp;<br />
<strong>Quels sont les marques et modèles pour chaque couleur ?</strong></p>
<div class="codecolorer-container sql blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">SELECT</span> &nbsp; Couleur<span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ConcatColonne<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span>couleur<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">&quot;couleur&quot;</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">&quot;marque &amp; &quot;</span><span style="color: #ff0000;">&quot; &quot;</span><span style="color: #ff0000;">&quot; &amp; modele&quot;</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">&quot;tConcat&quot;</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">&quot;&quot;</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span><span style="color: #993333; font-weight: bold;">FALSE</span><span style="color: #66cc66;">,</span><span style="color: #993333; font-weight: bold;">TRUE</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">&quot; - &quot;</span><span style="color: #66cc66;">&#41;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #66cc66;">&#91;</span>Marque et <span style="color: #993333; font-weight: bold;">MOD</span>èle<span style="color: #66cc66;">&#93;</span> <br />
<span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; &nbsp; tconcat <br />
<span style="color: #993333; font-weight: bold;">GROUP</span> <span style="color: #993333; font-weight: bold;">BY</span> Couleur <br />
<span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> Couleur</div></div>
<p>Résultat :</p>
<div class="codecolorer-container text geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Couleur | Marque et modèle<br />
blanc &nbsp; | Citroën DS<br />
bleu &nbsp; &nbsp;| Peugeot 205 - Renault R4<br />
marron &nbsp;| Peugeot 404<br />
noir &nbsp; &nbsp;| Peugeot 404<br />
rouge &nbsp; | Renault R4 - Renault R5<br />
vert &nbsp; &nbsp;| Peugeot 205</div></div>
<p>
Puis-je avoir une présentation inversée ?</p>
<div class="codecolorer-container sql blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">SELECT</span> &nbsp; <span style="color: #66cc66;">&#91;</span>Marque<span style="color: #66cc66;">&#93;</span> &amp; <span style="color: #ff0000;">&quot; &quot;</span> &amp; <span style="color: #66cc66;">&#91;</span>modele<span style="color: #66cc66;">&#93;</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #66cc66;">&#91;</span>Marque et <span style="color: #993333; font-weight: bold;">MOD</span>èle<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ConcatColonne<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span>marque<span style="color: #66cc66;">&#93;</span> &amp; <span style="color: #ff0000;">&quot; &quot;</span> &amp; <span style="color: #66cc66;">&#91;</span>Modele<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">&quot;[Marque] &amp; &quot;</span><span style="color: #ff0000;">&quot; &quot;</span><span style="color: #ff0000;">&quot; &amp; [modele]&quot;</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">&quot;couleur&quot;</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">&quot;tConcat&quot;</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">&quot;&quot;</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span><span style="color: #993333; font-weight: bold;">FALSE</span><span style="color: #66cc66;">,</span><span style="color: #993333; font-weight: bold;">TRUE</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">&quot; - &quot;</span><span style="color: #66cc66;">&#41;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #66cc66;">&#91;</span>Couleur<span style="color: #66cc66;">&#93;</span> <br />
<span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; &nbsp; tconcat <br />
<span style="color: #993333; font-weight: bold;">GROUP</span> <span style="color: #993333; font-weight: bold;">BY</span> <span style="color: #66cc66;">&#91;</span>Marque<span style="color: #66cc66;">&#93;</span> &amp; <span style="color: #ff0000;">&quot; &quot;</span> &amp; <span style="color: #66cc66;">&#91;</span>modele<span style="color: #66cc66;">&#93;</span> <br />
<span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> <span style="color: #66cc66;">&#91;</span>Marque<span style="color: #66cc66;">&#93;</span> &amp; <span style="color: #ff0000;">&quot; &quot;</span> &amp; <span style="color: #66cc66;">&#91;</span>modele<span style="color: #66cc66;">&#93;</span></div></div>
<p>qui donne :</p>
<div class="codecolorer-container text geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Marque et modèle | Couleur<br />
Citroën DS &nbsp; &nbsp; &nbsp; | blanc<br />
Peugeot 205 &nbsp; &nbsp; &nbsp;| bleu - vert<br />
Peugeot 404 &nbsp; &nbsp; &nbsp;| marron - noir<br />
Renault R4 &nbsp; &nbsp; &nbsp; | bleu - rouge<br />
Renault R5 &nbsp; &nbsp; &nbsp; | rouge</div></div>
<p>Enfin, je souhaite afficher séparément les colonnes &lsquo;Marque&rsquo; et &lsquo;Modèle&rsquo; et ne pas voir les informations pour &lsquo;Citroën&rsquo;<br />
La requête :</p>
<div class="codecolorer-container sql blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">SELECT</span> &nbsp; Marque<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Modele<span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ConcatColonne<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span>Marque<span style="color: #66cc66;">&#93;</span> &amp; <span style="color: #ff0000;">&quot; &quot;</span> &amp; <span style="color: #66cc66;">&#91;</span>Modele<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #ff0000;">&quot;[Marque] &amp; &quot;</span><span style="color: #ff0000;">&quot; &quot;</span><span style="color: #ff0000;">&quot; &amp; [modele]&quot;</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #ff0000;">&quot;couleur&quot;</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #ff0000;">&quot;tConcat&quot;</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #ff0000;">&quot;Marque &lt;&gt; 'citroën'&quot;</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">FALSE</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">TRUE</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #ff0000;">&quot; - &quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #66cc66;">&#41;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #66cc66;">&#91;</span>Couleur<span style="color: #66cc66;">&#93;</span> <br />
<span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; &nbsp; tconcat <br />
<span style="color: #993333; font-weight: bold;">GROUP</span> <span style="color: #993333; font-weight: bold;">BY</span> Marque<span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Modele<br />
<span style="color: #993333; font-weight: bold;">HAVING</span> &nbsp; Marque <span style="color: #66cc66;">&lt;&gt;</span> <span style="color: #ff0000;">'Citroën'</span><br />
<span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> Marque<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Modele</div></div>
<p>On remarquera l&rsquo;utilisation du paramètre &lsquo;Filtre&rsquo; de la fonction ConcatColonne pour écarter la marque &lsquo;Citroën&rsquo; de la concaténation. Dans ce cas précis, l&rsquo;utilisation du filtre n&rsquo;est pas indispensable puisque la requête écarte elle-même cette marque (voir clause HAVING).<br />
Résultat attendu :</p>
<div class="codecolorer-container text geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Marque &nbsp;| Modele | Couleur<br />
Peugeot | 205 &nbsp; &nbsp;| bleu - vert<br />
Peugeot | 404 &nbsp; &nbsp;| marron - noir<br />
Renault | R4 &nbsp; &nbsp; | bleu - rouge<br />
Renault | R5 &nbsp; &nbsp; | rouge</div></div>
<p>&nbsp;<br />
<strong>Performance</strong><br />
La fonction (et la requête sous-jacente!) est exécutée pour chaque ligne de la table ce qui ralentit sensiblement la requête principale.<br />
Il faudra donc limiter son usage à une table ou requête peu peuplée (&lt; 10 000 lignes ?) et si possible, indexer avec doublons la colonne pivot.<br />
De plus, pour ne pas dégrader les performances, éviter de réaliser des opérations complémentaires sur la colonne concaténée (Where, Order by, Group by) dans la requête principale.</p>
<p><strong>Lien</strong><br />
Voir ce <a href="http://blog.developpez.com/philben/p11140/ms-access-vba/transposer_une_colonne_en_lignes" title="Transposer une colonne en lignes">billet</a> pour réaliser une transposition inverse.</p>
<p>@+</p>
<p>Philippe</p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Transposer une colonne en lignes</title>
		<link>https://blog.developpez.com/philben/p11140/vba-access/transposer_une_colonne_en_lignes</link>
		<comments>https://blog.developpez.com/philben/p11140/vba-access/transposer_une_colonne_en_lignes#comments</comments>
		<pubDate>Sun, 08 Jul 2012 06:02:53 +0000</pubDate>
		<dc:creator><![CDATA[philben]]></dc:creator>
				<category><![CDATA[SQL - Ms Access]]></category>
		<category><![CDATA[VBA - Ms Access]]></category>
		<category><![CDATA[Chaîne de caractères]]></category>
		<category><![CDATA[Code VBA]]></category>
		<category><![CDATA[Requête SQL]]></category>
		<category><![CDATA[Transposer]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Après l&#8217;import d&#8217;un fichier Excel dans une table Access, on souhaitait regrouper et compter les sous-chaînes d&#8217;une colonne par une requête SQL. On part donc de : &#8230;pour arriver à : Générer la table et ses lignes Cette fontion génère &#8230; <a href="https://blog.developpez.com/philben/p11140/vba-access/transposer_une_colonne_en_lignes">Lire la suite <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Après l&rsquo;import d&rsquo;un fichier Excel dans une table Access, on souhaitait regrouper et compter les sous-chaînes d&rsquo;une colonne par une requête SQL.</p>
<p>On part donc de :<br />
<img src="http://philben.developpez.com/TranspositionTable.png" alt="Colonne à transposer" /></p>
<p>&#8230;pour arriver à :<br />
<img src="http://philben.developpez.com/TranspositionResultat.png" alt="Transposition de la colonne en lignes" /><br />
<span id="more-29"></span><br />
<strong>Générer la table et ses lignes</strong><br />
Cette fontion génère la table tTransposition qui est peuplée de 10 000 lignes de textes aléatoires.<br />
Pour l&rsquo;utiliser, copier l&rsquo;ensemble du code dans un module VBA, placer le focus dans la fonction &lsquo;CreationTableTransposition()&rsquo; puis appuyer sur &lsquo;<strong>F5</strong>&lsquo; pour l&rsquo;exécuter.</p>
<div class="codecolorer-container vb blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:400px;"><div class="vb codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #E56717; font-weight: bold;">Public</span> <span style="color: #E56717; font-weight: bold;">Function</span> CreationTableTransposition() <br />
&nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">With</span> DoCmd <br />
&nbsp; &nbsp; &nbsp; .SetWarnings <span style="color: #00C2FF; font-weight: bold;">False</span> <br />
&nbsp; &nbsp; &nbsp; .RunSQL <span style="color: #800000;">&quot;CREATE TABLE tTransposition (Texte VARCHAR NOT NULL);&quot;</span> <br />
&nbsp; &nbsp; &nbsp; .SetWarnings <span style="color: #00C2FF; font-weight: bold;">True</span> <br />
&nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #8D38C9; font-weight: bold;">With</span> <br />
&nbsp; &nbsp;GenereTranspositionTextes <br />
&nbsp;<br />
&nbsp; &nbsp;MsgBox <span style="color: #800000;">&quot;Création terminée&quot;</span> <br />
<span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #E56717; font-weight: bold;">Function</span> <br />
&nbsp;<br />
<span style="color: #E56717; font-weight: bold;">Private</span> <span style="color: #E56717; font-weight: bold;">Function</span> GenereTranspositionTextes() <br />
&nbsp; &nbsp;Const clMaxLignes <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Long</span> = 10000 <br />
&nbsp;<br />
&nbsp; &nbsp;<span style="color: #151B8D; font-weight: bold;">Dim</span> odb <span style="color: #151B8D; font-weight: bold;">As</span> DAO.Database <br />
&nbsp; &nbsp;<span style="color: #151B8D; font-weight: bold;">Dim</span> ors <span style="color: #151B8D; font-weight: bold;">As</span> DAO.Recordset <br />
&nbsp; &nbsp;<span style="color: #151B8D; font-weight: bold;">Dim</span> Texte <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">String</span> <br />
&nbsp; &nbsp;<span style="color: #151B8D; font-weight: bold;">Dim</span> i <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Long</span>, j <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Long</span>, n <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Long</span> <br />
&nbsp;<br />
&nbsp; &nbsp;<span style="color: #151B8D; font-weight: bold;">Set</span> odb = CurrentDb <br />
&nbsp; &nbsp;<span style="color: #151B8D; font-weight: bold;">Set</span> ors = odb.OpenRecordset(<span style="color: #800000;">&quot;tTransposition&quot;</span>, dbOpenDynaset) <br />
&nbsp; &nbsp;Randomize <br />
&nbsp;<br />
&nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">With</span> ors <br />
&nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">For</span> i = 1 <span style="color: #8D38C9; font-weight: bold;">To</span> clMaxLignes <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000;">'construction du texte <br />
</span> &nbsp; &nbsp; &nbsp; &nbsp; n = Int(Rnd() * 9) &nbsp; <span style="color: #008000;">'nombre de separateurs <br />
</span> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">If</span> n &gt; 0 <span style="color: #8D38C9; font-weight: bold;">Then</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Texte = <span style="color: #800000;">&quot;&quot;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">For</span> j = 1 <span style="color: #8D38C9; font-weight: bold;">To</span> n <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Texte = Texte &amp; <span style="color: #F660AB; font-weight: bold;">String</span>$(j, 49 + j - 1) &amp; <span style="color: #800000;">&quot;@&quot;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">Next</span> j <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Texte = Left$(Texte, Len(Texte) - 1) <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">Else</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; j = Int(Rnd() * 9) + 1 <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Texte = <span style="color: #F660AB; font-weight: bold;">String</span>$(j, 49 + j - 1) <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #8D38C9; font-weight: bold;">If</span> <br />
&nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000;">'Ajout de la ligne <br />
</span> &nbsp; &nbsp; &nbsp; &nbsp; .AddNew <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;!Texte = Texte <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.Update <br />
&nbsp;<br />
&nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">Next</span> i <br />
&nbsp; &nbsp; &nbsp; .<span style="color: #8D38C9; font-weight: bold;">Close</span> <br />
&nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #8D38C9; font-weight: bold;">With</span> <br />
&nbsp;<br />
&nbsp; &nbsp;<span style="color: #151B8D; font-weight: bold;">Set</span> ors = <span style="color: #00C2FF; font-weight: bold;">Nothing</span> <br />
&nbsp; &nbsp;<span style="color: #151B8D; font-weight: bold;">Set</span> odb = <span style="color: #00C2FF; font-weight: bold;">Nothing</span> <br />
<span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #E56717; font-weight: bold;">Function</span></div></div>
<p><strong>La trousse à outils VBA</strong><br />
Pour chaque ligne de la table, il faudra compter les occurrences du séparateur &laquo;&nbsp;@&nbsp;&raquo;, déterminer ainsi le nombre de sous-chaînes (nombre de séparateurs + 1) qui seront enfin séparées et extraites.</p>
<p>Par chance, on a déjà présenté dans ce blog deux fonctions personnelles qui remplissent ces tâches :</p>
<div class="codecolorer-container vb blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="vb codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #E56717; font-weight: bold;">Public</span> <span style="color: #E56717; font-weight: bold;">Function</span> CompteOccurrences(<span style="color: #151B8D; font-weight: bold;">ByVal</span> Texte <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">String</span>, <span style="color: #151B8D; font-weight: bold;">ByVal</span> Chaine <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">String</span>, <span style="color: #151B8D; font-weight: bold;">Optional</span> <span style="color: #151B8D; font-weight: bold;">ByVal</span> TypeComparaison <span style="color: #151B8D; font-weight: bold;">As</span> VbCompareMethod = vbBinaryCompare) <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Long</span> <br />
&nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">If</span> Texte &nbsp;vbNullString <span style="color: #8D38C9; font-weight: bold;">Then</span> CompteOccurrences = <span style="color: #151B8D; font-weight: bold;">UBound</span>(Split(Texte, Chaine, -1, TypeComparaison)) <br />
<span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #E56717; font-weight: bold;">Function</span></div></div>
<p>et</p>
<div class="codecolorer-container vb blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:400px;"><div class="vb codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #E56717; font-weight: bold;">Public</span> <span style="color: #E56717; font-weight: bold;">Function</span> Token(<span style="color: #151B8D; font-weight: bold;">ByVal</span> Texte <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">String</span>, <span style="color: #151B8D; font-weight: bold;">ByVal</span> Separateur <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">String</span>, <span style="color: #151B8D; font-weight: bold;">ByVal</span> Numero <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Long</span>, <span style="color: #151B8D; font-weight: bold;">Optional</span> <span style="color: #151B8D; font-weight: bold;">ByVal</span> TypeComparaison <span style="color: #151B8D; font-weight: bold;">As</span> VbCompareMethod = vbBinaryCompare) <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">String</span> <br />
<span style="color: #008000;">'Retourne le token spécifié par son numéro <br />
</span><span style="color: #008000;">'Auteur : Philben - v1.01 <br />
</span><span style="color: #008000;">'Exemple : Token(&quot;a-b-c&quot;,&quot;-&quot;,2) -&gt; b <br />
</span> &nbsp; <span style="color: #151B8D; font-weight: bold;">Dim</span> Tok <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">String</span> <br />
&nbsp; &nbsp;<span style="color: #151B8D; font-weight: bold;">Dim</span> x <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Long</span>, y <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Long</span> <br />
&nbsp;<br />
&nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">Select</span> <span style="color: #8D38C9; font-weight: bold;">Case</span> Numero <br />
&nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">Case</span> <span style="color: #8D38C9; font-weight: bold;">Is</span> &gt; 1 <br />
&nbsp; &nbsp; &nbsp; x = InStr(1, Replace(Texte, Separateur, vbNullString, 1, Numero - 2, TypeComparaison), Separateur, TypeComparaison) <br />
&nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">If</span> x &gt; 0 <span style="color: #8D38C9; font-weight: bold;">Then</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;x = x + Len(Separateur) * (Numero - 1) <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;y = InStr(x, Texte, Separateur, TypeComparaison) <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">If</span> y &gt; 0 <span style="color: #8D38C9; font-weight: bold;">Then</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Tok = Mid$(Texte, x, y - x) <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">Else</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Tok = Mid$(Texte, x) <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #8D38C9; font-weight: bold;">If</span> <br />
&nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #8D38C9; font-weight: bold;">If</span> <br />
&nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">Case</span> 1 <br />
&nbsp; &nbsp; &nbsp; x = InStr(1, Texte, Separateur, TypeComparaison) <br />
&nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">If</span> x &gt; 0 <span style="color: #8D38C9; font-weight: bold;">Then</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Tok = Left$(Texte, x - 1) <br />
&nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">Else</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Tok = Texte <br />
&nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #8D38C9; font-weight: bold;">If</span> <br />
&nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">Case</span> -1 <br />
&nbsp; &nbsp; &nbsp; x = InStrRev(Texte, Separateur, -1, TypeComparaison) <br />
&nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">If</span> x &gt; 0 <span style="color: #8D38C9; font-weight: bold;">Then</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Tok = Mid$(Texte, x + Len(Separateur)) <br />
&nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">Else</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Tok = Texte <br />
&nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #8D38C9; font-weight: bold;">If</span> <br />
&nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #8D38C9; font-weight: bold;">Select</span> <br />
&nbsp; &nbsp;Token = Tok <br />
<span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #E56717; font-weight: bold;">Function</span></div></div>
<p><strong>Comment créer des lignes qui n&rsquo;existent pas ?</strong><br />
En effet, la colonne contient entre une et huit sous-chaînes selon la ligne. Comment générer une ligne par sous-chaîne à partir d&rsquo;une ligne source dans la requête SQL ?<br />
Pour ce faire, on s&rsquo;appuie sur le fait qu&rsquo;une relation 1-n entre deux tables génère n lignes dans une requête.</p>
<p>Connaissant le nombre de sous-chaîne, il nous faut donc créer une table de chiffres que l&rsquo;on mettra en relation avec ce nombre pour générer les lignes.</p>
<p>Cette fonction créée pour vous la table tChiffres et ajoute une ligne par chiffre (0 à 9)</p>
<div class="codecolorer-container vb blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="vb codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #E56717; font-weight: bold;">Public</span> <span style="color: #E56717; font-weight: bold;">Function</span> CreationTableChiffres() <br />
&nbsp; &nbsp;Const cInsert <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">String</span> = <span style="color: #800000;">&quot;INSERT INTO tChiffres (Chiffre) VALUES &quot;</span> <br />
&nbsp; &nbsp;<span style="color: #151B8D; font-weight: bold;">Dim</span> i <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Long</span> <br />
&nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">With</span> DoCmd <br />
&nbsp; &nbsp; &nbsp; .SetWarnings <span style="color: #00C2FF; font-weight: bold;">False</span> <br />
&nbsp; &nbsp; &nbsp; .RunSQL <span style="color: #800000;">&quot;CREATE TABLE tChiffres (Chiffre LONG CONSTRAINT PrimaryKey PRIMARY KEY);&quot;</span> <br />
&nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">For</span> i = 0 <span style="color: #8D38C9; font-weight: bold;">To</span> 9 <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.RunSQL cInsert &amp; <span style="color: #800000;">&quot;(&quot;</span> &amp; i &amp; <span style="color: #800000;">&quot;)&quot;</span> <br />
&nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">Next</span> i <br />
&nbsp; &nbsp; &nbsp; .SetWarnings <span style="color: #00C2FF; font-weight: bold;">True</span> <br />
&nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #8D38C9; font-weight: bold;">With</span> <br />
&nbsp;<br />
&nbsp; &nbsp;MsgBox <span style="color: #800000;">&quot;Création terminée de la table &quot;</span> <br />
<span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #E56717; font-weight: bold;">Function</span></div></div>
<p>Par exemple, la requête SQL suivante génére tous les nombres entre 0 et 999 :</p>
<div class="codecolorer-container sql blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">SELECT</span> C1<span style="color: #66cc66;">.</span>chiffre <span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">100</span> <span style="color: #66cc66;">+</span> C2<span style="color: #66cc66;">.</span>chiffre <span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">10</span> <span style="color: #66cc66;">+</span> C3<span style="color: #66cc66;">.</span>chiffre <span style="color: #993333; font-weight: bold;">AS</span> Nombre<br />
<span style="color: #993333; font-weight: bold;">FROM</span> tchiffres C1<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp;tchiffres C2<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp;tchiffres C3</div></div>
<p>Dans notre cas, nous avons au maximum 8 sous-chaînes, donc la première partie de notre requête s&rsquo;écrira :</p>
<div class="codecolorer-container sql blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">...</span><span style="color: #993333; font-weight: bold;">FROM</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> Texte<span style="color: #66cc66;">,</span> CompteOccurrences<span style="color: #66cc66;">&#40;</span>Texte<span style="color: #66cc66;">,</span><span style="color: #ff0000;">&quot;@&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">+</span><span style="color: #cc66cc;">1</span> <span style="color: #993333; font-weight: bold;">AS</span> Compte <span style="color: #993333; font-weight: bold;">FROM</span> tTransposition<span style="color: #66cc66;">&#41;</span> T <span style="color: #993333; font-weight: bold;">INNER</span> <span style="color: #993333; font-weight: bold;">JOIN</span> tChiffres C <span style="color: #993333; font-weight: bold;">ON</span> T<span style="color: #66cc66;">.</span>Compte<span style="color: #66cc66;">&gt;</span>C<span style="color: #66cc66;">.</span>Chiffre<span style="color: #66cc66;">...</span></div></div>
<p>Ceci permettra d&rsquo;associer un chiffre à chaque sous-chaîne extraite.</p>
<p><strong>Extraire les sous-chaînes</strong><br />
La fonction Token(&#8230;) a pour argument le numéro (<strong>1</strong> à x) de la sous-chaîne à extraire. Il faut donc ajouter 1 aux chiffres retournés par la relation précédente.<br />
La clause FROM de notre requête est maintenant formée :</p>
<div class="codecolorer-container sql blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">...</span><span style="color: #993333; font-weight: bold;">FROM</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> Token<span style="color: #66cc66;">&#40;</span>Texte<span style="color: #66cc66;">,</span><span style="color: #ff0000;">&quot;@&quot;</span><span style="color: #66cc66;">,</span>Chiffre<span style="color: #66cc66;">+</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> Chaine <span style="color: #993333; font-weight: bold;">FROM</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> Texte<span style="color: #66cc66;">,</span> CompteOccurrences<span style="color: #66cc66;">&#40;</span>Texte<span style="color: #66cc66;">,</span><span style="color: #ff0000;">&quot;@&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">+</span><span style="color: #cc66cc;">1</span> <span style="color: #993333; font-weight: bold;">AS</span> Compte <span style="color: #993333; font-weight: bold;">FROM</span> tTransposition<span style="color: #66cc66;">&#41;</span> &nbsp;<span style="color: #993333; font-weight: bold;">AS</span> T <span style="color: #993333; font-weight: bold;">INNER</span> <span style="color: #993333; font-weight: bold;">JOIN</span> tChiffres <span style="color: #993333; font-weight: bold;">AS</span> C <span style="color: #993333; font-weight: bold;">ON</span> T<span style="color: #66cc66;">.</span>Compte<span style="color: #66cc66;">&gt;</span>C<span style="color: #66cc66;">.</span>Chiffre<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> R<span style="color: #66cc66;">...</span></div></div>
<p><strong>Requête finale</strong><br />
Finalement, on réalise un regroupement sur les chaînes et un comptage décroissant :</p>
<div class="codecolorer-container sql blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">SELECT</span> R<span style="color: #66cc66;">.</span>chaine<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">COUNT</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">*</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> Compte<br />
<span style="color: #993333; font-weight: bold;">FROM</span> &nbsp;<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> Token<span style="color: #66cc66;">&#40;</span>texte<span style="color: #66cc66;">,</span> <span style="color: #ff0000;">&quot;@&quot;</span><span style="color: #66cc66;">,</span> chiffre <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> Chaine<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">FROM</span> <span style="color: #66cc66;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">SELECT</span> texte<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Compteoccurrences<span style="color: #66cc66;">&#40;</span>texte<span style="color: #66cc66;">,</span> <span style="color: #ff0000;">&quot;@&quot;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">1</span> <span style="color: #993333; font-weight: bold;">AS</span> Compte<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">FROM</span> ttransposition<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> T<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">INNER</span> <span style="color: #993333; font-weight: bold;">JOIN</span> tchiffres <span style="color: #993333; font-weight: bold;">AS</span> C<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">ON</span> T<span style="color: #66cc66;">.</span>compte <span style="color: #66cc66;">&gt;</span> C<span style="color: #66cc66;">.</span>chiffre<br />
&nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> R<br />
<span style="color: #993333; font-weight: bold;">GROUP</span> <span style="color: #993333; font-weight: bold;">BY</span> R<span style="color: #66cc66;">.</span>chaine<br />
<span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> <span style="color: #993333; font-weight: bold;">COUNT</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">*</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">DESC</span>;</div></div>
<p><strong>Pour ne pas plomber la vitesse d&rsquo;exécution </strong>de la requête, l&rsquo;enregistrer, quitter l&rsquo;application Access, la redémarrer puis lancer la requête SQL sans ouvrir auparavant l&rsquo;éditeur VBA.<br />
En moins de 2 secondes, le résultat de la requête devrait s&rsquo;afficher.<br />
A noter que le texte des lignes est généré aléatoirement d&rsquo;où certainement des écarts entre les comptages de la capture d&rsquo;écran et les vôtres.</p>
<p><strong>Requête SQL sans UDF</strong><br />
Si le coeur vous en dit, la requête suivante réalise le même travail sans utiliser une fonction personnelle.<br />
Mis à part les fonctions VBA déjà décrites dans ce blog, on utilise la fonction StrReverse() pour supprimer les séparateurs postérieurs à la sous-chaîne à extraire et déterminer ainsi sa longueur.</p>
<div class="codecolorer-container sql blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">SELECT</span> &nbsp;R<span style="color: #66cc66;">.</span>Chaine<span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">COUNT</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">*</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> Compte<br />
<span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; &nbsp;<span style="color: #66cc66;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">SELECT</span> IIf<span style="color: #66cc66;">&#40;</span>Num<span style="color: #66cc66;">&gt;</span><span style="color: #cc66cc;">1</span> <span style="color: #993333; font-weight: bold;">AND</span> NUM1<span style="color: #66cc66;">,</span> <span style="color: #993333; font-weight: bold;">LEFT</span>$<span style="color: #66cc66;">&#40;</span>T<span style="color: #66cc66;">.</span>Texte<span style="color: #66cc66;">,</span>InStr<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span>T<span style="color: #66cc66;">.</span>Texte<span style="color: #66cc66;">,</span><span style="color: #ff0000;">&quot;@&quot;</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">-</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>T<span style="color: #66cc66;">.</span>Texte<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MID$<span style="color: #66cc66;">&#40;</span>T<span style="color: #66cc66;">.</span>Texte<span style="color: #66cc66;">,</span>InStrRev<span style="color: #66cc66;">&#40;</span>T<span style="color: #66cc66;">.</span>Texte<span style="color: #66cc66;">,</span><span style="color: #ff0000;">&quot;@&quot;</span><span style="color: #66cc66;">,-</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">+</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> Chaine<span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Chiffre<span style="color: #66cc66;">+</span><span style="color: #cc66cc;">1</span> <span style="color: #993333; font-weight: bold;">AS</span> Num <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">FROM</span> <span style="color: #66cc66;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">SELECT</span> Texte<span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Len<span style="color: #66cc66;">&#40;</span>Texte<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">-</span> Len<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">REPLACE</span><span style="color: #66cc66;">&#40;</span>Texte<span style="color: #66cc66;">,</span><span style="color: #ff0000;">&quot;@&quot;</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">&quot;&quot;</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,-</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">+</span><span style="color: #cc66cc;">1</span> <span style="color: #993333; font-weight: bold;">AS</span> Compte <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">FROM</span> tTransposition<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#41;</span> &nbsp;<span style="color: #993333; font-weight: bold;">AS</span> T <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">INNER</span> <span style="color: #993333; font-weight: bold;">JOIN</span> tChiffres <span style="color: #993333; font-weight: bold;">AS</span> C <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">ON</span> T<span style="color: #66cc66;">.</span>Compte<span style="color: #66cc66;">&gt;</span>C<span style="color: #66cc66;">.</span>Chiffre<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#41;</span> &nbsp;<span style="color: #993333; font-weight: bold;">AS</span> R<br />
<span style="color: #993333; font-weight: bold;">GROUP</span> <span style="color: #993333; font-weight: bold;">BY</span> R<span style="color: #66cc66;">.</span>Chaine<br />
<span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> <span style="color: #993333; font-weight: bold;">COUNT</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">*</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">DESC</span>;</div></div>
<p><strong>Lien</strong><br />
Voir ce <a href="http://blog.developpez.com/philben/p11320/ms-access-vba/transposer-des-lignes-en-une-colonne" title="Transposer des lignes en une colonne">billet</a> pour réaliser une transposition inverse.</p>
<p>@+</p>
<p>Philippe</p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
