I have tried this question on northwind database: NEED TO KNOW WHERE I DID WRONG. as when I used where statement it worked .
q. List all the products total sales amount from 1997? List the query result by ProductID, ProductName, and Annual Sales Amount for 1997 and sorted it by Annual Sales in Descending order (the annual sales is a calculated column). */
select p.productid, p.productname ,
'$'+ CONVERT(varchar(12), sum(od.unitprice*od.quantity*1-od.discount)) as AnnualSales
from products as p , [Order Details] as od , orders as o
inner join [Order Details]
on p.productid = od.productid
inner join orders
on od.orderid = o.orderID
and o.OrderDate >= '1997/01/01' and o.orderdate <= '1997/12/31'
group by p.productid, p.productname
order by annualsales desc
;
I have got these errors:
Msg 4104, Level 16, State 1, Line 207
The multi-part identifier "p.productid" could not be bound.
Msg 4104, Level 16, State 1, Line 207
The multi-part identifier "od.productid" could not be bound.
Msg 4104, Level 16, State 1, Line 209
The multi-part identifier "od.orderid" could not be bound.
when I tried like this , It worked , please advise where I did wrong:
select p.productid, p.productname , '$'+ CONVERT(varchar(12), sum (od.unitprice*od.quantity*1-od.discount)) as AnnualSales
from products as p , orders as o , [Order Details] as od
where p.productid = od.productID
and o.OrderID = od.OrderID
and o.OrderDate >= '1997/01/01' and o.orderdate <= '1997/12/31'
group by p.productid, p.productname
order by annualsales desc
;