database view creation issue - sql

I have many users with unique userID in table-1 with their specifications, I have other table-2 that Supervisors choose hours that users work. (I connect userID of table-2 to table-1 and it's ok)
supervisors are many so maybe their is many row with different hour for same useriD. So I create view with
select w_userid,sum(w_hour)as total from Workk group by w_userid
for show me one userID with sum of his hours. But I cant add it in database layer.

You should join two tables using 'inner join' and you have to use a 'where' condition. In where condition use 'AND' to check its the same person using the employee id and then sum the total working hours.

Related

Counting Unique IDs In a LEFT/RIGHT JOIN Query in Access

I am working on a database to track staff productivity. Two of the ways we do that is by monitoring the number of orders they fulfil and by tracking their error rate.
Each order they finish is recorded in a table. In one day they can complete many orders.
It is also possible for a single order to have multiple errors.
I am trying to create a query that provides a summary of their results. This query should have one column with "TotalOrders" and another with "TotalErrors".
I connect the two tables with a LEFT/RIGHT join since not all orders will have errors.
The problem comes when I want to total the number of orders. If someone made multiple mistakes on an order, that order gets counted multiple times; once for each error.
I want to modify my query so that when counting the number of orders it only counts records with distinct OrderID's; yet, in the same query, also count the total errors without losing any.
Is this possible?
Here is my SQL
SELECT Count(tblTickets.TicketID) AS TotalOrders,
Count(tblErrors.ErrorID) AS TotalErrors
FROM tblTickets
LEFT JOIN tblErrors ON tblTickets.TicketID = tblErrors.TicketID;
I have played around with SELECT DISTINCT and UNION but am struggling with the correct syntax in Access. Also, a lot of the examples I have seen are trying to total a single field rather than two fields in different ways.
To be clear when totalling the OrderCount field I want to only count records with DISTINCT TicketID's. When totalling the ErrorCount field I want to count ALL errors.
Ticket = Order.
Query Result: Order Count Too High
Ticket/Order Table: Total of 14 records
Error Table: You can see two errors for the same order on 8th
do a query that counts orders by staff from Orders table and a query that counts errors by staff from Errors table then join those two queries to Staff table (queries can be nested for one long SQL statement)
correlated subqueries
SELECT Staff.*,
(SELECT Count(*) FROM Orders WHERE StaffID = Staff.ID) AS CntOrders,
(SELECT Count(*) FROM Errors WHERE StaffID = Staff.ID) AS CntErrors
FROM Staff;
use DCount() domain aggregate function
Option 1 is probably the most efficient and option 3 the least.

duplicated rows in select query

I am trying to run a query to get all the customers from my database. These are my tables in a diagram :
when running the query by joining the table Companies_Customers and the Customers table based on the customerId in both tables(doesn't show in the join table in the pic), I get duplicate rows, which is not the desired outcome.
This is normal from a database standpoint since a Customer can be related to different companies (Companies can share single customer).
My question is how do I get rid of the duplication via SQL.
There can be 2 approaches to your problem.
Either only select data from Customers table:
SELECT * FROM Customers
Or select from both tables joined together, but without CompanyName and with GROUP BY CompanyCustomerId - although I highly suggest the first approach.

New report from two tables with reportviewer doesnt have true result

I create a new view in SQL Server 2008 from two tables that have relation on a field. I want to create a report and do grouping on that common field.
For example:
table1: student(ID,first-name,last-name,phone,address,...)
table2: courses(ID,fk_ID,Course,....)
Now I want to have report that shows all data from both tables with grouping on ID from student table, that must show courses information separated for every student.
my query is:
SELECT TOP (100) PERCENT
dbo.tbl_student.ID,
dbo.tbl_student.firstname, dbo.tbl_student.lastname,
dbo.tbl_courses.Coursename,
dbo.tbl_Courses.CourseDate, dbo.tbl_courses.coursetype,
FROM
dbo.tbl_student LEFT OUTER JOIN
dbo.tbl_courses ON dbo.tbl_student.ID = dbo.tbl_courses.fk_id
ORDER BY
dbo.tbl_student.firstname DESC
But when I create a new report from this view, it shows just one record for every group. I spent 2 hours to solve the problem but I did not succeed.
please help me to create report from two or more tables.
Now it shows one record duplicates for several times for every group
Have you tried a query like this:
SELECT s.[ID], s.[first-name], s.[last-name], s.[phone], ...
c.[ID], c.[Course], ...
FROM student s
LEFT OUTER JOIN
courses c ON s.[ID] = c.[fk_ID]

Database table showing multiple results per field

Attempting to form a query using 2 tables involving member of a cycling club and their respective place in a race with Microsoft Access. When I attempt to run the following SQL code I get a query table that displays every RaceID and Member, but doesn't link each member to 1 place per race.
SELECT RaceID, LastName, FirstName, Place
FROM Members, RaceResults;
What I do wind up with is a listing for every member of the club in all places (1-10) in every race. I have attempted to do both a count function per raceID and a Join function to combine the memberID's between both tables. Neither seem to either work, or have the same result as my current table. I would appreciate any suggestions on what I am missing in my SQL Query to properly display my table.
When you join the result of 2 tables, you need to tell the database how they are linked, otherwise you'll get a cartesian product of both tables,.
Example:
SELECT t.RaceID, t.LastName, t.FirstName, tt.Place
FROM Members t join RaceResults tt on (t.RaceId = tt.RaceId);

Ms-Access: counting from 2 tables

I have two tables in a Database
and
I need to retrieve the number of staff per manager in the following format
I've been trying to adapt an answer to another question
SELECT bankNo AS "Bank Number",
COUNT (*) AS "Total Branches"
FROM BankBranch
GROUP BY bankNo
As
SELECT COUNT (*) AS StaffCount ,
Employee.Name AS Name
FROM Employee, Stafflink
GROUP BY Name
As I look at the Group BY I'm thinking I should be grouping by The ManID in the Stafflink Table.
My output with this query looks like this
So it is counting correctly but as you can see it's far off the output I need to get.
Any advice would be appreciated.
You need to join the Employee and Stafflink tables. It appears that your FROM clause should look like this:
FROM Employee INNER JOIN StaffLink ON Employee.ID = StaffLink.ManID
You have to join the Eployee table twice to get the summary of employees under manager
select count(*) as StaffCount,Manager.Name
from Employee join Stafflink on employee.Id = StaffLink.EmpId
join Employee as Manager on StaffLink.ManId = Manager.Id
Group by Manager.Name
The answers that advise you on how to join are correct, assuming that you want to learn how to use SQL in MS Access. But there is a way to accomplish the same thing using the ACCESS GUI for designing queries, and this involves a shorter learning curve than learning SQL.
The key to using the GUI when more than one table is involved is to realize that you have to define the relationships between tables in the relationship manager. Once you do that, designing the query you are after is a piece of cake, just point and click.
The tricky thing in your case is that there are two relationships between the two tables. One relationship links EmpId to ID and the other links ManId to ID.
If, however, you want to learn SQL, then this shortcut will be a digression.
If you don't specify a join between the tables, a so called Cartesian product will be built, i.e., each record from one table will be paired with every record from the other table. If you have 7 records in one table and 10 in the other you will get 70 pairs (i.e. rows) before grouping. This explains why you are getting a count of 7 per manager name.
Besides joining the tables, I would suggest you to group on the manager id instead of the manager name. The manager id is known to be unique per manager, but not the name. This then requires you to either group on the name in addition, because the name is in the select list or to apply an aggregate function on the name. Each additional grouping slows down the query; therefore I prefer the aggregate function.
SELECT
COUNT(*) AS StaffCount,
FIRST(Manager.Name) AS ManagerName
FROM
Stafflink
INNER JOIN Employee AS Manager
ON StaffLink.ManId = Manager.Id
GROUP BY
StaffLink.ManId
I don't know if it makes a performance difference, but I prefer to group on StaffLink.ManId than on Employee.Id, since StaffLink is the main table here and Employee is just used as lookup table in this query.