Advertisement
lukifrancuz

BD3_K1_Z3_g1

Nov 17th, 2022 (edited)
596
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 0.57 KB | None | 0 0
  1. --3
  2.  
  3. create or alter function OrderedProducts(
  4.     @categoryid INT = 0,
  5.     @year INT = 2000
  6. )
  7. returns int
  8. as
  9. begin
  10.  
  11.     declare @result int = 0
  12.  
  13.     select @result = sum(sod.qty) from Sales.OrderDetails sod
  14.         inner join Production.Products pp on sod.productid = pp.productid
  15.         inner join Sales.Orders so on sod.orderid = so.orderid
  16.         where year(so.orderdate) = @year and pp.categoryid = @categoryid
  17.  
  18.      return @result
  19. end
  20. go
  21.  
  22. select c.categoryname, dbo.OrderedProducts(c.categoryid, 2006) as 'Orders in 2006' from Production.Categories c
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement