Calculate Percentage Of Certified Managers - sql

I asked a similar question a few weeks ago, but now the requirements have changed.
Considering the following tables:
http://www.maroisconsulting.com/Temp/query.png
I need to create a query that returns the percentages of employees who are managers (Titles.IsManager) and who have a date in the Certified field (Employees.Certified). The results need to be grouped by the Group each store is in.
So far I have this:
SELECT d.GroupId,
Sum(d.cert_complete) AS SumOfcert_complete,
Count(d.cert_complete) AS CountOfcert_complete
FROM (SELECT DISTINCT
s.GroupId,
e.EmployeeID,
IIf(e.Certified Is Null,0,1) AS cert_complete
FROM
((Stores AS s
INNER JOIN EmployeeStores AS es ON s.StoreId = es.StoreId)
INNER JOIN Employees AS e ON es.EmployeeId = e.EmployeeID)
INNER JOIN Titles AS t ON e.TitleId = t.TitleId
) AS d
WHERE t.IsManager
GROUP BY d.GroupId;
And then this
SELECT q.GroupId,
(SumOfcert_complete/CountOfcert_complete)*100 AS percent_certified,
Groups.GroupName
FROM qryGroupCert_base AS q
INNER JOIN Groups ON q.GroupId = Groups.GroupId;
You can see in the first query where I added the Titles table.
1) I get prompted for the IsManager, although I don't know why
2) The results coming back are not different than before I added the IsManager
Anyone see what's wrong here?
Many thanks

Within your first query, you have this subquery which includes Titles aliased as "t":
(SELECT DISTINCT
s.GroupId,
e.EmployeeID,
IIf(e.Certified Is Null,0,1) AS cert_complete
FROM
((Stores AS s
INNER JOIN EmployeeStores AS es ON s.StoreId = es.StoreId)
INNER JOIN Employees AS e ON es.EmployeeId = e.EmployeeID)
INNER JOIN Titles AS t ON e.TitleId = t.TitleId
) AS d
Then, after the definition of the subquery, you have this WHERE clause:
WHERE t.IsManager
The problem is the "t" alias and IsManager column only exist within the the subquery --> they are unknown to the outer (parent) query. In cases where the Access database engine encounters something it doesn't recognize as an object name, function, literal value, or SQL keyword, it thinks that something must be a parameter ... so pops up the input box asking you to provide a value for the (IsManager) parameter.
I think you should move the WHERE clause inside the subquery definition.
SELECT d.GroupId,
Sum(d.cert_complete) AS SumOfcert_complete,
Count(d.cert_complete) AS CountOfcert_complete
FROM [SELECT DISTINCT
s.GroupId,
e.EmployeeID,
IIf(e.Certified Is Null,0,1) AS cert_complete
FROM
((Stores AS s
INNER JOIN EmployeeStores AS es ON s.StoreId = es.StoreId)
INNER JOIN Employees AS e ON es.EmployeeId = e.EmployeeID)
INNER JOIN Titles AS t ON e.TitleId = t.TitleId
WHERE t.IsManager = True
]. AS d
GROUP BY d.GroupId;

Perhaps you need to supply a criteria for t.IsManager, such as t.IsManager = TRUE. If the where clause doesn't have a value to set it equal to, Access probably isn't resolving it to the actual column, but thinks it's a query parameter.

Related

Struggling with SQL subquery selection

I'm trying to answer a SQL question for revision purposes but can't seem to work out how to get it to work. The tables in question are:
The question is asking me to write an SQL command to display for each employee who has a total distance from all journeys of more than 100, the employee's name and the total number of litres used by the employee on all journeys (the number of litres for a journey is distanceInKm / kmPerLitre).
So far I've tried several variations of code beginning with:
SELECT
name, TravelCost.distanceInKm / Car.kmPerLitre AS "Cost in Litres"
FROM
Employee, Car, TravelCost
WHERE
Employee.id = TravelCost.employeeID
AND Car.regNo = TravelCost.carRegNo
It's at this point I get a bit stuck, any help would be greatly appreciated, thanks!
Never use commas in the FROM clause. Always use proper, standard, explicit JOIN syntax.
You are missing a GROUP BY and a HAVING:
SELECT e.name, SUM(tc.distanceInKm / c.kmPerLitre) AS "Cost in Litres"
FROM Employee e JOIN
TravelCost tc
ON e.id = tc.employeeID JOIN
Car c
ON c.regNo = tc.carRegNo
GROUP BY e.name
HAVING SUM(tc.distanceInKm) > 100;
Use Group By and Having Clause
SELECT NAME,
Sum(TravelCost.distanceInKm/ Car.kmPerLitre) AS "Cost in Litres"
FROM Employee
INNER JOIN TravelCost
ON Employee.id = TravelCost.employeeID
INNER JOIN Car
ON Car.regNo = TravelCost.carRegNo
GROUP BY NAME
HAVING Sum(distanceInKm) > 100
You need to JOIN all the tables and find sum of litres like this:
select
e.*,
sum(distanceInKm/c.kmPerLitre) litres
from employee e
inner join travelcost t
on e.id = t.employeeId
inner join car c
on t.carRegNo = c.regNo
group by e.id, e.name
having sum(t.distanceInKm) > 100;
Also, you need to group by id instead of just names as the other answers suggest. There can be multiple employees with same name.
Also, use explicit JOIN syntax instead of older comma based syntax. It's modern and clearer.
-- **How fool am I! How arrogant am I! I just thought `sum(tc.distanceInKm/c.kmPerLitre)`
-- may have a problem, since a employee may have multiple cars,and car's kmPerLitre is differenct.
-- However there is no problem, it's simple and right!
-- The following is what I wrote, what a bloated statement it is! **
-- calcute the total number of litres used by the employee on all journeys
select e.name, sum(Cost_in_Litres) as "Cost in Litres"
from (
select t.employeeID
-- calcute the litres used by the employee on all journeys group by carRegNo
, sum(t.distanceInKm)/avg(c.kmPerLitre) as Cost_in_Litres
from TravelCost t
inner join Car c
on c.regNo = t.carRegNo
where t.employeeID in
( -- find the employees who has a total distance from all journeys of more than 100
select employeeID
from TravelCost
group by employeeID
having sum(distanceInKm)> 100
)
group by t.carRegNo, t.employeeID
) a
inner join Employee e
on e.id = a.employeeID
group by e.id,e.name;

SQL Server query issue - ambiguous column

I have four tables :
Applicant (aid, aname)
entrance_test (Etid, etname)
etest_centre (etcid, location)
etest_details (aid, etid, etcid, etest_dt)
I want to select the number of applicants who have appeared for each test, test center wise.
This is my current query:
select
location, etname, count(Aid) as number of applicants
from
applicant as a
inner join
etest_details as d on a.aid = d.aid
inner join
Entrance_Test as t on t.Etid = d.Etid
inner join
Etest_Centre as c on c.Etcid = d.Etcid
group by
Location, Etname
This is the error I am getting :
Ambiguous column name 'Aid'
You have the column aid in multiple tables, and it doesn't know which to pick from. You should specify which table it is from using the aliases you defined.
In this case, since a.Aid is the same as d.Aid (due to the JOIN), I'm using the a alias, but do keep in mind if location and etname also appear in multiple tables, you need to specify which table it should pick from.
Select c.location, t.etname, Count(a.Aid)
From Applicant As a
Inner Join etest_details As d On a.aid = d.aid
Inner Join Entrance_Test As t On t.Etid = d.Etid
Inner Join Etest_Centre As c On c.Etcid = d.Etcid
Group By c.Location, t.Etname
As a rule of thumb, when you have multiple sources in one query, you should always be explicit about which table it should come from. Even if you're sure it only exists in one of them, it's a good habit to get into to avoid issues like this in the future.
You need to mention the alias in the COUNT clause. Since you are using aliases, it would be better if you use them in the SELECT and GROUP BY sections as well. In this case, it should be :
SELECT a.location,
a.etname,
COUNT(d.Aid)
FROM applicant AS a
INNER JOIN etest_details AS d ON a.aid = d.aid
INNER JOIN Entrance_Test AS t ON t.Etid = d.Etid
INNER JOIN Etest_Centre AS c ON c.Etcid = d.Etcid
GROUP BY a.Location,
a.Etname

SQL Solving with Subqueries?

