I have two tables. One with a list of shops and their ID's (shop_id)
and one with a list of employees with the ID (shop_id) of the shop they work at.
I have to print out each employee with a certain position form a certain shop.
My query is normally correct but I seem to get an error like tblEmployees.
Normally my query would look something like.
SELECT tblEmployees.Name, tblEmployees.Surname, tblShops.shop_id
FROM tblEmployees, tblShops
GROUP BY tblEmployees.shop_id
HAVING tblEmployees.shop_id = tblShops.shop_id;
Normally I get an error saying something like:
tblEmployees.Name is not part of an aggregate function.
What I want to know is if it would solve my problem if I put every column that gives me this error under the GROUP BY statement. Or is there another way of fixing this error without it affecting the result I need to get from this query.
Drop the GROUP BY and HAVING clauses. You aren't aggregating here. You want to be joining your tables.
SELECT tblEmployees.Name, Surname, tblShops.shop_id
FROM tblEmployees JOIN tblShops
ON tblEmployees.shop_id=tblShops.shop_id
Related
I created a SELECT query, after I execute it, it shows me a new table as I wanted (you can see it in the screenshot).
The problem is that I'm trying to add a new line to the query which supposes to sum() the column "Price" in the table that I got from the query
SELECT SUM(PRICE) AS totalPay
FROM Meals
You can see the tables here, and also the queries that worked at that, didn't.
I want it to be like that:
Like this
Just add the SUM(price) as new field in your first SELECT.
SELECT SUM(Meals.Price) as 'TotalPay', tableN01.waiterID
FROM tableN01 INNER JOIN Meals etc.
I'm new to SQL and recently saw this question, which is described on the [attached picture]
. Any suggestions on how to solve it? Thanks in advance.
In most cases such requirements are better to resolve by starting from the end. Let's try that: "have more than 3 items ... and total price".
select InvoiceId, sum(price)
from InvoiceItem
group by InvoiceId
having count(1) > 3;
This query gives you the overall price for every invoice that has more than 3 items. You might be wondering why there is that funny "having" clause and not the "where" clause.
The database executes first "from" and "where" parts of the query to get the data and only after that using aggregations (sum, count etc.) is possible, so they are to be specified afterwards.
The query above actually returns all the data from requirement, but I assume that whoever gave you this task (that was a teacher, right?) was asking you to retrieve all the data from Invoices table that correspond to the records within the InvoiceItem table that has more than 3 items and so on.
So, for now all you have left is to join the Invoice table with a query from above
select i.id, i.customername, i.issuedate, sum(it.price)
from InvoiceItem it
join Invoice i on it.invoiceid = i.id
group by i.id, i.customername, i.issuedate
having count(1) > 3;
Teacher might ask you why did you use count(1) and not count(*) or count(id) or something. Just tell him/her all these things are equal so you picked just one.
Now it should be working fine presumably. I did not tested it at all as you did not provide the data to test it on and it is late so I am pretty tired. Because of this you might needed to fix some typos or syntax errors I probably made - but hey let's keep in mind you still have to do something on your own with this task.
I have the following dataset:
I am trying to convert the table on the left into the table on the right. I have several duplicates of orders with the same name but different products sold. I would like to combine the rows so it shows just one orderID. I've tried joining the table to itself based on order but I must be doing something wrong. Do you guys have any suggestions? this is probably super easy but I am not proficient with SQL yet. Thank you in advance.
If there is at most one value in each column, you can use group by:
select order, name, max(product1) as product1, max(product2) as product2,
max(product3) as product3
from lefttable
group by order, name;
That said, I suspect that the table on the left is the result of a query on the data. You probably simply need the right aggregation for that query.
Also, if you have more than one value in any column for an order, you can still do this, but the query is a bit more complicated.
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);
I have a query as follows in MS Access
SELECT tblUsers.Forename, tblUsers.Surname,
(SELECT COUNT(ID)
FROM tblGrades
WHERE UserID = tblUsers.UserID
AND (Grade = 'A' OR Grade = 'B' OR Grade = 'C')) AS TotalGrades
FROM tblUsers
I've put this into a report and now when trying to view the report it displays an alert "Multi-level GROUP BY clause is not allowed in subquery"
What I dont get is I dont even have any GROUP BY clauses in the query so why is it returning this error?
From Allen Browne's excellent website of Access tips: Surviving Subqueries
Error: "Multi-level group by not allowed"
You spent half an hour building a query with subquery, and verifying it all works. You create a report based on the query, and immediately it fails. Why?
The problem arises from what Access does behind the scenes in response to the report's Sorting and Grouping or aggregation. If it must aggregate the data for the report, and that's the "multi-level" grouping that is not permitted.
Solutions
In report design, remove everything form the Sorting and Grouping dialog, and do not try to sum anything in the Report Header or Report Footer. (In most cases this is not a practical solution.)
In query design, uncheck the Show box under the subquery. (This solution is practical only if you do not need to show the results of the subquery in the report.)
Create a separate query that handles the subquery. Use this query as a source "table" for the query the report is based on. Moving the subquery to the lower level query sometimes (not always) avoids the problem, even if the second query is as simple as
SELECT * FROM Query1;
Use a domain aggregate function such as DSum() instead of a subquery. While this is fine for small tables, performance will be unusable for large ones.
If nothing else works, create a temporary table to hold the data for the report. You can convert your query into an Append query (Append on Query menu in query design) to populate the temporary table, and then base the report on the temporary table.
IMPORTANT NOTE: I'm reposting the info here because I believe Allen Browne explicitly allows it. From his website:
Permission
You may freely use anything (code, forms, algorithms, ...) from these articles and sample databases for any purpose (personal, educational, commercial, resale, ...). All we ask is that you acknowledge this website in your code, with comments such as:
'Source: http://allenbrowne.com
'Adapted from: http://allenbrowne.com
Try this version:
SELECT users.Forename, users.Surname, grades.TotalGrades
FROM tblUsers AS users
LEFT JOIN (SELECT COUNT(ID) as TotalGrades, UserID FROM tblGrades WHERE (Grade = 'A' OR Grade = 'B' OR Grade = 'C') group by userid) AS grades on grades.UserID = users.UserID
I have not tested it. The query itself should be OK, but I'm not sure whether it works in the report data source.
try this:
SELECT users.Forename, users.Surname, count(grades.id) AS TotalGrades
FROM tblUsers AS users
INNER JOIN tblGrades AS grades ON users.ID=grades.UserID
WHERE grades.Grade in ("A","B","C") group by users.ID;
This is a simple joined table. Basically it means. Select all cases where a user has a grade with "A" or "B" or "C" (which would give you a table like this:
user1 | A
user1 | B
user1 | A
user2 | A
...
And then it groups it by users, counting how many times a grade appeared -> giving you the number of grades in the desired range for each user.