Précédent   Forum des professionnels en informatique > Bases de données > Langage SQL
Langage SQL Forum d'entraide sur le langage SQL et sur les questions liées à la conception de schéma (DDL). Cours SQL
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse Proposer ce sujet en actualité
 
Outils de la discussion
Publicité
'
Vieux 11/04/2011, 17h08   #1
Membre régulier
 
Olivier Albertini
Inscription : avril 2006
Messages : 194
Détails du profil
Informations personnelles :
Nom : Olivier Albertini

Informations forums :
Inscription : avril 2006
Messages : 194
Points : 77
Points : 77
Par défaut [SQL]SQL Server 2008 - concaténation de colonne

Bonjour,

j'ai un petit soucis de concaténation, j'utilise SQL Server 2008
voici ma vue :
Code :
1
2
3
4
5
6
 
SELECT dbo.PRG_HC.HC_ID, dbo.T_ARBO_PRG.PRG_NAME, dbo.PRG_HC.HC_ANUM, LTRIM(dbo.T_ARBO_PRG.PRG_ROOT + dbo.PRG_HC.HC_ANUM) 
                      AS [HC CODE], dbo.T_ARBO_PRG.PRG_LEVEL
FROM dbo.PRG_HC 
INNER JOIN dbo.T_ARBO_PRG ON dbo.PRG_HC.PRG_ID = dbo.T_ARBO_PRG.PRG_ID 
INNER JOIN dbo.HC ON dbo.HC.HC_ID = dbo.PRG_HC.HC_ID
HC_ID est retourné plusieurs fois et la colonne alias HC CODE possède les différentes appellations de mon HC_ID.
Je voudrais concaténer les HC_CODE pour avoir des unique HC_ID et avoir la colonne HC CODE qui possède toutes les appellations..

c'est pour un affichage utilisateur. car en application la concaténation prend trop de temps... il y a 532 HC et et une 10zaine d'appellations par HC

J'espère avoir été assez clair
lerieure est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 11/04/2011, 18h02   #2
Rédacteur/Modérateur

 
Avatar de SQLpro
 
Homme Frédéric BROUARD
Expert SGBDR & SQL
Inscription : mai 2002
Messages : 10 953
Détails du profil
Informations personnelles :
Nom : Homme Frédéric BROUARD
Localisation : France

Informations professionnelles :
Activité : Expert SGBDR & SQL
Secteur : Conseil

Informations forums :
Inscription : mai 2002
Messages : 10 953
Points : 17 773
Points : 17 773
Utilisez une requête récursive comme celle que j'ai donné dans mon cours ici :
http://sqlpro.developpez.com/cours/s...ursives/#LIV-D

A +
__________________
Frédéric Brouard - SQLpro - ARCHITECTE DE DONNÉES - expert SGBDR et langage SQL
Site sur les SGBD relationnels et le langage SQL: http://sqlpro.developpez.com/
Expert Microsoft SQL Server - M.V.P. (Most valuable Professional) MS Corp.
Blog SQL, SQL Server, modélisation données : http://blog.developpez.com/sqlpro
http://www.sqlspot.com : modélisation, conseils, audit, optimisation, formation
* * * * * Enseignant CNAM PACA - ISEN Toulon - CESI Aix en Provence * * * * *
SQLpro est déconnecté   Envoyer un message privé Réponse avec citation 10
Vieux 11/04/2011, 19h21   #3
Membre régulier
 
Olivier Albertini
Inscription : avril 2006
Messages : 194
Détails du profil
Informations personnelles :
Nom : Olivier Albertini

Informations forums :
Inscription : avril 2006
Messages : 194
Points : 77
Points : 77
J'essaye mais j'y arrive pas

pouvez vous me dire comment ça fonctionne..
Dans mon cas ma table n'a pas de position
Du coup ça boucle à l'infini...

Code :
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
 
