guocongwudi / mysql-pivot-procedure Goto Github PK
View Code? Open in Web Editor NEWAutomatically exported from code.google.com/p/mysql-pivot-procedure
Automatically exported from code.google.com/p/mysql-pivot-procedure
The replace statement made an error
Would you like to work on some more? I could need a total sum of columns to
sort the results.
-------------------------------------
CREATE DEFINER = 'root'@'%'
PROCEDURE npreports.pivotwizard_text(
IN P_Row_Field VARCHAR(255),
IN P_Column_Field VARCHAR(255),
IN P_Value VARCHAR(255),
IN P_From VARCHAR(4000),
IN P_Where VARCHAR(4000))
ThisSP:BEGIN
DECLARE done INT DEFAULT 0;
DECLARE M_Count_Columns int DEFAULT 0;
DECLARE M_Column_Field varchar(60);
DECLARE M_Columns VARCHAR(8000) DEFAULT '';
DECLARE M_sqltext VARCHAR(8000);
DECLARE M_stmt VARCHAR(8000);
DECLARE cur1 CURSOR FOR SELECT CAST(Column_Field AS CHAR) FROM Temp;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
DROP TABLE IF EXISTS Temp;
SET @M_sqltext = CONCAT('CREATE TEMPORARY TABLE Temp ',
' SELECT DISTINCT ',P_Column_Field,
' AS Column_Field',
' FROM ',P_From,
' WHERE ',P_Where,
' ORDER BY ', P_Column_Field);
#SELECT @M_sqltext;LEAVE ThisSP;
PREPARE M_stmt FROM @M_sqltext;
EXECUTE M_stmt;
SELECT COUNT(*) INTO M_Count_Columns
FROM Temp
WHERE Column_Field IS NOT NULL;
IF (M_Count_Columns > 0) THEN
OPEN cur1;
REPEAT
FETCH cur1 INTO M_Column_Field;
IF (NOT done) and (M_Column_Field IS NOT NULL) THEN
SET M_Columns = CONCAT(M_Columns,
' REPLACE( GROUP_CONCAT( CASE WHEN ',P_Column_Field,'=''',M_Column_Field,'''',
' THEN ',P_Value,
' ELSE NULL END, ''''), '','', '''') AS ''', M_Column_Field ,''',');
END IF;
UNTIL done END REPEAT;
SET M_Columns = Left(M_Columns,Length(M_Columns)-1);
#SELECT M_Columns;LEAVE ThisSP;
SET @M_sqltext = CONCAT('SELECT ',P_Row_Field,',',M_Columns,
' FROM ', P_From,
' WHERE ', P_Where,
' GROUP BY ', P_Row_Field,
' ORDER BY ', P_Row_Field);
#SELECT @M_sqltext;LEAVE ThisSP;
PREPARE M_stmt FROM @M_sqltext;
EXECUTE M_stmt;
END IF;
END
Original issue reported on code.google.com by [email protected]
on 10 Dec 2014 at 6:26
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.