I have a list of grades that students received. Each student appears multiple times in the table.
How do I produce a list of all the students with their average grade?
P.s. I've tried looking at previously asked questions to see if I can find something relevant, but with no luck.
Look up the AVG function and the GROUP BY clause.
Since it's homework . . .
You are basically trying to aggregate data in SQL. Try googling along those lines.
Related
If I was given a question in an exam, where I would explain a piece of code from Microsoft Access SQL Query, without any external data or information.
How would I explain the following code in simple/plain English, I just want to get an idea because this is my first time explaining a code:
SELECT last_name, department_id, job_id
FROM employees
WHERE department_id IN
(
SELECT department_id
FROM departments
WHERE location_id = 1700
);
Is this a good explanation to a layman:
The following code: asks us to list employees' last name, their job at each department and their job identity. For each department that has a location ID equals/equivalent to '1700'. Would this be a great explanation?
SQL means Structured Query Language. Another word for "Query" is just "Question". Just about every query is written to provide a set of data that answers a question.
When you're explaining a code sample, or trying to figure out some code you inherited for that matter, a good place to start is to start by trying to figure out what question the results of the query were intended to answer. If you were sitting at your computer and somebody came up and asked you a question, what question would that be that would result in you writing that block of code? Oftentimes, just that question alone is enough of an explanation for a layperson.
Here, I'd say a fair start would be something along these lines.
This code answers the question, "What are the last names, department IDs and job IDs of everyone whose department is at location 1700?"
When giving a technical explanation, you take it to the next step. How does this code go about answering the question that was asked? This is the nuts and bolts of how the query works, as opposed to what it's supposed to do.
So, technically:
This code uses a sub-query against the departments table, which includes a location_id column, to identify all the departments at location_id = 1700. Then it uses the department list from the sub-query to get the last_name and job_id data from the employees table, using the department_id list as a filter.
As per my point of view, you need to explain the following thing
This query type is a subquery
This query provides the employee list.
This employee list is base on department and only those department need to consider which location id is 1700
So in a short way, you can say like this query give a list of employee which work on the specific location.
I hope you got this answer as correct answer.
I think a pithier explanation would be along the lines:
Fetch certain information about employees whose department is at location 1700.
I'm using Tableau to show some schools data.
My data structure gives a table that has all de school classes in the country. The thing is I need to count, for example, how many schools has Primary and Preschool (both).
A simplified version of my table should look like this:
In that table, if I want to know the number needed in the example, the result should be 1, because in only one school exists both Primary and Preschool.
I want to have a multiple filter in Tableau that gives me that information.
I was thinking in the SQL query that should be made and it needs a GROUP BY statement. An example of the consult is here in a fiddle: Database example query
In the SQL query I group by id all the schools that meet either one of the conditions inside de IN(...) and then count how many of them meet both (c=2).
Is there a way to do something like this in Tableau? Either using groups or sets, using advanced filters or programming a RAW SQL calculated fiel?
Thanks!
Dubafek
PS: I add a link to my question in Tableu's forum because you can download my testing workbook there: Tableu's forum question
I've solved the issue using LODs (specifically INCLUDE and EXCLUDE statements).
I created two calculated fields having the aggregation I needed:
Then I made a calculated field that leaves only the School IDs that matches the number of types they have (according with the filtering) with the number of types selected in the multiple filter (both of the fields shown above):
Finally, I used COUNTD([Condition]) to display the amounts of schools matching with at least the School types selected.
Hope this helps someone with similar issue.
PS: If someone wants the Workbook with the solution I've uploaded it in an answer in the Tableau Forum
I am writing an app that is basically a survey and I've written almost everything but this last part where I need to get stats from the survey.
The table looks like this:
user_id|question_id|answer
I need to get the average for each answer while still knowing which question ID that average value is associated with. Keep in mind that there can be multiple answers for the same question and multiple and multiple questions with the same answer. The only thing unique is the user_id. The database is Oracle but im sure I can adapt the query if needed. Just need some help with getting started.
I was able to answer my own question.
Select avg (answer), question_id
From table
Group By question_id
I'm kind new in this forum but I'm stuck in a problem and I need our help.
I Have one table with several lines where each line represent one project, then in another table I'll have many tasks that need to be done in each project, each task would have a percentage of at what level is, and the result of this two tables is one table where I'll have the process Id and also the percentage of accomplished with the average of the last entries of every tasks values.
I can't figured out the query that needs to be done to have the result that I want.
Can anyone help me? You can see follow the link bellow to see tables and the result that I want.
Table iamges
I didnt understand the colors of rows you used, but with your description, i think this is the query you are looking for:
select P.id_Proceso, P.SubProceso, avg(R.estado)
from Processos P
join Registros R
on P.ID = R.Id_processo
group by P.id_Proceso, P.SubProceso
I have a timecard file that list the daily hours for each employee. I have figured out, using a query, how to get the total number of hours but I also need the total number of employees. I have searched here and on other places and tried just about everything and nothing works. I am sure it is something pretty simple but I just cannot seem to get the total number of employees. Any help will be greatly appreciated.
The main file is called Timecards and I link to a file call Employees using the field employee number to get the employee name. There are other fields in Timecards namely "Job No", "Job Description" and "Type of Hours"( such as regular and overtime) that I wanted on the summary report but I did not realize that a employee could have two entries in those fields. So that is why I could not get the total employees to come out right when using those fields.
I was finally able to create a query and print a report with the total employees using fields that only appear once in the Timecards file. So unless someone knows how to count total lines is a report I will stick with the report I have that works.
I agree with Vladimir, it helps to post what you expect to see and what kind of tables you are working with. However, I have been in the same situation. Maybe this is a good starting point in SQL
SELECT COUNT(*) AS TotalNumberEmployees FROM TheTableShowingEmployees;
Posting a question with:
A problem statement
A list of expected results
Sample Code tried
Table structures involved (Including relevant fields, data types, and tableNames)
Will get you better results. Show your work, we'll show you ours!
Given limited information so far this is the best I can come up with
I use a left join incase some of the employees haven't reported hours yet and you want all employees.
I use coalesce to change null hours to 0.
I assume that Employees Table exists and it joins to timecard on Employee_no.
I assume firstName and LastName are in the
employeetable
.
SELECT coalesce(Sum(T.hours),0) AS SumOfhours, E.FirstName, E.LastName
FROM Employees E
LEFT JOIN Timecards T
on T.Employee_no = E.Employee_no
GROUP BY E.FirstName, E.Last_name
Also note you can edit your question don't comment on it; it makes it difficult to follow.. and last but not least .... Welcome to SO!
Well after much searching and testing I finally found a post from 2009 that took care of my problem.
`If you want to count groups, you might need to add a running sum text box in
the group header. For instance, if your report contains orders and order
details but you want to count the number of unique orders, you can't use a
text box in the report footer with a control source like:
=Count(OrderID)
This expression will count orders and details.
You can add a text box in the Order header:
Name: txtOrderCount
Control Source: =1
Running Sum: Over All
Visible: No
Then add a text box in the Report Footer section:
Control Source: =txtOrderCount
Duane Hookom
Microsoft Access MVP`
Thanks for all the help.