Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9029717
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T07:18:47+00:00 2026-06-16T07:18:47+00:00

I know this question or a variance thereof has been asked multiple times, and

  • 0

I know this question or a variance thereof has been asked multiple times, and I have even tried implementing a solution, but I am struggling in completing it.

I have a very simple table with three data columns: Date1, Report_#, Name. I would like to pivot it around Date and the various names as the header tab, and have all the report numbers appear below.

So it would LOOK like this:

Report | Date   | Name                     Date   | Name1 | Name2 | Name3
-----------------------                    ------------------------------
1      | 4-5-12 | Name1                    4-5-12 | 1     | 2     | 3
2      | 4-5-12 | Name2      ----->        4-6-12 | 4     | 5     | 6
3      | 4-5-12 | Name3                    4-7-12 | 7     | 8     | 9
4      | 4-6-12 | Name1                    
5      | 4-6-12 | Name2                    
6      | 4-6-12 | Name3                    
7      | 4-7-12 | Name1
8      | 4-7-12 | Name2
9      | 4-7-12 | Name3

I was able to get an idea of what to do from http://www.artfulsoftware.com/infotree/queries.php#78 but I’m stuck.

I was able to isolate the column names manually and list the report # under each name, but I want to do dynamically figure out the distinct names, make them column names, and list the corresponding reports.

So I created a procedure that would find distinct names and output the correct code. Now I have a hard time plugging the results of the procedure onto the query. And the link above does not help whatsoever, (it seems to skip that step).

So here is the code for the manual way:

SELECT `DATE`, 
GROUP_CONCAT(CASE `Name` WHEN 'Name1' THEN `Report` END) AS 'Name1', 
GROUP_CONCAT(CASE `Name` WHEN 'Name2' THEN `Report` END) AS 'Name2' 
FROM `report_db` GROUP BY `DATE` ORDER BY `DATE`;

And here is the code that dynamic prints the GROUP_CONCAT(… for all the distinct names in the database:

DROP PROCEDURE IF EXISTS writecountpivot;
DELIMITER | 
CREATE PROCEDURE writecountpivot( db CHAR(64), tbl CHAR(64), col CHAR(64) ) 
BEGIN 
DECLARE datadelim CHAR(1) DEFAULT '"'; 
DECLARE singlequote CHAR(1) DEFAULT CHAR(39); 
DECLARE comma CHAR(1) DEFAULT ','; 
SET @sqlmode = (SELECT @@sql_mode); 
SET @@sql_mode=''; 
  SET @sql = CONCAT( 'SELECT DISTINCT CONCAT(', singlequote, 
                     ',group_concat(IF(', col, ' = ', datadelim, singlequote, comma, 
                     col, comma, singlequote, datadelim, comma, '`IR NO`,null)) AS `',  
                     singlequote, comma, col, comma, singlequote, '`', singlequote,  
                     ') AS countpivotarg FROM ', db, '.', tbl, 
                     ' WHERE ', col, ' IS NOT NULL' ); 
  -- UNCOMMENT TO SEE THE MIDLEVEL CODE: 
  -- SELECT @sql;  
  PREPARE stmt FROM @sql; 
  EXECUTE stmt; 
  DROP PREPARE stmt; 
  SET @@sql_mode=@sqlmode; 
END
| 
DELIMITER ; 
CALL writecountpivot('database','`report_db`','name');

and the result of the code above would be this

,group_concat(IF(name = "Name1",`IR NO`,null)) AS `Name1`
,group_concat(IF(name = "Name2",`IR NO`,null)) AS `Name2`
,group_concat(IF(name = "Name3",`IR NO`,null)) AS `Name3`

** So how do I take this text and plug it to my SQL? How do I put the procedure and the query together?**

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T07:18:48+00:00Added an answer on June 16, 2026 at 7:18 am

    It’s difficult to tell exactly what it is you’re trying to do, but if you’re attempting to create a procedure that can perform arbitrary pivots then you’ll need to provide it with more arguments (such as the column on which to pivot and the column in which values can be found).

    Furthermore, you will need to create a prepared statement from within a prepared statement. The outermost statement will use GROUP_CONCAT() to construct the GROUP_CONCAT() expressions that are to be executed, based on the unique values in the specified column:

    CREATE FUNCTION SQL_ESC(_identifier VARCHAR(64))
    RETURNS VARCHAR(130) DETERMINISTIC
    RETURN CONCAT('`',REPLACE(_identifier,'`','``'),'`')//
    
    CREATE PROCEDURE writecountpivot(
      IN _db_nm VARCHAR(64),
      IN _tb_nm VARCHAR(64),
      IN _cl_gp VARCHAR(64),
      IN _cl_pv VARCHAR(64),
      IN _cl_vl VARCHAR(64)
    ) BEGIN
      SET @sql := CONCAT(
        "SELECT CONCAT('
                  SELECT   ",SQL_ESC(_cl_gp),",',
                  GROUP_CONCAT(DISTINCT CONCAT(
                    'GROUP_CONCAT(IF(",
                       SQL_ESC(_cl_pv),"=',QUOTE(",SQL_ESC(_cl_pv),"),'
                     , ",SQL_ESC(_cl_vl),"
                     , NULL
                     )) AS ',SQL_ESC(",SQL_ESC(_cl_pv),")
                  )), '
                  FROM     ",SQL_ESC(_db_nm),".",SQL_ESC(_tb_nm),"
                  GROUP BY ",SQL_ESC(_cl_gp),"
                ')
         INTO   @sql
         FROM   ",SQL_ESC(_db_nm),".",SQL_ESC(_tb_nm)
      );
    
      PREPARE stmt FROM @sql; 
      EXECUTE stmt; 
      DEALLOCATE PREPARE stmt;
    
      PREPARE stmt FROM @sql; 
      EXECUTE stmt; 
      DEALLOCATE PREPARE stmt;
    END//
    

    See it on sqlfiddle.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this question has been asked previously and I have tried all the
I know this question has been asked a couple of times before. I m
I know this question had been asked a hundred times, but I have difficulties
I know this question has been asked and seemingly answered a gazillion times over
I know this question has been asked many times, but I couldn't manage to
I know this question has been asked before, but I tried eveything and can't
I know this question has been asked many times. But from what I read,
I know this question has been asked a million times in various ways by
I know this question has been asked many times, but my problem is a
I know this question has been asked many times and I've researched it myself

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.