1

各項目の値を各グループの合計で割って、パーセンテージを計算したいと思います。

これが私のコーディングです:

 SET @VARBRANCH='
            MAX(IF(Branch =''A'',`data`, NULL)) AS ''A'',
            MAX(IF(Branch =''B'',`data`, NULL)) AS ''B''';

            DROP TEMPORARY TABLE IF EXISTS TBL_ACBR;
            SET @VARBRANCH=CONCAT('CREATE TEMPORARY TABLE TBL_ACBR SELECT Prtype,SUM(data) AS Total,  '
            ,@VARBRANCH,
            ' FROM(
            SELECT Prtype, Branch, `data` FROM TBLGETDATAALL_1 GROUP BY  Branch,Prtype) t
            GROUP BY Prtype;');
            PREPARE stmacbr FROM @VARBRANCH;
            EXECUTE stmacbr;
            DEALLOCATE PREPARE stmacbr; 

            SELECT * FROM TBL_ACBR;


Script above it show as the following:

Prtype Total  A  B
JAM     5     3  2  
Jim     6     3  3

The result that i need as the following by percentage:

Prtype Total     A      B
Total   11       6      5
JAM     5/11     3/6    2/5  
Jim     6/11     3/6    3/5
4

1 に答える 1

1

これを試して:

 SET @VARBRANCH='
        MAX(IF(Branch =''A'',`data`, NULL)) AS ''A'',
        MAX(IF(Branch =''B'',`data`, NULL)) AS ''B''';

        DROP TEMPORARY TABLE IF EXISTS TBL_ACBR;
        SET @VARBRANCH=CONCAT('CREATE TEMPORARY TABLE TBL_ACBR SELECT Prtype,SUM(data) AS Total,  '
        ,@VARBRANCH,
        ' FROM(
        SELECT Prtype, Branch, `data` FROM TBLGETDATAALL_1 GROUP BY  Branch,Prtype) t
        GROUP BY Prtype;');
        PREPARE stmacbr FROM @VARBRANCH;
        EXECUTE stmacbr;
        DEALLOCATE PREPARE stmacbr; 

        -- New queries
        SELECT SUM(A), SUM(B) INTO @SUM_A, @SUM_B FROM TBL_ACBR; -- edit
        UPDATE TBL_ACBR SET A = A / @SUM_A, B = B/@SUM_B;
        INSERT INTO TBL_ACBR (Prtype, Totel, A, B) 
          VALUES ("Total", @SUM_A+@SUM_B, SUM_A, SUM_B);

        SELECT * FROM TBL_ACBR;
于 2013-06-02T14:36:26.900 に答える