SQL Server Pivot table for survey responses - sql

I have a sql server table called surveys with the following data
+------------+--------------+----+----+----+----+
| ModuleCode | SurveyNumber | Q1 | Q2 | Q3 | Q4 |
+------------+--------------+----+----+----+----+
| NME3519 | 1 | 5 | 4 | 5 | 3 |
| NME3519 | 2 | 3 | 3 | 2 | 1 |
| NME3519 | 3 | 4 | 3 | 2 | 1 |
| NME3520 | 1 | 4 | 3 | 2 | 1 |
| NME3519 | 4 | 4 | 2 | 2 | 1 |
+------------+--------------+----+----+----+----+
I'd like to be able to report on one module at a time and for the result to be something like this:
Count of scores
+----------+---+---+---+---+---+
| Question | 1 | 2 | 3 | 4 | 5 |
+----------+---+---+---+---+---+
| Q1 | 0 | 0 | 1 | 2 | 1 |
| Q2 | 0 | 1 | 2 | 1 | 0 |
| Q3 | 0 | 3 | 0 | 0 | 1 |
| Q4 | 3 | 0 | 1 | 0 | 0 |
+----------+---+---+---+---+---+
I'm pretty sure from other examples I need to unpivot and then pivot but I can't get anywhere with my own data.
Many thanks
Richard

Unpivot and aggregate:
select v.question,
sum(case when v.score = 1 then 1 else 0 end) as score_1,
sum(case when v.score = 2 then 1 else 0 end) as score_2,
sum(case when v.score = 3 then 1 else 0 end) as score_3,
sum(case when v.score = 4 then 1 else 0 end) as score_4,
sum(case when v.score = 5 then 1 else 0 end) as score_5
from responses r cross apply
( values ('Q1', r.q1), ('Q2', r.q2), ('Q3', r.q3), ('Q4', r.q4), ('Q5', r.q5)
) v(question, score)
group by v.question;
This version uses a lateral join for unpivoting. I find the syntax simpler and lateral joins more powerful. Why bother learning unpivot when something else does the same thing more concisely, more powerfully, and has the same performance?
As for the pivoting, it uses conditional aggregation. In my experience with SQL Server, this has pretty much the same performance as pivot.

Related

SQL Count columns from other columns

I have this view generated after using LEFT JOIN over 2 tables (simplified example).
Tables:
T1: Id, ...other columns not used
+----+-----+
| Id | ... |
+----+-----+
| 1 | ... |
| 2 | ... |
+----+-----+
T2: Id, NewId (Foreign Key from T1), Status, ...other columns not used
+-----+-------+--------+-----+
| Id | NewId | Status | ... |
+-----+-------+--------+-----+
| 1 | 1 | 1 | ... |
| 2 | 1 | 2 | ... |
| 3 | 1 | 2 | ... |
| 4 | 1 | 3 | ... |
| 5 | 1 | 1 | ... |
| 6 | 1 | 1 | ... |
| 7 | 2 | 0 | ... |
| 8 | 2 | 2 | ... |
| 9 | 2 | 1 | ... |
| 10 | 2 | 2 | ... |
+-----+-------+--------+-----+
Current View:
SELECT
T1.Id,
T2.Status
FROM T1
LEFT JOIN T2 ON T1.Id = T2.NewId;
View: (I got till here)
+----+--------+
| Id | Status |
+----+--------+
| 1 | 1 |
| 1 | 2 |
| 1 | 2 |
| 1 | 3 |
| 1 | 1 |
| 1 | 1 |
| 2 | 0 |
| 2 | 2 |
| 2 | 1 |
| 2 | 2 |
+----+--------+
The required view needs to have separate columns for each status value (which are exactly 0, 1, 2 or 3). 0 & 1 are kept in the same column.
Required View: (But I need this)
+----+------------+----------+----------+
| Id | Status 0/1 | Status 2 | Status 3 |
+----+------------+----------+----------+
| 1 | 1 | 1 | 1 |
| 2 | 2 | 2 | 2 |
+----+------------+----------+----------+
I feel like I missing something basic. How can I get this view?
I don't think we need Rank() stuff, and it's a big query over 4 normalized tables (in practice), which is why I need an optimal solution. Subqueries work with inline SELECT queries, but they need JOINs as well. The production requirement has 10 columns for counts over 2 separate columns.
Use conditional aggregation:
select
id,
sum(case when status in (0, 1) then 1 else 0 end) status_0_1,
sum(case when status = 2 then 1 else 0 end) status_2
sum(case when status = 3 then 1 else 0 end) status_3
from mytable
group by id
In your orignal query, this should look like:
select
t1.id,
sum(case when t2.status in (0, 1) then 1 else 0 end) status_0_1,
sum(case when t2.status = 2 then 1 else 0 end) status_2
sum(case when t2.status = 3 then 1 else 0 end) status_3
from t1
left join t2 on t1.id = t2.newid
group by t1.id
Use aggregation:
select id,
sum(case when status in (0, 1) then 1 else 0 end) as status_01,
sum(case when status = 2 then 1 else 0 end) as status_2,
sum(case when status = 3 then 1 else 0 end) as status_3
from t
group by id;
You should be able to build this directly into your left join:
from a left join
b
on . . .
Can be the from clause. Or use your current query as a subquery or CTE.

