PIVOT after execution the message is "Incorrect syntax next to the keyword 'FOR'."

Asked

Viewed 109 times

0

I need my data to return customer information by courses in a single row, that is, the various different courses done instead of coming in individual rows need to appear in new columns in the respective row to the customer.

I used the command PIVOT Table to sort as needed, but when executing the message appears:

Incorrect syntax next to the keyword 'FOR'.

I’ll synthesize my code:

SELECT [CLIENTE],
      [CURSO 1],
      [CURSO 2],
      [CURSO 3],
      [CURSO 4],
      [CURSO 5],
      [CURSO 6],
      [CURSO 7]
 FROM (
    SELECT DISTINCT
      [CLIENTE],
      [CURSO],
      [CH CURSO CT]
   .
   .
   ) AS CLIENTE

    PIVOT ([CH CURSO CT] FOR [CURSO] IN
         ([CURSO 1],
         [CURSO 2],
         [CURSO 3],
         [CURSO 4],
         [CURSO 5],
         [CURSO 6],
         [CURSO 7]))
    AS PVT

Who can help me I will be very grateful.

1 answer

0

Roberto, before the FOR must inform an aggregation function. Something like max(), avg(), sum() etc.

inserir a descrição da imagem aqui

In case of your code maybe you can use

       PIVOT (max ([CH CURSO CT]) FOR [CURSO] IN 

Details in the article "Alas & Pivots”.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.