0

I have an SQL query to display information about companies. One of the columns is called PieceType and a company could have many piece types and each type has a quantity. So I want to display each piece type as a column, then the quantity under it. So each type has its own column.

So it will look like this:

enter image description here

I've created a sample database here: http://www.sqlfiddle.com/#!9/13230/4

SET group_concat_max_len=4294967294;
SET @COLUMNS = NULL;

/* Build columns to pivot */

SELECT GROUP_CONCAT(
        DISTINCT CONCAT(
            'CASE WHEN jp.PieceType = "', 
            jp.PieceType ,
            '" THEN 1 ELSE NULL END AS ',
            jp.PieceType
        )
) INTO @COLUMNS
FROM job_pieces jp;

/* Build full query */
SET @SQL = CONCAT(
        'SELECT    
                c.Name,
                ',@COLUMNS'
        FROM customer c
        LEFT JOIN job_new jn ON c.JobID = jn.ID
        LEFT JOIN job_pieces jp ON c.JobID = jp.JobID
        WHERE c.Company_ID = 123
        GROUP BY c.ID'
);

/* Prepare and execute the query*/
PREPARE stmt FROM @SQL;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

But I'm getting this error when I run the query:

Incorrect parameters in the call to native function 'CONCAT'

1 Answer 1

0

You've got a few issues with your code that is causing some problems.

First, the code for setting the @SQL is missing a comma for the CONCAT function`:

SET @SQL = CONCAT(
        'SELECT    
                c.Name,
                ',@COLUMNS, '
                       -- ^ this is missing  
        FROM customer c
        LEFT JOIN job_new jn ON c.JobID = jn.ID
        LEFT JOIN job_pieces jp ON c.JobID = jp.JobID
        WHERE c.Company_ID = 123
        GROUP BY c.Name'
);

Once you add that comma you will get another error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CASE,CASE WHEN jp.PieceType = "CTN" THEN 1 ELSE NULL END AS CTN,CASE WHEN jp.Pie' at line 3

This error is because of your code to get the list of columns. Since you have a jp.PieceType value called CASE which is a reserved word your syntax fails. You need to escape the aliases for the column names:

SELECT GROUP_CONCAT(
        DISTINCT CONCAT(
            'CASE WHEN jp.PieceType = ''', 
            jp.PieceType ,
            ''' THEN 1 ELSE NULL END AS `',
                                     -- ^ add this
            jp.PieceType, '`'
                     -- ^ add this after the comma
        )
) INTO @COLUMNS
FROM job_pieces jp;

Here is a sql fiddle with the code now working. However, I must also note that you are missing an aggregate function around your CASE expression. Typically when you PIVOT data like this you use an aggregate function like MAX/MIN/SUM, etc. If you are looking to total the Qty for PieceType, then it seems that you'd want to use SUM, so your code would be:

SET group_concat_max_len=4294967294;
SET @COLUMNS = NULL;


/* Build columns to pivot */
SELECT GROUP_CONCAT(
        DISTINCT CONCAT(
            'SUM(CASE WHEN jp.PieceType = ''', 
            jp.PieceType ,
            ''' THEN jp.QTY ELSE 0 END) AS `',
            jp.PieceType, '`'
        )
) INTO @COLUMNS
FROM job_pieces jp;

/* Build full query */
SET @SQL = CONCAT(
        'SELECT    
                c.Name,
                ',@COLUMNS, '
        FROM customer c
        LEFT JOIN job_new jn ON c.JobID = jn.ID
        LEFT JOIN job_pieces jp ON c.JobID = jp.JobID
        WHERE c.Company_ID = 123
        GROUP BY c.Name'
);

/* Prepare and execute the query*/
PREPARE stmt FROM @SQL;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

See SQL Fiddle with Demo

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.