I am trying to output the results of a query. I'd like to be able to flip how the results come out. Currently they come out like so
Question1
Location1 count of Question1 likes/dislikes
Location2 count of Question1 likes/dislikes
Location3 count of Question1 likes/dislikes
I'd like the results to come out like so:
Location1
count of Question1 likes/dislikes
count of Question2 likes/dislikes
count of Question3 likes/dislikes
count of Question4 likes/dislikes
Location is a variable in a field.
Each question (Question1, Question2, Question3, etc) is a field in the datase.
This is what I've come up with to output the data so far. Unfortunately, it'll take a lot of reformatting on an excel sheet to get it to look the way I want it to. Just trying to figure out if there's a better way of writing the query.
/** Show the count and percentage of each response **/
select
/**Show count and % of those that answered with a 1 for this question**/
Location,CleanlinessBldg_1 As 'Q3_1',
cast(sum(CleanlinessBldg_1)/(SQ3_tot*1.0)*100 as decimal(9,0))as PQ3_1,
/**Show count and % of those that answered with a 2 for this question**/
CleanlinessBldg_2 As 'Q3_2',
cast(sum(CleanlinessBldg_2)/(SQ3_tot*1.0)*100 as decimal(9,0)) as PQ3_2,
/**Show count and % of those that answered with a 3 for this question**/
CleanlinessBldg_3 As 'Q3_3',
cast(sum(CleanlinessBldg_3)/(SQ3_tot*1.0)*100 as decimal(9,0)) as PQ3_3,
/**Show count and % of those that answered with a 4 for this question**/
CleanlinessBldg_4 As 'Q3_4',
cast(sum(CleanlinessBldg_4)/(SQ3_tot*1.0)*100 as decimal(9,0)) as PQ3_4,
/**Show count and % of those that answered with a 5 for this question**/
CleanlinessBldg_5 As 'Q3_5',
cast(sum(CleanlinessBldg_5)/(SQ3_tot*1.0)*100 as decimal(9,0)) as PQ3_5,
CleanlinessBldg_0 As 'NM',cast(sum(CleanlinessBldg_0)/(SQ3_tot*1.0)*100 as decimal(9,0)) as 'PNM',
SQ3_Tot as Total
/**run counts **/
FROM (SELECT
Sum(CASE WHEN CleanlinessBldg='0' then 1 else 0 end) as CleanlinessBldg_0,
Sum(CASE WHEN CleanlinessBldg='1' then 1 else 0 end) as CleanlinessBldg_1,
Sum(CASE WHEN CleanlinessBldg='2' then 1 else 0 end) as CleanlinessBldg_2,
Sum(CASE WHEN CleanlinessBldg='3' then 1 else 0 end) as CleanlinessBldg_3,
Sum(CASE WHEN CleanlinessBldg='4' then 1 else 0 end) as CleanlinessBldg_4,
Sum(CASE WHEN CleanlinessBldg='5' then 1 else 0 end) as CleanlinessBldg_5,
Sum(CASE WHEN CleanlinessBldg in ('1','2','3','4','5')then 1 else 0 end) as SQ3_Tot,
Location
FROM tbl_SurveyLocations sl inner join tbl_SurveySite ssa on (sl.siteid=ssa.siteid)
Group by Location)tmp
Group by Location,CleanlinessBldg_1,CleanlinessBldg_2, CleanlinessBldg_3,
CleanlinessBldg_4,CleanlinessBldg_5, CleanlinessBldg_0,SQ3_Tot
ORDER BY Location
Question1
Location1 count of Question1 likes/dislikes
Location2 count of Question1 likes/dislikes
Location3 count of Question1 likes/dislikes
I'd like the results to come out like so:
Location1
count of Question1 likes/dislikes
count of Question2 likes/dislikes
count of Question3 likes/dislikes
count of Question4 likes/dislikes
Location is a variable in a field.
Each question (Question1, Question2, Question3, etc) is a field in the datase.
This is what I've come up with to output the data so far. Unfortunately, it'll take a lot of reformatting on an excel sheet to get it to look the way I want it to. Just trying to figure out if there's a better way of writing the query.
/** Show the count and percentage of each response **/
select
/**Show count and % of those that answered with a 1 for this question**/
Location,CleanlinessBldg_1 As 'Q3_1',
cast(sum(CleanlinessBldg_1)/(SQ3_tot*1.0)*100 as decimal(9,0))as PQ3_1,
/**Show count and % of those that answered with a 2 for this question**/
CleanlinessBldg_2 As 'Q3_2',
cast(sum(CleanlinessBldg_2)/(SQ3_tot*1.0)*100 as decimal(9,0)) as PQ3_2,
/**Show count and % of those that answered with a 3 for this question**/
CleanlinessBldg_3 As 'Q3_3',
cast(sum(CleanlinessBldg_3)/(SQ3_tot*1.0)*100 as decimal(9,0)) as PQ3_3,
/**Show count and % of those that answered with a 4 for this question**/
CleanlinessBldg_4 As 'Q3_4',
cast(sum(CleanlinessBldg_4)/(SQ3_tot*1.0)*100 as decimal(9,0)) as PQ3_4,
/**Show count and % of those that answered with a 5 for this question**/
CleanlinessBldg_5 As 'Q3_5',
cast(sum(CleanlinessBldg_5)/(SQ3_tot*1.0)*100 as decimal(9,0)) as PQ3_5,
CleanlinessBldg_0 As 'NM',cast(sum(CleanlinessBldg_0)/(SQ3_tot*1.0)*100 as decimal(9,0)) as 'PNM',
SQ3_Tot as Total
/**run counts **/
FROM (SELECT
Sum(CASE WHEN CleanlinessBldg='0' then 1 else 0 end) as CleanlinessBldg_0,
Sum(CASE WHEN CleanlinessBldg='1' then 1 else 0 end) as CleanlinessBldg_1,
Sum(CASE WHEN CleanlinessBldg='2' then 1 else 0 end) as CleanlinessBldg_2,
Sum(CASE WHEN CleanlinessBldg='3' then 1 else 0 end) as CleanlinessBldg_3,
Sum(CASE WHEN CleanlinessBldg='4' then 1 else 0 end) as CleanlinessBldg_4,
Sum(CASE WHEN CleanlinessBldg='5' then 1 else 0 end) as CleanlinessBldg_5,
Sum(CASE WHEN CleanlinessBldg in ('1','2','3','4','5')then 1 else 0 end) as SQ3_Tot,
Location
FROM tbl_SurveyLocations sl inner join tbl_SurveySite ssa on (sl.siteid=ssa.siteid)
Group by Location)tmp
Group by Location,CleanlinessBldg_1,CleanlinessBldg_2, CleanlinessBldg_3,
CleanlinessBldg_4,CleanlinessBldg_5, CleanlinessBldg_0,SQ3_Tot
ORDER BY Location