Click here to Skip to main content
15,899,679 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My table output is given below

Gender jan feb mar apr may jun jul

Female 11 13 13 13 13 13 13
Male 35 43 43 43 43 43 43

i want my output as

Gender female male

jan 11 35
feb 13 43
mar 13 43
....

How to do ??? pls help me
Posted

You need to order by and group by year and month

like this

VB
ORDER BY
  year(Date),
  month(Date)
GROUP BY
  year(Date),
  month(Date)
 
Share this answer
 
SQL
SELECT
    *
FROM
    (
        SELECT *
        FROM
           (
            SELECT 'Female' AS Gender, 11 AS Jan, 13 AS Feb, 13 AS Mar
            UNION SELECT 'Male' AS Gender, 35 AS Jan, 43 AS Feb, 43 AS Mar
           ) p
        UNPIVOT (
            C FOR [Month] IN (Jan, Feb, Mar)
        ) AS unpvt
    ) p
    PIVOT (
        SUM(C) FOR Gender IN (Female, Male)
    ) pvt


See also Using PIVOT and UNPIVOT[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900