WITH
phrases (phrase, id)
AS
(
SELECT CAST(T_ARBO_PRG.PRG_ROOT + PRG_HC.HC_ANUM AS VARCHAR(max))
      + CASE
           WHEN SUBSTRING(T_ARBO_PRG.PRG_ROOT + PRG_HC.HC_ANUM, LEN(T_ARBO_PRG.PRG_ROOT + PRG_HC.HC_ANUM), 1) = '''' THEN ''
           ELSE ' '
        END, HC.HC_ID
FROM   PRG_HC INNER JOIN
                      T_ARBO_PRG ON PRG_HC.PRG_ID = T_ARBO_PRG.PRG_ID INNER JOIN
                      HC ON HC.HC_ID = PRG_HC.HC_ID
UNION ALL
SELECT phrase + CAST(T_ARBO_PRG.PRG_ROOT + PH.HC_ANUM AS VARCHAR(max))
             + CASE
                  WHEN SUBSTRING(T_ARBO_PRG.PRG_ROOT + PH.HC_ANUM, LEN(T_ARBO_PRG.PRG_ROOT + PH.HC_ANUM), 1) = '''' THEN ''
                  ELSE ' '
               END AS PHRASE,
      suiv.HC_ID
FROM   HC AS suiv
      INNER JOIN phrases
            ON suiv.HC_ID = phrases.id
      INNER JOIN PRG_HC PH ON suiv.HC_ID=PH.HC_ID 
      INNER JOIN T_ARBO_PRG ON PH.PRG_ID = T_ARBO_PRG.PRG_ID 
),
maxphrase
AS
(
SELECT id, MAX(id) AS maxposition
FROM   phrases
GROUP  BY id
)
SELECT id, RTRIM(phrase) + '.' AS PHRASE
FROM   phrases
ORDER  BY id
lerieure est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 11/04/2011, 19h51   #4
Rédacteur/Modérateur

 
Avatar de SQLpro
 
Homme Frédéric BROUARD
Expert SGBDR & SQL
Inscription : mai 2002
Messages : 10 953
Détails du profil
Informations personnelles :
Nom : Homme Frédéric BROUARD
Localisation : France

Informations professionnelles :
Activité : Expert SGBDR & SQL
Secteur : Conseil

Informations forums :
Inscription : mai 2002
Messages : 10 953
Points : 17 773
Points : 17 773
Si vous n'avez pas de clef dans votre table... C'est pas une table c'est un fichier Cobol.

Commencez par le commencement en définissant la clef de votre table.

Accessoirement respecter la charte de postage. http://www.developpez.net/forums/a69...gage-sql-lire/
On ne peut pas deviner la structure de votre table !

A +
__________________
Frédéric Brouard - SQLpro - ARCHITECTE DE DONNÉES - expert SGBDR et langage SQL
Site sur les SGBD relationnels et le langage SQL: http://sqlpro.developpez.com/
Expert Microsoft SQL Server - M.V.P. (Most valuable Professional) MS Corp.
Blog SQL, SQL Server, modélisation données : http://blog.developpez.com/sqlpro
http://www.sqlspot.com : modélisation, conseils, audit, optimisation, formation
* * * * * Enseignant CNAM PACA - ISEN Toulon - CESI Aix en Provence * * * * *
SQLpro est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 11/04/2011, 20h14   #5
Membre régulier
 
Olivier Albertini
Inscription : avril 2006
Messages : 194
Détails du profil
Informations personnelles :
Nom : Olivier Albertini

Informations forums :
Inscription : avril 2006
Messages : 194
Points : 77
Points : 77
Oui excusez moi voici les tables qui ont des clées

T_ARBO_PRG: PRG_ID,PRG_NAME,PRG_BD,PRG_BG,PRG_LEVEL,PRG_ROOT,PRG_LEN

HC : HC_ID, ...

