table featuring fictitious employee data, area ( in hectares (ha)) cleared (noted in french as superficie), rate for 1 hectare of cleared land on the specific lot (french:taux) and amount due( expr1) for that lot.
My problem here is that I want the total amount due for each Worker, not the amount due for each worker for each lot. Totals for Sirs Alain, Jacques, Paul, Roger and Tanguay should normally be 4066, 4082 , 5638, 5811 and 3131 , respectively.
My code so far is this
SELECT tbl_Employés.Num_deb, tbl_Employés.Prénom, tbl_Employés.Nom, tbl_Employés.Age, tbl_Employés.DEP, tbl_Employés.Expérience, tbl_Employés.Adresse, Tbl_terrain.superficie, Tbl_terrain.Taux, [superficie]*[taux] AS [Montant à payer]
FROM tbl_Employés INNER JOIN Tbl_terrain ON tbl_Employés.Num_deb = Tbl_terrain.Num_deb
ORDER BY tbl_Employés.Nom;
I have so far tried to use GROUP BY Numéro_terrain, which returns an error that my query does not include the specified expression ''Num_deb'' as a part of an aggregation fuction
I woul greatly appreciate any imput. I am very sorry if you have a hard time understanding some words, as I am doing my best to translate everything from french.
One method is to include all the columns in the GROUP BY that are not arguments to aggregation functions:
SELECT e.Num_deb, e.Prénom, e.Nom, e.Age, e.DEP, e.Expérience, e.Adresse,
SUM([superficie]*[taux]) AS [Montant à payer]
FROM tbl_Employés as e INNER JOIN
Tbl_terrain as t
ON e.Num_deb = t.Num_deb
GROUP BY e.Num_deb, e.Prénom, e.Nom, e.Age, e.DEP, e.Expérience, e.Adresse
ORDER BY e.Nom;
Related
I have a simple manufacturing job card system that track parts and labor for an assigned job.
It consists of a JobHeader table that holds the Job Card number (JobHeader.JobNo), ID of the part being manufactured (JobHeader.RegNo) and quantity to be manufactured (JobHeader.RegNo).
There is a child table (JobLabour) that tracks all the times that have been worked on the job (JobLabour.WorkedTime)
I'm looking for a query that will return the average time taken to produce a part accross the last 5 job cards for that particular part.
The following query
SELECT TOP 5 JobHeader.RegNo, JobHeader.BOMQty, sum(JobLabour.WorkedTime) AS TotalTime FROM JobHeader INNER JOIN JobLabour ON JobHeader.JobNo=JobLabour.JobNo
WHERE JobHeader.RegNo='RM-BRU-0134'
GROUP BY JobHeader.BOMQty, JobHeader.JobNo, JobHeader.RegNo
will return this:
But what I'm looking for is a query that will return the average BOMQty and average totalTime. Something like this:
Is there a way to do this?
Your question explicitly mentions the "last five" but does not specify how that is determined. Presumably, you have some sort of date/time column in the data that defines this.
In SQL Server, you can use apply:
select jh.*, jl.*
from jobheader jh outer apply
(select top (5) avg(BOMQty) as avg_BOMQty, avg(totalTime) as avg_totalTime
from (select top (5) jl.*
from joblabour jl
where jl.regno = jh.regno
order by jl.<some datetime> -- however you determine the last five
) jl
) jl;
You can add a where clause to the outer query to filter on one or more particular jobs.
If I understand you correctly this will do the work
this will work for 1 RegNo='RM-BRU-0134' at a time
with topFive as (
SELECT TOP 5 JobHeader.RegNo, JobHeader.BOMQty, sum(JobLabour.WorkedTime) AS TotalTime
FROM JobHeader
INNER JOIN JobLabour ON JobHeader.JobNo = JobLabour.JobNo
WHERE JobHeader.RegNo = 'RM-BRU-0134'
GROUP BY JobHeader.BOMQty, JobHeader.JobNo, JobHeader.RegNo
)
select RegNo, avg(BOMQty) as BOMQty, avg(TotalTime) as TotalTime
from topFive
group by RegNo
I am new on this, so a little about my aspirations :
I am technical engineer, and wanting to improve my SQL skills, and get to advanced/master level for the purpose of going down the business analyst route, leveraging data analysis to give me an advantage in the role.
I have just about go my head around correlated sub queries, ableit confusing termonology, and 9/10 being able to mentally remember what to write(not without trial and error), anyway my problem/question is as below:
I am using the adventure works DB and am using the below script to return workers who have a rate above the avg for their department
select Concat(v_dep.FirstName, ' ', v_dep.LastName) as Full_name, eph.Rate
from HumanResources.vEmployeeDepartment v_dep
join HumanResources.EmployeePayHistory eph
on v_dep.BusinessEntityID = eph.BusinessEntityID
where eph.Rate > (select AVG(eph2.rate)
from HumanResources.vEmployeeDepartment v_dep2
join HumanResources.EmployeePayHistory eph2
on v_dep2.BusinessEntityID = eph2.BusinessEntityID
where v_dep2.Department = v_dep.Department);
However, what I want to do for comparative purposes is have an additional column next to their rate, showing me the average rate for the department they belong to. I have been trolling through the web trying to construct a concise question to encapsulate what I have described but have been struggling.
Any advice would be great !
It is time you learned about window functions:
select x.*
from (select Concat(v_dep.FirstName, ' ', v_dep.LastName) as Full_name,
eph.rate,
avg(eph.rate) over (partition by v_dep.BusinessEntityID) as avg_rate
from HumanResources.vEmployeeDepartment v_dep join
HumanResources.EmployeePayHistory eph
on v_dep.BusinessEntityID = eph.BusinessEntityID
) x
where rate > avg_rate;
Note: I'm not intimately familiar with the database. However, a table with the name EmployeePayHistory sounds like it has multiple rows per employee. If so, you probably want to filter based on the time or the "current" rate.
So this is the requirement I need to meet:
Aggregated data of all the schools in the ESD, grouped by
SchoolDistrict.SchoolDistrictID
(get the same data as the school district scenario above, then add grouping by district, filtered to
EducationServiceDistrict. EducationServiceDistrictID
)
Also calculate percentage of pass, fail, and untested
How do I calculate the percentage pass, fail, and untested?
This is the query I have written so far.
CREATE VIEW district_map AS
SELECT * and SchoolDistrictID,
EducationServiceDistrict
FROM SchoolDistrict_View
and SchoolDistrict,
EducationServiceDistrict
GROUP BY EducationServiceDistrict.EducationServiceDistrictID
ORDER BYLeadWaterTestLocation.PassFail
This is the general idea of how these problems are solved - if you understand this simplified version you will be able to solve your problem.
select d.districtName,
s.studentCount,
case when s.studentCount=0 then 0 else s.passed / s.studentCount * 100 end as PassedPct
from district d
join (select districtId,
sum(studentCount) as studentCount,
sum(passed) as passed
from schools
group by districtId) as s
on d.districtId = s.districtId
order by d.districtName
I have a table storing sports results for a series of events: ONS_Skippers
The relevant columns from this table for the question are:
FK_EventID, FK_SkipperID and intResult.
I'm presenting different statistics from this database, but I've not succeeded to generate the query for the most advanced one: I would like to list average performance for each participant (FK_SkipperID). I've defined performance to be 100% for an event win, 0% for last place in an event and performance on a linear curve between the two extents. The formula for this is:
Performance = 100*(1-(intResult-1)/(NumberOfParticipantsInTheEvent-1))
NumberOfParticipantsInTheEvent varies from each event, hence needs to be counted from each group of FK_EventID. All my attempts so far has failed:
Example:
SELECT FK_SkipperID, AVG((1-(intResult-1.0)/((SELECT Count(FK_EventID)
FROM ONS_Skippers AS ONS_Skippers2
WHERE ONS_Skippers.FK_EventID = ONS_Skippers2.FK_EventID AND FK_SkipperID > 0
GROUP BY FK_EventID)-1))*100)
FROM ONS_Skippers
GROUP BY FK_SkipperID
This gives error messages "Cannot perform an aggregate function on an expression containing an aggregate or a subquery".
Any idea on how to produce the wanted output?
Try to join to the subquery instead:
SELECT
FK_SkipperID,
AVG((1-(intResult-1.0)/(e.events-1))*100)
FROM ONS_Skippers o
INNER JOIN
(
SELECT Count(FK_EventID) AS events
FROM ONS_Skippers AS ONS_Skippers2
WHERE FK_SkipperID > 0
GROUP BY FK_EventID
) e
ON o.FK_EventID = e.FK_EventID
GROUP BY FK_SkipperID
I think you could achieve this by joining to an inline table as follows...
select SkipperID,
AVG(100*(1-(Result-1)/(p.NumParticipants-1))) as Performance
from Spike.Skippers s
inner join (
select EventId, Count(EventId) as NumParticipants -- Or Max(Result)
from Spike.Skippers
group by EventID
) p on s.EventID = p.EventID
group by SkipperID
[Edit] Apologies for not sticking to your column naming conventions - my OCD insisted I adhere to my own personal standard. Fussy, I know. [/Edit]
SELECT NMC.*, Exam.Final_Exam_Level
FROM
(SELECT Technicians.Technician_ID AS Technician_ID,
Technicians.First_Name AS First_Name,
Technicians.Surname AS Surname,
MAX(New_Models.Date_Issued) AS Last_Course_Date,
MAX(New_Models.Issue) AS Last_Issue,
MAX(New_Models.Model_ID) AS Last_Model_ID,
Technicians.Course_Level AS No_Training_Courses
FROM New_Models, New_Models_Allocation, Technicians
WHERE New_Models.Model_ID=New_models_Allocation.Model_ID
And Technicians.Technician_ID=New_Models_Allocation.Technician_ID
GROUP BY Technicians.Technician_ID, Technicians.Course_Level, First_Name, Surname
ORDER BY MAX(New_Models.Model_ID) DESC)
AS NMC
INNER JOIN (SELECT Technicians.Technician_ID, COUNT(*) AS Final_Exam_Level
FROM Technicians, Exams, Exam_Allocation
WHERE (Technicians.Technician_ID)=Exam_Allocation.Technician_ID
And ((Exams.Exam_ID)=Exam_Allocation.Exam_ID)
And (Exams.Date_Taken)<=#12/31/2010#
GROUP BY Technicians.Technician_ID, Technicians.Course_Level
ORDER BY Technicians.Technician_ID)
AS Exam ON Exam.Technician_ID=NMC.Technician_ID;
This query shows each technician, Last Exam, Last New_Model, Last course.
SELECT Technicians.Technician_ID, Jobs.Job_ID, Jobs.Date_Occured, Fix
FROM Technicians, Jobs, Tech_Allocation, Recovery
WHERE Technicians.Technician_ID=Tech_Allocation.Technician_ID
And Jobs.Job_ID=Tech_Allocation.Job_ID
And Jobs.Job_ID=Recovery.Job_ID
And Jobs.Date_Occured>=#1/1/2010#
And Jobs.Date_Occured<=#12/31/2010#
ORDER BY Fix;
This query shows the jobs each technician has done.
However, when creating a report in Ms Access, the jobs are repeated. Hence, instead of a technician having done 3 jobs, it shows 12 for example. Although when running the second query itself, results aren't repeated.
Any Help?
For obvious reasons, I don't usually read other people's SQL queries, but your example was very well formatted. Is this the problem?
INNER JOIN (SELECT Technicians.Technician_ID, COUNT(*) AS Final_Exam_Level
...
GROUP BY Technicians.Technician_ID, Technicians.Course_Level
These 2 lines are from the 2nd subquery of your first query. You have 1 index field (Technician_ID), but 2 grouping fields (Technician_ID and Course_Level). This would produce results like:
Technician_ID Final_Exam_Level
Bob 5
Bob 4
Nadine 5
I recommend either removing Course_Level from the Group By or adding it to the Select row.