Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create query for below output using pivot

I want to pivot my table with multiple columns as per below requirement.

Employee_ID ProcessingMonth Amount      FinancialYear
3           April           41668.00    2017
3           June            41668.00    2017
3           March           41668.00    2017
3           May             41668.00    2017
4           April           10037.92    2017
4           June            10037.92    2017
4           March           10037.92    2017
4           May             10037.92    2017

i want result like below

Employee_ID year jan feb mar      apr      may      june     jul aug sep oct nov dec
3           2017 0   0   41668.00 41668.00 41668.00 41668.00 0   0   0   0   0   0
4           2017 0   0   10037.92 10037.92 10037.92 10037.92 0   0   0   0   0   0
like image 231
Rushang Avatar asked Aug 29 '26 16:08

Rushang


1 Answers

Try this:

SELECT *
FROM (
    SELECT 
          SUM(Amount) AS sum_amount
        , Employee_ID
        , FinancialYear
        , ProcessingMonth
    FROM your_table
    GROUP BY
          Employee_ID
        , FinancialYear
        , ProcessingMonth
) t
PIVOT (SUM(sum_amount)
    FOR ProcessingMonth IN ( 
        [January], [February], [March], [April], [May], [June], [July], [August], [September], [October], [November], [December])
    ) AS pvt;
like image 170
xcvd Avatar answered Aug 31 '26 05:08

xcvd



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!