对于这两个表:
产品(ProductID,ProductName)
销售(SalesOrderID,ProductID,SaleDate)
获取去年销售的产品清单。获取
去年未售出的产品。
答案 0 :(得分:-1)
去年出售
select Product.ProductName from Product inner join Sales on Product.ProductID = Sales.ProductID where year(Sales.SaleDate) = year(getdate())-1
去年没有出售
select Product.ProductName from Product where ProductID not in ( select ProductID in Sales where year(Sales.SaleDate) = year(getdate())-1)
答案 1 :(得分:-1)
以下是SARG
能够提供的优选解决方案:
select distinct p.ProductID, p.ProductName
from Product p
join Sales s on p.ProductID = s.ProductID
where s.SaleDate >= cast (cast(year(getdate()) as varchar(8)) + '0101' as date)
和
select distinct p.ProductID, p.ProductName
from Product p
left join Sales s on p.ProductID = s.ProductID
and s.SaleDate >= cast (cast(year(getdate()) as varchar(8)) + '0101' as date)
where s.ProductID is null