Creating three columns in SQL using the following information - mysql

I am trying to create a statement that combines the following into three columns, BusinessEntityID, AveQuota and PctComm. I am having trouble joining, I keep getting a syntax error near join. Please help
SELECT BusinessEntityID, AVG (SalesQuota) as AveQuota
from [sales].[SalesPersonQuotaHistory]
group by BusinessEntityID
join
select (commissionpct * salesytd) as PctComm from sales.SalesPerson
The orginal question was to find each sales person's average Quote (from QuotaHistory, using aggregation) as a column called AvgQuota and join it with a column that calucalates the same sales person's commissions earnings YTD (from SalesPerson, uses two colums from a calculation).

You are missing the information on which column to join those tables in the the 'join' syntax. Please refer to join syntax here: https://www.w3schools.com/mysql/mysql_join.asp#:~:text=MySQL%20Joining%20Tables,a%20related%20column%20between%20them.&text=Notice%20that%20the%20%22CustomerID%22%20column,is%20the%20%22CustomerID%22%20column.

Related

How can I fix this syntax error involving the FROM clause?

I am working on a simple problem set, and I cannot seem to find the issue that is generating this same error: "Syntax Error in FROM Clause".
The question involves the use of various databases in this instant to find "Which employee has sold the most product?"
Here is my code
SELECT (Employees.FirstName + Employees.LastName) as Employee, SUM(Orders.Quantity)
FROM Employees, Orders
JOIN Employees ON Orders.EmployeeID=Employees.EmployeeID
JOIN OrderDetails ON Orders.OrderID=OrderDetails.OrderID
GROUP BY Employee
ORDER BY max(SUM(Quantity)) DESC;
If I am misinterpreting the use of some syntax, please let me know. I am still learning.
Thanks for your help!
When you're using ANSI JOIN you don't list all the tables in the FROM clause. Just list the first table, and the other tables are in JOIN.
You also can't nest aggregate functions as MAX(SUM(Quantity)). If you want to find the employee who sold the most, order by quantity, and use TOP 1 to get the first row.
There's no need to join with OrderDetails, since you're not using anything from that table.
The query should be:
SELECT TOP 1 (Employees.FirstName + Employees.LastName) as Employee, SUM(Orders.Quantity) AS Quantity
FROM Employees
JOIN Orders ON Orders.EmployeeID=Employees.EmployeeID
GROUP BY Employee
ORDER BY Quantity DESC;
Note that if there's a tie for the most sold, this will just show one of them. Getting all of them is more complex, because you need a second query to get that maximum. See sql HAVING max(count()) return zero rows

I cannot get my joins to work on a some tables, but they will work on others. Trying to select columns from 2 tables

I am new to mySQL so sorry for the basic question,
When I try to join my rental table and tp_rental table via the 'Rental_ID' column I get errors saying Error code: 1052 Column 'Rental_ID' in field list is ambiguous.
i can join rental and member tables no problem.
i am trying to print out a transaction report when a rental is completed (game is returned) that it will show rental ID, memberID, Due Date and Date Returned.
I was using the below code but getting errors:
select Rental_ID,member_ID,completed,Date_Due,Date_Returned
from gamestoredb.rental
inner join gamestoredb.tp_rental
on rental.Rental_ID=tp_rental.Rental_ID
Rental_ID is a column in each of the two tables you are joining so the server does not know which one you want rental.Rental_ID or tp_rental.Rental_ID even though in this particular they both would have the same value. Make which one you want explicit, for example:
select tp_rental.Rental_ID,member_ID,completed,Date_Due,Date_Returned
from gamestoredb.rental
inner join gamestoredb.tp_rental
on rental.Rental_ID=tp_rental.Rental_ID

Invalid Column Name with INNER JOIN and Multiple Tables

Im currently writing a stored procedure in SQL to print results from multiple tables to find the top ten products purchased, but I am getting the syntax error
"Invalid column name 'ProductID'".
This appears on the 2nd INNER JOIN statement at sod.ProductID
My code below
CREATE PROCEDURE usp_top10ProfitableProducts
AS
BEGIN
SELECT TOP 10 sp.StoreProductID, sup.ProductName, sum(sod.Quantity) AS quantitysold, (sum(sod.Quantity) * sum(sod.unitPrice)) - (sum(sod.Quantity) * sum(sp.costPrice)) AS Profit
FROM SalesOrderDetails sod
INNER JOIN StoreProduct sp ON sp.StoreProductID = sod.StoreProductID
INNER JOIN SupplierProduct sup ON sup.ProductID = sod.ProductID
WHERE Quantity > 0
END
Thanks in advance.
EDIT** Below is also my Entity Relationship diagram. 'ProductID' lives in 'SupplierProduct'
Probably the field does not exist in one of the tables. Another point: group by was missing.
GROUP BY sp.StoreProductID, sup.ProductName
To do a join between two tables the joining column (in above case ProductID) should be available in both tables, otherwise sql will not be able to do the join.
According to the table structure SalesOrderDetails doesn't have a column ProductID which is why it's giving the error, you need to the join on cloumns that exist in the respective tables

Analysing multiple rows to determine one status

In one of my tables, some customers have multiple lines - this could be due to re-visits from technicians etc. What I want to do is for each customer ID, analyse whether a re-vist has taken place and place a marker against their name.
I have tried to combine an if/in statement that analyses the max/min visit dates for each customert ID. So if the max>min its classed as a "re-visit", however, i keep getting a syntax error.
Can someone help?
This is a job for two SQL queries:
1st query:
SELECT customerID, count(customerID) as visitCount
FROM tableOfInterest
GROUP BY customerID
2nd query uses first query:
UPDATE customerManifest INNER JOIN queryAbove ON queryAbove.customerID = customerManifest.customerID
SET customerManifest.multipleVisitIndicatorField to queryAbove.visitCount

inner join with group by function

I am trying to get results matching a variable using a LIKE operator combined with an INNER JOIN and a GROUPING modifier.
The problem I am having with this query:
-the join will not work with the modifier as it is being grouped by the same column that is being used to join the two tables. I get the error '#1052 - Column 'Agency' in field list is ambiguous'
SELECT Agency,Acronym,last,sum(last),current,sum(current),source,url
FROM `budget_table` INNER join budget_table2 on
budget_table.agency=budget_table2.agency WHERE (Agency) LIKE('%$agency%') GROUP BY Agency
I have looked through other answers but am not able to apply what is posted to what I am doing. Assistance would be appreciated. Is there something wrong with my JOIN?
I am trying to select only agencies common to both tables and then match data against the variable from within those results.
If you have the same column name in two tables, you have to reference the table do you want to get the column value. Write the table name before the column name in your select, where and group.
Example:
SELECT budget_table.Agency /* ...your code... */
WHERE budget_table.Agency LIKE ('%$agency%')
GROUP BY budget_table.Agency