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
Related
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
I am trying to make a table that includes join between 3 tables in the MSSS 2008. There is a fact table, a date table, and a course table. I should join them to make a base table. In date table there is a one parameter that name is Academic Year lookup, and the values in this parameter is like 2000/1, 2001/2. This parameter in the base table should separate to three parameter such as CensusYear, StartYear, and ApplicationYear. Therefore, I need the data table multiple times. I executed a inner join query, and already I have four inner join statement, but I am getting some extra years, and I'm losing some years. I believe, my query should be wrong somewhere.
The attached file is include the design view that created in the MS Access, it'll help to see the tables, and understand what I need to create.
[Design View in Ms Access][1]
SELECT
A.[EventCount],
B.[AcademicYearLookup] AS [CensusYear],
C.[AcademicYearLookup] AS [StartYear],
D.[AcademicYearLookup] AS [ApplicationYear],
B.[CurrentWeekComparisonFlag],
B.[AcademicWeekOfYear],
case
when A.[ApplicationCensusSK] = 1 then 'Same Year'
when A.[ApplicationCensusSK] = 2 then 'Next Year'
when A.[ApplicationCensusSK] = 5 then 'Last Year'
ELSE 'Other'
END as [CensusYearDescription],
B.[CurrentAcademicYear],
A.[StudentCodeBK],
A.[ApplicationSequenceNoBK],
A.[CourseSK],
A.[CourseGroupSK],
A.[CourseMoaSK],
A.[CboSK],
A.[CourseTaughtAbroadSK],
A.[ApplicationStatusSK],
A.[ApplicationFeeStatusSK],
A.[DecisionResponseSK],
A.[NationalityCountrySK],
A.[DomicileCountrySK],
A.[TargetRegionSK],
A.[InternationalSponsorSK] INTO dbo.[BaseTable3yrs]
FROM Student.FactApplicationSnapshot A
INNER JOIN Conformed.DimDate AS B ON A.[CensusDateSK] = B.[DateSK]
INNER JOIN Conformed.DimDate AS C ON A.[AcademicYearStartDateSK] = C.[DateSK]
INNER JOIN Conformed.DimDate AS D ON A.[ApplicationDateSK] = D.[DateSK]
INNER JOIN Student.DimCourse ON A.CourseSK = Student.DimCourse.CourseSK
WHERE (((B.CurrentAcademicYear) In (0,-1))
AND ((A.ApplicationCensusSK) In (1,2,5))
AND ((Student.DimCourse.DepartmentShortName)= 'TEACH ED'));
/* the query to check that the result it's correct or not, and I check it by academic week of year, and I found that I am lossing some data, and I have some extra data, means maybe join is wrong*/
select * from [BaseTable3yrs]
where [StudentCodeBK]= '26002423'
AND [ApplicationSequenceNoBK] = '0101'
order by [AcademicWeekOfYear]
When doing recursive joins like this, it's easy to get duplicate records. You could try gathering the Conformed data separately into a table variable and then joining to it. This would also make your query more readable.
You might also try a SELECT DISTINCT on your main query.
I need help with an SQL query I am trying to execute. I have two MYSQL tables as follows:
Table 1: Orderdetails with column name QtyOrdered
and
Table 2: Titles with column name No_of_copies_in_inventory
I would like to perform a SQL query where I can effectively update the No_of_copies_in_inventory in Titles table and do this
No_of_copies_in_inventory in Titles = No_of_copies_in_inventory - QtyOrdered
Any input/feedback would be greatly appreciated.
Thanks!
Assuming a uniquely indexed field (such as ID), you could do it this way:
UPDATE Orderdetails od
INNER JOIN Titles t ON od.id = t.id
SET t.No_of_copies_in_inventory = (t.No_of_copies_in_inventory - o.QtyOrdered)
WHERE od.id = 'idhere'
However, chances are you will have a variable stored for QtyOrdered and could simply use that rather than joining the tables
I have a single table that contain columns:
UserID, EmployeeID, BadgeType, HiredDate, TermDate
Now I need to find userID that are with (gbro, qunro, 1utny, ybeiot, 4ybey)
The 3 users (gbro, qunro, 1utny) exist so it is listed with respective its column info.
What if ybeiot, 4ybey does not exist AT ALL but still I want them listed in a separate table still but with a message that PRINTS: User that does not exist: ybeiot;4ybey
Help, been finding way how to do this.
I tried JOIN (all the joins) but it does not result to what I wanted.
Did you look at SQL EXISTS keyword?
put all the users to be searched in a temp table or table variable #userstoSearch
select * from #userstoSearch us left join users u
on us.UserID=u.UserID where u.userID is not null
select us.UserID from #userstoSearch us left join users u
on us.UserID=u.UserID where u.userID is null
for xml path('')
You need two selects. The first will list the existing values and the second lists the not existing values. You should merge these results using the union keyword.
In SQL Server, I am trying to get the rows from the two tables where first table is master table and second is child table. like I have a table which contain file extensions with id, and description of extension. second table is having employees who having extension ids from Master table. now i want to get the total master table and if i place where condition for particular employee his extensionid and description.
When I write left outer join it is getting only that particular employee extension details,
my sql statement like this:
select *
from Tbl_File_Extn_M FEM
left outer join Tbl_File_Extn_EmpLink_M FEEM on FEM.ExtId = FEEM.ExtId
where FEEM.EmpID = '004135'
Result is giving only one row, but I need the complete master table and particular employee details can you help me.
how about
select *
from Tbl_File_Extn_M FEM
left outer join Tbl_File_Extn_EmpLink_M FEEM on FEM.ExtId = FEEM.ExtId AND FEEM.EmpID = '004135'