So, I have a table where a bunch of people took multiple surveys. Each survey had a rating out of 10. I need to calculate the average rating per survey and number of 10s (which I've already done). Now I need to find the percentage of 10s
Below is the code I have currently:
SELECT person,
survey,
AVG(rating),
COUNT(CASE WHEN rating = 10 THEN 1 END)
FROM Table
GROUP BY person, survey
I have a general of idea of how to calculate percentages, but struggling on how to calculate the percentage of just the 10's. Thanks in advance.
SELECT rating, COUNT(CASE WHEN rating = 10 THEN 1 END) / COUNT(*) FROM table
GROUP BY rating;
Did you mean this?
Related
I've got this table I've defined in PowerBI:
I'd like to define a new table which has the percentage of medals won by USA from the total of medals that were given that year for each sport.
An example:
Year Sport Percentage
1986 Aquatics 0.0%
How could I do it?
You can use SUMMARIZE() to calculate a new table:
NewTable =
SUMMARIZE(
yourDataTable;
[Year];
[Sports];
"Pct";
DIVIDE(
CALCULATE(
COUNTROWS(yourDataTable);
yourDataTable[Nat] = "USA"
);
CALCULATE(
COUNTROWS(yourDataTable);
ALLEXCEPT(
yourDataTable;
yourDataTable[Year];
yourDataTable[Sports]
)
);
0
)
I know that an answer has already been accepted, but I feel that I should provide my suggested solution to utilize all of Power BI's capabilities.
By creating a calculated table, you are limited in what you can do with the data, in that it is hard coded to be filtered to USA and is only based on Year and Sport. While that is the current requirements, what if they change? Then you have to recode your table or make another one.
My suggestion is to use measures to accomplish this task, and here's how...
First, here is my set of sample data.
With that data, I created a simple measure that count the rows to get the count of medals.
Medal Count = COUNTROWS(Olympics)
Throwing together a basic matrix with that measure we can see the data like this.
A second measure can then be created to get a percentage for a specific country.
Country Medal Percentage = DIVIDE([Medal Count], CALCULATE([Medal Count], ALL(Olympics[Country])), BLANK())
Adding that measure to the matrix we can start to see our percentages.
From that matrix, we can see that USA won 25% of all medals in 2000. And their 2 medals in Sport B made up 33.33% of all medals that year.
With this you can utilize slicers and the layout of the matrix to get the desired percentage. Here's a small example with a country and year slicer that shows the same numbers.
From here you are able to cut the data by any sport or year and see the percentage of any selected country (or countries).
Using sqlite3, I have a column "grades" in table "students" and I want to get the proportion of students who scored over 80 on a test. How do I get that? I can select count(*) from students and then select count(*) from students where score>80, but how do I get the proportion in one statement?
Here is a simple way to do this:
SELECT
AVG(CASE WHEN grades > 80 THEN 1 ELSE 0 END)
FROM students;
This just takes a conditional average over the entire table, counting the number of students with a grade over 80, then normalizing that count by the total number of students.
For a certain data range, for a specific dimension, I need to calculate the average value of a daily rank based on the average value.
First of all this is the starting point:
This is quite simple and for each day and category I get the AVG(value) and the Ranke based on that AVG(Value) computed using Category.
Now what I need is "just" a table with one row for each Category with the average value of that rank for the overall period.
Something like this:
Category Global Rank
A (blue) 1,6 (1+3+1+1+1+3)/6
B (orange) 2,3 (3+2+3+2+2+2)/6
C (red) 2,0 (2+1+2+3+3+1)/6
I tried using the LOD but it's not possble using rank table calculation inside them so I'm wondering if I'm missing anything or if it's even possible in Tableau.
Please find attached the twbx with the raw data here:
Any Help would be appreciated.
I am new to SQL and am having trouble with a (fairly simple) query to rank time stamps.
I have one table with survey data from 2014. I am trying to determine the 'learning curve' for good customer satisfaction performance. I want to order and rank each survey at an agent level based on the time stamp of the survey. This would let me see what the average performance is when an agent has 5 total surveys, 10, 20 etc.
I imagine it should be something like (table name is tablerank):
select T1.*,
(select count(*)
from tablerank as T2
where T2.call_date > T1.call_date
) as SurveyRank
from tablerank as T1
where p1.Agent_ID = T2.Agent_ID;
For each agent, it would list each survey in order and tag a 1 for the earliest survey, a 2 for the second earliest, etc. Then I could Pivot the data in Excel and see the learning curve based on survey count rather than tenure or time (since surveys are more rare, sometimes you only get 1 or 2 in a month).
A correlated subquery must have the correlation in the subquery itself; any table names/aliases from the subquery (such as T2) are not visible in the outer query.
For ranking, you want to count earlier surveys, and you want to include the current survey so that the first one gets the rank number 1, so you need to use <= instead of >:
SELECT *,
(SELECT COUNT(*)
FROM tablerank AS T2
WHERE T2.Agent_ID = T1.Agent_ID
AND T2.call_date <= T1.call_date
) AS SurveyRank
FROM tablerank AS T1
I have a very large dataset with employers, employees and salaries.
Each employee has a salary and is linked to an employer.
Employers can have hundreds, even thousands of employees working for them.
I want to find the average salary per employer. ie, I want to return an output with just 1 line per employer with an average salary based on all the employees they have.
Thanks
You can use aggregate for this:
aggregate(salaries, by = list(employers), FUN = mean)