I am trying to get a SQL Server pivot table table to work, which allows me to count and then summarize multiple columns (total 6). The purpose of the pivot table is to combine the results of an online survey for any number of production sites. There are 6 questions that can have 3 result values โโ- Target, Action and Fail. What I'm trying to do is count the number of goals, actions, and failures for each question, and then summarize them for each. So, for example, on production site A there can be 2 Target objects, 2 Action and 2 Fail.
I understand that a SQL Server pivot table can provide this information, which can then be displayed in ASP.Net ReportViewer. Below is my code, but it does not work and may help with some help:
SELECT PRODUCTION_Site, [Target], [Action], [Fail] FROM (SELECT Production_Site, SUM(Coding), SUM(Measurable), SUM(Appearance), SUM(Aroma), SUM(Flavour), SUM(Texture) FROM t_Pqe_Grocery GROUP BY Production_Site) AS T PIVOT ( COUNT(Coding) FOR Grocery_Packaging_And_Coding IN ([Target],[Action],[Fail]) COUNT(Measurable) FOR Grocery_Measurable IN ([Target],[Action],[Fail]) COUNT(Appearance) FOR Grocery_Appearance IN ([Target],[Action],[Fail]) COUNT(Aroma) FOR Grocery_Aroma IN ([Target],[Action],[Fail]) COUNT(Flavour) FOR Grocery_Flavour IN ([Target],[Action],[Fail]) COUNT(Texture) FOR Grocery_Texture IN ([Target],[Action],[Fail])) AS P
Is there a way around this, or is this pivot table not a solution?
Table
Production_Site, Grocery_Packaging_And_Coding, Grocery_Measurable, Grocery_Appearance, Grocery_Aroma, Grocery_Flavour, Grocery_Texture
The data in the table:
Site A, Target, Action, Fail, Target, Target, Target Site B, Target, Action, Fail, Target, Target, Target Site C, Target, Target, Target, Target, Target, Target Site A, Target, Target, Target, Target, Target, Target
As a result, I seek
Production_Site | Target | Action | Fail Site A 10 1 1 Site B 4 1 1 Site C 6 0 0