Query to give count of unique results in column A but then split by values in column B

I'm trying to create a SQL query to count the number of occurences of a dynamic value in one column, but then have this count split by what is in another column.
If the table I'm getting the data from looks something like:
id | Team | Period |
____________________
1 | A | 1 |
2 | B | 1 |
3 | B | 1 |
4 | A | 1 |
5 | E | 2 |
6 | D | 3 |
7 | A | 3 |
8 | A | 3 |
9 | B | 4 |
10 | A | 4 |
11 | C | 4 |
etc...
I want to get a list of how many times each team appears per period like so.
team | period | count |
_________________________
A | 1 | 2 |
B | 1 | 2 |
C | 1 | 0 |
D | 1 | 0 |
E | 1 | 0 |
A | 2 | 0 |
B | 2 | 0 |
C | 2 | 0 |
D | 2 | 0 |
E | 2 | 0 |
A | 3 | 2 |
This will be used in a PHP page to then make an assoc array and print out the data for reporting purposes.
I have previously used things like
SELECT sum(case when somecolumn = 'blah' then 1 else 0 end) as blah_count
But I can't do that here, because in future the names of the teams may change to a currently unknown value, so I can't use the names in the query. (and no, I won't be told about this so I can change the query.) So I need a query where it both gives count of any occurrence in the team column and splits them by period. Period will always be a number from 1 to 13.
You could cross join all distinct values of teams and periods, and then left join the original table and aggregate.
select
te.team,
pe.period,
count(ta.team) cnt
from
(select distinct team from mytable) te
cross join (select distinct period from mytable) pe
left join mytable ta
on ta.team = te.team
and ta.period = pe.period
group by te.team, pe.period
order by pe.period, te.team
Demo on DB Fiddle:
team | period | cnt
:--- | -----: | --:
A | 1 | 2
B | 1 | 2
C | 1 | 0
D | 1 | 0
E | 1 | 0
A | 2 | 0
B | 2 | 0
C | 2 | 0
D | 2 | 0
E | 2 | 1
A | 3 | 2
B | 3 | 0
C | 3 | 0
D | 3 | 1
E | 3 | 0
A | 4 | 1
B | 4 | 1
C | 4 | 1
D | 4 | 0
E | 4 | 0
SELECT team,period,count(*) as count
from TABLENAME group by team
Why not pivot the periods?
select team,
sum( period = 1 ) as period_1,
sum( period = 2 ) as period_2,
sum( period = 3 ) as period_3,
sum( period = 4 ) as period_4
from t
group by team;
You seem to know that there are four periods.

Oracle SQL count and group by multiple fields

I am able to get the data merging two tables to get the following table.
+------------+------+--------+--------+------------+------------+
| Group Name | Type | Manger | Status | ControlOne | ControlTwo |
+------------+------+--------+--------+------------+------------+
| Group A | 1 | 1 | finish | 2 | 2 |
| Group A | 2 | 1 | open | 0 | 2 |
| Group A | 1 | 1 | finish | 0 | 0 |
| Group A | 1 | 2 | finish | 2 | 0 |
| Group B | 1 | 1 | open | 2 | 0 |
| Group B | 1 | 2 | open | 2 | 2 |
| Group B | 2 | 2 | open | 0 | 2 |
| Group B | 2 | 1 | finish | 0 | 0 |
| Group B | 1 | 1 | open | 2 | 0 |
+------------+------+--------+--------+------------+------------+
Now I need to get the total counts based on GroupName/ Type and Manager to have the output for each group in the following format:
+------------+------+-------------------------------------------------+--------------------------------------------+------------------------------+----------------------------+
| Group Name | Type | Manager1Finish | Manager1Open | Manager2Finish | Manager2Open |
+------------+------+-------------------------------------------------+--------------------------------------------+------------------------------+----------------------------+
| Group A | 1 | 2(count of finish by Group A, manager1, type 1) | 0(count of open Manager1, Type 1, Group A) | 1(count of finish Manager 2) | 0(count of open manager 2) |
| Group A | 2 | 0 | 1 | 0 | 0 |
+------------+------+-------------------------------------------------+--------------------------------------------+------------------------------+----------------------------+
Could you please help to how to achieve this?
Try with CASE WHEN:
SELECT GroupName,
TYPE,
COUNT (CASE
WHEN Manager = 1
AND status = 'Finish'
THEN
1
END)
AS Manager1Finish,
COUNT (CASE
WHEN Manager = 1
AND status = 'Open'
THEN
1
END)
AS Manager1Open,
COUNT (CASE
WHEN Manager = 2
AND status = 'Finish'
THEN
1
END)
AS Manager2Finish,
COUNT (CASE
WHEN Manager = 2
AND status = 'Open'
THEN
2
END)
AS Manager2Open
FROM tablename
GROUP BY GroupName, TYPE
select [group], [type],
sum(case when manager=1 and status='finish' then 1 else 0 end) as m1finish,
sum(case when manager=1 and status='open' then 1 else 0 end) as m1open,
sum(...etc...)
from mytable
group by [group],[type]

SQL Aggregation depending on value of attribute in unselected column

I've got a table TABLE1 like this:
|--------------|--------------|--------------|
| POS | UNIT | VOLUME |
|--------------|--------------|--------------|
| 1 | M2 | 20 |
| 1 | M2 | 30 |
| 1 | M3 | 40 |
| 2 | M2 | 100 |
| 2 | M3 | 20 |
| 3 | ST | 30 |
| 3 | M2 | 10 |
|--------------|--------------|--------------|
Depending on the value of the column UNIT I want to aggregate as follows (each UNIT becomes a new column with the sum of the according value):
|--------------|--------------|--------------|--------------|
| POS | VOLUME_M2 | VOLUME_M3 | VOLUME_ST |
|--------------|--------------|--------------|--------------|
| 1 | 50 | 40 | 0 |
| 2 | 100 | 20 | 0 |
| 3 | 10 | 0 | 30 |
|--------------|--------------|--------------|--------------|
My Solution is
SELECT POS,
CASE
WHEN UNIT = 'M2'
THEN SUM(VOLUME)
ELSE 0
END AS VOLUME_M2,
CASE
WHEN UNIT = 'M3'
THEN SUM(VOLUME)
ELSE 0
END AS VOLUME_M3,
CASE
WHEN UNIT = 'ST'
THEN SUM(VOLUME)
ELSE 0
END AS VOLUME_S
FROM TABLE1
GROUP BY POS, UNIT
My problem is, that my code does not work if I leave out UNIT in the GROUP BY statement (I either have to use it in my aggregation or in my GROUP BY statement)
Therefore I get something like this:
|--------------|--------------|--------------|--------------|
| POS | VOLUME_M2 | VOLUME_M3 | VOLUME_ST |
|--------------|--------------|--------------|--------------|
| 1 | 50 | 0 | 0 |
| 1 | 0 | 40 | 0 |
| 2 | 0 | 20 | 0 |
| 2 | 100 | 0 | 0 |
| 3 | 10 | 0 | 0 |
| 3 | 0 | 0 | 30 |
|--------------|--------------|--------------|--------------|
Besides, could anyone give me a hint, how it is possible to automatically get this type of result (especially if there are a lot of values for UNIT).
Close. For conditional aggregation, the case expression is an argument to the aggregation function:
SELECT POS,
SUM(CASE WHEN UNIT = 'M2' THEN VOLUME ELSE 0 END) AS VOLUME_M2,
SUM(CASE WHEN UNIT = 'M3' THEN VOLUME ELSE 0 END) AS VOLUME_M3,
SUM(CASE WHEN UNIT = 'ST' THEN VOLUME ELSE 0 END) AS VOLUME_ST
FROM TABLE1
GROUP BY POS;

MySQL: Pivot + Counting

I need help with a SQL that will convert this table:
===================
| Id | FK | Status|
===================
| 1 | A | 100 |
| 2 | A | 101 |
| 3 | B | 100 |
| 4 | B | 101 |
| 5 | C | 100 |
| 6 | C | 101 |
| 7 | A | 102 |
| 8 | A | 102 |
| 9 | B | 102 |
| 10 | B | 102 |
===================
to this:
==========================================
| FK | Count 100 | Count 101 | Count 102 |
==========================================
| A | 1 | 1 | 2 |
| B | 1 | 1 | 2 |
| C | 1 | 1 | 0 |
==========================================
I can so simple counts, etc., but am struggling trying to pivot the table with the information derived. Any help is appreciated.
Use:
SELECT t.fk,
SUM(CASE WHEN t.status = 100 THEN 1 ELSE 0 END) AS count_100,
SUM(CASE WHEN t.status = 101 THEN 1 ELSE 0 END) AS count_101,
SUM(CASE WHEN t.status = 102 THEN 1 ELSE 0 END) AS count_102
FROM TABLE t
GROUP BY t.fk
use:
select * from
(select fk,fk as fk1,statusFK from #t
) as t
pivot
(COUNT(fk1) for statusFK IN ([100],[101],[102])
) AS pt
Just adding a shortcut to #OMG's answer.
You can eliminate CASE statement:
SELECT t.fk,
SUM(t.status = 100) AS count_100,
SUM(t.status = 101) AS count_101,
SUM(t.status = 102) AS count_102
FROM TABLE t
GROUP BY t.fk