1

I have a table which contains the following rows :

╔══════════╦═══════════╦════════╗
║ PK_VALUE ║ RATING_ID ║ RATING ║
╠══════════╬═══════════╬════════╣
║       11 ║         1 ║     90 ║
║       11 ║         2 ║     80 ║
║       11 ║         3 ║     90 ║
║       12 ║         1 ║     90 ║
║       12 ║         2 ║     80 ║
║       12 ║         3 ║     90 ║
╚══════════╩═══════════╩════════╝

I want the above table as :

╔══════════╦════════════╦════════════╦════════════╗
║ PK_VALUE ║ RATING_ID1 ║ RATING_ID2 ║ RATING_ID3 ║
╠══════════╬════════════╬════════════╬════════════╣
║       11 ║         90 ║         80 ║         90 ║
║       12 ║         90 ║         80 ║         90 ║
╚══════════╩════════════╩════════════╩════════════╝

I tried some of the suggestions I found within a Google search, but it shows we have to insert into a table. I am not sure about this. Any suggestions welcome...

I have uploaded the screenshots related to my doubt..image of the result query before what i use to get..

In this image we can see two review ID's for the same pk_value.. But I am receiving only one review ID after including your query.. result after including your query..

In the above How can I get result of both the review ID's???

2

1 Answer 1

3

use CASE and MAX

SELECT  pk_value,
        MAX(CASE WHEN rating_ID = 1 THEN rating ELSE NULL END) AS r1,
        MAX(CASE WHEN rating_ID = 2 THEN rating ELSE NULL END) AS r2,
        MAX(CASE WHEN rating_ID = 3 THEN rating ELSE NULL END) AS r3
FROM    tableName
GROUP   BY pk_value

Otherwise, if you have unknown number of Rating_ID and you don't want to constantly updated the query, a Dynamic SQL is recommended,

SET @sql = NULL;
SELECT
  GROUP_CONCAT(DISTINCT
    CONCAT(
      'MAX(CASE WHEN rating_ID = ',
      rating_id,
      ' then rating ELSE NULL end) AS `rating_id', rating_id, '`')
  ) INTO @sql
FROM TableName;

SET @sql = CONCAT('SELECT  pk_value, ', @sql, ' 
                   FROM    tableName
                   GROUP   BY pk_value');

PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

both the query above have the same result,

╔══════════╦════════════╦════════════╦════════════╗
║ PK_VALUE ║ RATING_ID1 ║ RATING_ID2 ║ RATING_ID3 ║
╠══════════╬════════════╬════════════╬════════════╣
║       11 ║         90 ║         80 ║         90 ║
║       12 ║         90 ║         80 ║         90 ║
╚══════════╩════════════╩════════════╩════════════╝
Sign up to request clarification or add additional context in comments.

7 Comments

Thank u sir... upto now I havent known about this SQL Fiddle.. this is really beautiful for developers.. Thank u once again..
You have edited my question and added table structure to the numbers.. I would like to know how can we do that??
sir a problem.. It is returning only one row of a table. if we have two or more pk values it is not displaing rest of them..
@PavanKumar give mi some sample data :D so we can fix it.
the original query was grouping with pk_value. try GROUP BY pk_value,review_id
|

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.