CASE语句和JOIN 3表中的SQL是空的吗?

时间:2016-01-19 07:37:47

标签: sql-server

使用表格:
[AdventureWorks2012].[Production].[ProductInventory]
[Production].[Product]
[Production].[Location]

列: ProductID
Name
ProductLine(如果为null值,则显示N / A,如果为M,则显示' Mountain',如果T显示' Touring',则所有其他-s,显示'别人'),
SellStartDate
LocationID
Availability
shelf
quantity

要求:2006年获得sellstartdate的产品,以及总可用性低于50的位置。

请帮助纠正我的CASE,(CASE中为IS NULL)和3个表的JOIN: enter image description here

2 个答案:

答案 0 :(得分:0)

SELECT Name, CASE WHEN ProductLine IS NULL THEN 'N/A'
                  WHEN ProductLine='M' THEN 'Mountain'
                  WHEN ProductLine='T' THEN 'Touring'
                  ELSE Others END ProductLine,
        SellStartDate
FROM Production.Product

答案 1 :(得分:0)

如果我没有错,那么你会看下面的事情

SELECT Name, CASE WHEN ProductLine IS NULL THEN 'N/A'
              WHEN ProductLine='M' THEN 'Mountain'
              WHEN ProductLine='T' THEN 'Touring'
              ELSE Others END ProductLine,
              SellStartDate,
              [Location].LocationID,
              Availability AS 'Total Availability'
FROM Production.Product
INNER JOIN Production.ProductInventory ON Production.Product.ProductID=Production.ProductInventory.ProductID
INNER JOIN [Production].[Location] ON Production.Product.LocationID=[Production].[Location].LocationID
WHERE SellStartDate BETWEEN '1/1/2006' AND '31/12/2006'
AND Availability < 50