I'm a beginner trying to learn and practice SQL with tables based on this schema:
EMPLOYEE - ID, Name
ASSIGNMENT - ID, Country, Start, End
The primary keys are Employee.ID and all four columns shown for ASSIGNMENT; and ASSIGNMENT.ID is a reference to EMPLOYEE.ID. The domain of start and end is in years.
Problem: I'm trying to write a query that will display all the employees (by name) where they were assigned to an assignment in the USA directly after they had completed an assignment in Canada.
This is my current attempt, which fails to compute. I believe I am heading in the correct direction but there are syntactical mistakes.
SELECT
E.Name
FROM
EMPLOYEE E
INNER JOIN
ASSIGNMENT A ON E.ID = A.ID
WHERE
(SELECT End FROM ASSIGNMENT
WHERE Country = 'Canada') = (SELECT Start FROM ASSIGNMENT
WHERE COUNTRY = 'USA')
GROUP BY
E.Name;
Any critique to benefit my understanding of my misconceptions are welcome. My errors are coming from the combination of the subqueries in the WHERE clause
At most one record can be returned by this subquery.
Perhaps someone can show me another way to compute this?
This query is being tested in MS Access since I found it easy to build a database and relationships quickly.
Instead of subqueries use another join and add constraints to the join conditions:
SELECT
E.Name
FROM
EMPLOYEE E
INNER JOIN
ASSIGNMENT A ON ( E.ID = A.ID
AND A.Country = 'Canada' )
INNER JOIN
ASSIGNMENT B ON ( E.ID = B.ID
AND B.Country = 'USA'
AND B.Start = A.End )
GROUP BY
E.Name;
Update
The OP reported an error from MS Access complaining about the composite join condition for the above version. However, you may safely move inner join conditions to the where clause. The interim resultsets will grow, though, since the product of the tables is produced first with less constraints and filtered thereafter (A good query optimizer might avoid the unnecessary generation of records, but I do not know about the capabilities of MS Access in this regard).
SELECT
E.Name
FROM
EMPLOYEE E
INNER JOIN
ASSIGNMENT A ON ( E.ID = A.ID )
INNER JOIN
ASSIGNMENT B ON ( E.ID = B.ID )
WHERE
A.Country = 'Canada'
AND B.Country = 'USA'
AND B.Start = A.End
GROUP BY
E.Name;

SQL server SELECT with join performance issue

