2

I have this table

CREATE TABLE [dbo].[MONITOR.Dati_Attivita]
(
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [IDEsecuzione] [int] NOT NULL,
    [IDIndice] [int] NOT NULL,
    [Valore] [nvarchar](100) NOT NULL,
    [IDRipetizione] [int] NOT NULL,

    CONSTRAINT [PK_Monitor_Dati_Attivita] 
      PRIMARY KEY CLUSTERED ([ID] ASC)
) ON [PRIMARY]

So I would like to SELECT the result set,

I try this:

SELECT IDEsecuzione, Valore, IDRipetizioe

The result is

1 -  100  - 1
1 - 'abc' - 1
1 - 'VVV' - 1
2 - 'aaa' - 1
2 - 'bbb' - 1
2 - 'ccc' - 1

But I would like to display result set like this:

1 - 100 - abc - VVV - 1
2 - aaa- bbb - ccc - 1

Can you help me?

Regards

7

1 Answer 1

1

To concatenate a column over rows, use the FOR XML PATH method.

    SELECT DISTINCT
        IDEsecuzione,
        Valore = SUBSTRING((SELECT ' - ' + t1.Valore 
                            FROM MONITOR.Dati_Attivita t1 
                            WHERE t1.IDEsecuzione = t2.IDEsecuzione 
                            FOR XML PATH('')), 4, 9999),
        IDRipetizione
    FROM MONITOR.Dati_Attivita t2
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.