Click here to Skip to main content
15,895,011 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a gridview and it is linked with SQL table, i want to display headers horizontally as well as vertically but i am stuck and have no idea how to do it.
I want the result to be displayed as follows,

--StepName---Product1
|--Step1-----------2
|--Step2-----------2
|--Step3-----------2
|--Step4-----------2
|--Step5-----------2

The numbers in above table are count of each step,
i am using the following SQL code,

select 
  sum(case when currentstepname = 'Step1' then 1 else 0 end) Step1,
  sum(case when currentstepname = 'Step2' then 1 else 0 end) Step2,
  sum(case when currentstepname = 'Step3 then 1 else 0 end) Step3,
  sum(case when currentstepname = 'Step4' then 1 else 0 end) Step4,
  sum(case when currentstepname = 'Step5' then 1 else 0 end) Step5

from Table1


And i am getting the result currently as follows,

--Step1---Step2---Step3---Step4---Step5
|--2---------2--------2--------2-------2----|

What I have tried:

Searched the web but not getting the required results.
Posted
Updated 19-Feb-17 20:25pm

1 solution

If i understand you well, you have to use Pivot[^].
SQL
SELECT StepName, [Product1], [Product2], [Product3]
FROM (
    SELECT StepName, ProductName, COUNT(*) AS StepsCount
    FROM Table1
    GROUP BY StepName, ProductName
) AS DT
PIVOT (SUM(StepsCount) FOR (ProductName) IN ([Product1], [Product2], [Product3])) AS PT


Try!
 
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