PRG_HC : PRG_ID, HC_ID, HC_ANUM

Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
CREATE TABLE [dbo].[T_ARBO_PRG](
	[PRG_ID] [int] IDENTITY(1,1) NOT NULL,
	[PRG_NAME] [varchar](32) NOT NULL,
	[PRG_BD] [int] NOT NULL,
	[PRG_BG] [int] NOT NULL,
	[PRG_LEVEL] [int] NOT NULL,
	[PRG_ROOT] [varchar](32) NULL,
	[PRG_LEN] [int] NOT NULL,
 CONSTRAINT [PK_PRG_ID] PRIMARY KEY CLUSTERED 
(
	[PRG_ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
Code :
1
2
3
4
5
6
7
8
9
10
11
CREATE TABLE [dbo].[PRG_HC](
	[PRG_ID] [int] NOT NULL,
	[HC_ID] [int] NOT NULL,
	[HC_ANUM] [varchar](20) NULL,
 CONSTRAINT [PK_PRG_HC] PRIMARY KEY CLUSTERED 
(
	[PRG_ID] ASC,
	[HC_ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO

Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
CREATE TABLE [dbo].[HC](
	[HC_ID] [int] IDENTITY(1,1) NOT NULL,
	[date_saisie] [datetime] NOT NULL,
	[PRG_ID] [int] NULL,
	[launching] [int] NULL,
	[version] [nchar](3) NULL,
	[engine] [nchar](10) NULL,
	[config] [varchar](10) NULL,
	[newRule] [nchar](7) NULL,
 CONSTRAINT [PK_HC_ID] PRIMARY KEY CLUSTERED 
(
	[HC_ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
lerieure est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 11/04/2011, 21h13   #6
Rédacteur/Modérateur

 
Avatar de SQLpro
 
Homme Frédéric BROUARD
Expert SGBDR & SQL
Inscription : mai 2002
Messages : 10 953
Détails du profil
Informations personnelles :
Nom : Homme Frédéric BROUARD
Localisation : France

Informations professionnelles :
Activité : Expert SGBDR & SQL
Secteur : Conseil

Informations forums :
Inscription : mai 2002
Messages : 10 953
Points : 17 773
Points : 17 773
Un petit jeu d'essais avec le résultat attendu ferais pas de mal....

A +
__________________
Frédéric Brouard - SQLpro - ARCHITECTE DE DONNÉES - expert SGBDR et langage SQL
Site sur les SGBD relationnels et le langage SQL: http://sqlpro.developpez.com/
Expert Microsoft SQL Server - M.V.P. (Most valuable Professional) MS Corp.
Blog SQL, SQL Server, modélisation données : http://blog.developpez.com/sqlpro
http://www.sqlspot.com : modélisation, conseils, audit, optimisation, formation
* * * * * Enseignant CNAM PACA - ISEN Toulon - CESI Aix en Provence * * * * *
SQLpro est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 11/04/2011, 22h16   #7
Membre régulier
 
Olivier Albertini
Inscription : avril 2006
Messages : 194
Détails du profil
Informations personnelles :
Nom : Olivier Albertini

Informations forums :
Inscription : avril 2006
Messages : 194
Points : 77
Points : 77
Citation:
Un petit jeu d'essais avec le résultat attendu ferais pas de mal....
En faisant cette requete

Code :
1
2
3
4
5
6
 
 
SELECT     dbo.PRG_HC.HC_ID, LTRIM(dbo.T_ARBO_PRG.PRG_ROOT + dbo.PRG_HC.HC_ANUM) AS [HC CODE], 
                    FROM         dbo.PRG_HC INNER JOIN
                      dbo.T_ARBO_PRG ON dbo.PRG_HC.PRG_ID = dbo.T_ARBO_PRG.PRG_ID INNER JOIN
                      dbo.HC ON dbo.HC.HC_ID = dbo.PRG_HC.HC_ID
Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 
 
HC_ID| HC CODE| 
2	JJ001	
2	AAA001	
2	T01	
2	A 0392	
2	1177	
2	440229	
3	440261	
3	1206	
3	A 0393	
3	T02	
3	JJ002	
3	AAA002

et je voudrais pour HC_ID :
Code :
1
2
3
4
5
6
7
8
 
 
	JJ001	
	AAA001	
2	T01	
	A 0392	
	1177	
	440229
en une ligne la concaténation de ma colonne HC_CODE

j'espère que vous m'avez compris
lerieure est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 12/04/2011, 10h42   #8
Membre régulier
 
Olivier Albertini
Inscription : avril 2006
Messages : 194
Détails du profil
Informations personnelles :
Nom : Olivier Albertini

Informations forums :
Inscription : avril 2006
Messages : 194
Points : 77
Points : 77
MDR! J'ai trouvé mais je comprend pas xD



Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
 
WITH
phrases (phrase, id, position)
AS
(
SELECT CAST(dbo.T_ARBO_PRG.PRG_ROOT + dbo.PRG_HC.HC_ANUM AS VARCHAR(max))
      + CASE
           WHEN SUBSTRING(dbo.T_ARBO_PRG.PRG_ROOT + dbo.PRG_HC.HC_ANUM, LEN(dbo.T_ARBO_PRG.PRG_ROOT + dbo.PRG_HC.HC_ANUM), 1) = '''' THEN ''
           ELSE ' '
        END, PRG_HC.HC_ID, PRG_LEVEL
FROM   dbo.PRG_HC 
INNER JOIN dbo.T_ARBO_PRG ON dbo.PRG_HC.PRG_ID = dbo.T_ARBO_PRG.PRG_ID 
INNER JOIN dbo.HC ON dbo.HC.HC_ID = dbo.PRG_HC.HC_ID
WHERE  PRG_LEVEL = 0
UNION ALL
SELECT phrase + CAST(dbo.T_ARBO_PRG.PRG_ROOT + suiv.HC_ANUM AS VARCHAR(max))
             + CASE
                  WHEN SUBSTRING(dbo.T_ARBO_PRG.PRG_ROOT + suiv.HC_ANUM, LEN(dbo.T_ARBO_PRG.PRG_ROOT + suiv.HC_ANUM), 1) = '''' THEN ''
                  ELSE ' '
               END AS PHRASE,
      suiv.HC_ID, PRG_LEVEL
FROM   dbo.PRG_HC AS suiv
INNER JOIN dbo.T_ARBO_PRG ON suiv.PRG_ID = dbo.T_ARBO_PRG.PRG_ID 
INNER JOIN dbo.HC ON dbo.HC.HC_ID = suiv.HC_ID 
INNER JOIN phrases
            ON suiv.HC_ID = phrases.id
               AND T_ARBO_PRG.PRG_LEVEL = phrases.position + 1
),
maxphrase
AS
(
SELECT id, MAX(position) AS maxposition
FROM   phrases
GROUP  BY id
)
SELECT P.id, RTRIM(phrase) + '.' AS PHRASE
FROM   phrases AS P
      INNER JOIN maxphrase AS M
            ON P.id = M.id
               AND P.position = M.maxposition
ORDER  BY id
pouvez vous m'expliquez ce que font les select svp ?

d'autre part j'ai mes codes qui sont concatener mais ils sont collés... comment puis je faire un retour à la ligne pour chaque code ?


Sinon c'est super puissant !!!
lerieure est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 12/04/2011, 18h39   #9
Rédacteur/Modérateur

 
Avatar de SQLpro
 
Homme Frédéric BROUARD
Expert SGBDR & SQL
Inscription : mai 2002
Messages : 10 953
Détails du profil
Informations personnelles :
Nom : Homme Frédéric BROUARD
Localisation : France

Informations professionnelles :
Activité : Expert SGBDR & SQL
Secteur : Conseil

Informations forums :
Inscription : mai 2002
Messages : 10 953
Points : 17 773
Points : 17 773
Citation:
Envoyé par lerieure Voir le message
d'autre part j'ai mes codes qui sont concatener mais ils sont collés... comment puis je faire un retour à la ligne pour chaque code ?
Ajoutez n'importe queoi entre les deux colonnes concaténées. pour un retour chariot :
CHAR(13) + CHAR(10)

A +
__________________
Frédéric Brouard - SQLpro - ARCHITECTE DE DONNÉES - expert SGBDR et langage SQL
Site sur les SGBD relationnels et le langage SQL: http://sqlpro.developpez.com/
Expert Microsoft SQL Server - M.V.P. (Most valuable Professional) MS Corp.
Blog SQL, SQL Server, modélisation données : http://blog.developpez.com/sqlpro
http://www.sqlspot.com : modélisation, conseils, audit, optimisation, formation
* * * * * Enseignant CNAM PACA - ISEN Toulon - CESI Aix en Provence * * * * *
SQLpro est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse Proposer ce sujet en actualité Cette discussion est résolue.
Outils de la discussion



Fuseau horaire GMT +2. Il est actuellement 00h55.


 
 
 
 
Partenaires

Hébergement Web