Sorry about the saga here but am trying to explain everything.
We have 2 databases that I would like to join some tables in.
1 database holds sales data from various different stores/sites. This database is quite large (over 3mill rows currently) This table is ItemSales
The other holds application data from an in house web app. These tables are Departments and GroupItems
I would like to create a query that joins 2 tables from the app database with the sales database table. This is so we can group some items together for a date range and see the amount sold for example.
My first attempt was (DealId being the variable that it is grouped on in the App):
SELECT d.Id, d.ItemNo, d.UnitValue, d.NoGST, d.ItemStartDate, d.ItemEndDate,
(SELECT SUM(ItemQty) AS Expr1
FROM Sales.dbo.ItemSales AS s
WHERE (Store = d.SiteId) AND (ItemNo = d.ItemNo) AND (ItemSaleDate >= d.ItemStartDate) AND (ItemSaleDate <= d.ItemEndDate)) AS ItemsSold, Sales.dbo.ItemSales.ItemDesc, Departments.Description
FROM Departments INNER JOIN
Sales.dbo.ItemSales ON Departments.Id = Sales.dbo.ItemSales.ItemDept RIGHT OUTER JOIN
GroupItems AS d ON Sales.dbo.ItemSales.ItemNo = d.ItemNo
WHERE (d.DealId = 11)
GROUP BY d.Id, d.ItemNo, d.UnitValue, d.NoGST, d.ItemStartDate, d.ItemEndDate, ItemDesc, Departments.Description, d.SiteId
ORDER BY d.Id
This does exactly what I want which is:
-Give me all the details from the GroupItems table (UnitValue, ItemStartDate, ItemEndDate etc)
-Gives me the SUM() on the ItemQty column for the amount sold (plus the description etc)
-Returns NULL for something with no sales for the period
It is VERY slow though. To the point that if the GroupItems table has more than about 7 items in it, it times out.
Second attempt has been:
SELECT d.Id, d.ItemNo, d.UnitValue, d.NoGST, d.ItemStartDate, d.ItemEndDate, SUM(ItemQty) AS ItemsSold, Sales.dbo.ItemSales.ItemDesc, Departments.Description
FROM Departments INNER JOIN
Sales.dbo.ItemSales ON Departments.Id = Sales.dbo.ItemSales.ItemDept RIGHT OUTER JOIN
GroupItems AS d ON Sales.dbo.ItemSales.ItemNo = d.ItemNo
WHERE (Store = d.SiteId) AND (d.DealId = 11) AND (Sales.dbo.ItemSales.ItemSaleDate >= d.ItemStartDate) AND (Sales.dbo.ItemSales.ItemSaleDate <= d.ItemEndDate)
GROUP BY d.Id, d.ItemNo, d.UnitValue, d.NoGST, d.ItemStartDate, d.ItemEndDate, ItemDesc, Departments.Description
ORDER BY d.Id
This is very quick and does not time out but does not return the NULLs for no sales items in the ItemSales table. This is a problem as we need to see nothing or 0 for a no sales item otherwise people will think we forgot to check that item.
Can someone help me come up with a query please that returns everything from the GroupItems table, shows the SUM() of items sold and doesn't time out? I have also tried a SELECT x WHERE EXISTS (Subquery) but this also didn't return the NULLs for me but I may have had that one wrong.
If you want everything from GroupItems regardless of the sales, use it as the base of the query and then use left outer joins from there. Something along these lines:
SELECT GroupItems.Id, GroupItems.ItemNo, GroupItems.UnitValue, GroupItems.NoGST,
GroupItems.ItemStartDate, GroupItems.ItemEndDate,
Sales.ItemDesc,
SUM(ItemQty) AS SumOfSales,
Departments.Description
FROM GroupItems
LEFT OUTER JOIN #tempSales AS Sales ON
Sales.ItemNo = GroupItems.ItemNo
AND Sales.Store = GroupItems.SiteId
AND Sales.ItemSaleDate >= GroupItems.ItemStartDate
AND Sales.ItemSaleDate <= GroupItems.ItemEndDate
LEFT OUTER JOIN Departments ON Departments.Id = Sales.ItemDept
WHERE GroupItems.DealId = 11
GROUP BY GroupItems.Id, GroupItems.ItemNo, GroupItems.UnitValue, GroupItems.NoGST,
GroupItems.ItemStartDate, GroupItems.ItemEndDate,
Sales.ItemDesc,
SUM(ItemQty) AS SumOfSales,
Departments.Description
ORDER BY GroupItems.Id
Does changing the INNER JOIN to Sales.dbo.ItemSales into a LEFT OUTER JOIN to Sales.dbo.ItemSales and changing the RIGHT OUTER JOIN to GroupItems into an INNER JOIN to GroupItems fix your issue?

How do I compose this sql query in Oracle?

I have got an sql query that pulls out all sorts of information. Part of it is the following
select gsm.mobile_no, emp.employee_id, d.department_id
from data gsm, employees emp, department d
where gsm.code = e.code
and d.id = e.id
Now there's a column called roaming in another table called "call" . Here's the problem. There's information from the call table for only some of the mobile numbers so when I join gsm.code = call.id like below
select gsm.mobile_no, emp.employee_id, d.department_id, roaming.name
from data gsm, employees emp, department d, call roaming
where gsm.code = e.code
and d.id = e.i
and roaming.id = gsm.code
Then I lose information about employees and departments since only the records that satisfy the condition roaming.id = gsm.code are retrieved so I lose info about departments, employees and all other mobile numbers. I want to retrieve all records from all tables including roaming.id for the mobile numbers where applicable and if there's no data available for some of the mobile numbers then display null but I want all of the records displayed.
How could I do that?
Your time has come to move to the world of modern join syntax. Put your join conditions in the on clause and remember the simple rule: Never use a comma in the from clause.
What you need is a left outer join. You can't really do that in the where clause. Well, you can in Oracle, but it is not pretty and not as good as a real left outer join.
select gsm.mobile_no, emp.employee_id, d.department_id, roaming.name
from employes left outer join
data gsm
on gsm.code = e.code left join
department d
on d.id = e.i left outer join
call roaming
on roaming.id = gsm.code;
Although you can mix inner and outer joins, you want to keep all employees. So start with that table and make all the joins left outer join.