I am new to SQL so, I have a table that holds records for assigned jobs as for employee, holding hours required to complete the job.
An employee might have a lot records, regarding to different jobs that was assigned. So what I want is to select all the employees and sum their hours assigned in total..
I want an output to display each employee individually and the number of total hours assigned.
Something like: select username, Count(Hours) from mytable
group by username
The query above obviously is not giving the expected output.
Is there/what is the way to do that?
Any help is welcome!
Assuming username is unique, you should replace count() aggregate function with sum():
select username, sum(Hours)
from mytable
group by username
Related
I have the following table and I wanted to get the running unique count by dept, team, level. But the cumulative unique count will restart per year.
Note: sorry in my main table example, employee numbers may repeat up to four times. There is another column called leave type but wasn't able to illustrate it in the image
Main table
Expected output would be something like below.
Expected output
Is this possible? Apologies. Not too advanced when it comes to SQL. Thank you.
You can do:
select max(extract(year from date)), date, department, level, team, count(*)
from t
group by date, department, level, team
I have an SQL table that I'm trying to write a Query for. The table has four columns, course_id, title, dept_name and credits. Every course is different and has a unique name, but many of the courses share dept_name. Each course has between 1 and 4 credits.
Complete Table
The query I'm trying to write needs to first combine the rows that have the same dept_name, and then only display the resulting departments and credit amounts with a credit sum below 30.
By using the following Query I'm able to return the credit sum grouped by dept_name, but I can't seem to only return the dept_names with a sum of less than 30. Can anyone help me out here? I've tried adding additional WHERE statements, but I just receive errors. Please let me know if this is enough or if more info is needed.
Half-Filtered Table
Use HAVING. For example:
select deptname, sum(credits)
from bk_course
group by dept_name
having sum(credits) < 30
I have an sql statement which I cant get the structure right on, when I run what I currently have It says syntax wrong. I am looking the result to look like this:-
(source: churchcom.co.uk)
.
This is my query so far but I dont think I am on the right track at all.
SELECT Name, ValueofTaught, Amount
FROM Activities
WHERE (Department = #Department)
ORDER BY Name
GROUP BY BurnhamGrade
(SUM ValueofTaught AND Amount
WHERE Departmetn = #Department)
Structure of the activities Table is like this:-
(source: churchcom.co.uk)
.
It sort of sounds like you're looking to group by two columns. I'm assuming the Value of Taught column is what gets rolled up for hours:
SELECT Department, Name, SUM([Value of Taught]) Hours, SUM(Amount) Pay
FROM Activities
GROUP BY Department, Name
ORDER BY Department, Name
WITH ROLLUP
I am trying to create a query that will return results for the number of distinct users who have accessed something by date. Right now I have a query that will display 2 columns, the first being date and the second being user name. It will list all the distinct users who accessed the application on a certain date but they will each have their own distinct row. Here is the query that does that:
SELECT DISTINCT logdate, User AS ReportUser
FROM table
WHERE appname='abcd1234' AND logdate >=DATE-30
I have tried putting COUNT() around User but it says selected non-aggregate values must be part of the associated group.
Any idea how I can get this query to just show like row for the past 30 days and the count of distinct users?
This will be the right approach for that.
SELECT logdate, Count(User) AS ReportUser
FROM table
WHERE appname='abcd1234' AND logdate >=DATE-30
GROUP BY 1
Never use DINTINCT in Teradata. It always slows down your query performance. Use GROUP BYinstead.
CORRECTION In Teradata 13 the optimizer is able to determines which version is more efficient, based on statistics. This can be found in the Release Summary for Teradata 13 under "Group By and DISTINCT Performance Equivalence". - http://www.info.teradata.com/edownload.cfm?itemid=083440012 (PDF)
use "GROUP BY" after WHERE clause
SELECT logdate, COUNT (User) AS ReportUser
FROM table
WHERE appname='abcd1234' AND logdate >=DATE-30
GROUP BY logdate
I have a table Logins { Id, AccessTime }, I'd like to write a query that returns 3 columns, Total Logins during a time period, Unique Logins for a time period, and the Id of the user.
I know I could do this with two passes and a join, is there a better way to do it in a single pass?
Thanks,
~Prescott
Edit: After considering this futher, if they they show up at all, then they are a unique login, thus I can just grab count(*), userId group by userid.
Sorry for the sillyness.
Poorly thought out before asking this question, essentially, if they have a record in the date period for Count(*), then their unique login is 1..