在SQL查询中过滤掉NULL值

时间:2018-06-23 22:27:34

标签: sql sql-server

我试图弄清楚如何摆脱这些NULL值。我写了一个查询,该查询应该只返回1994年发货的订单,但我得到几个NULL。我认为这与我如何加入餐桌有关,但我不确定。

我使用以下查询解决了此问题。我实际上需要做的是在订购日期前增加10天,以获得ShippedDate的结果。

SELECT    
    Orders.OrderID,
    Products.ProductID,
    Customers.CompanyName,
    (OrderDetails.Quantity * Products.UnitPrice) AS OrderCost,
    FORMAT(DATEADD(DAY, 10,Orders.OrderDate), 'MMM dd yyyy') AS ShippedDate
FROM            
    Customers 
INNER JOIN
    Orders ON Customers.CustomerID = Orders.CustomerID 
INNER JOIN
    OrderDetails ON Orders.OrderID = OrderDetails.OrderID 
INNER JOIN
    Products ON OrderDetails.ProductID = Products.ProductID
WHERE 
    YEAR (OrderDate) >= ('1994') 
    AND (OrderDetails.Quantity * Products.UnitPrice) >= '2500'
ORDER BY 
    CompanyName

enter image description here

1 个答案:

答案 0 :(得分:0)

您按一列进行过滤,然后显示另一列。你不应该显示相同的吗?也许解决方案是这样的:

SELECT Orders.OrderID,
       Products.ProductID,
       Customers.CompanyName,
       (OrderDetails.Quantity * Products.UnitPrice) AS OrderCost,
       FORMAT(Orders.ShippedDate, 'MMM dd yyyy') AS ShippedDate
  FROM Customers 
  INNER JOIN Orders ON Customers.CustomerID = Orders.CustomerID
  INNER JOIN OrderDetails ON Orders.OrderID = OrderDetails.OrderID
  INNER JOIN Products ON OrderDetails.ProductID = Products.ProductID
  WHERE YEAR (Orders.ShippedDate) >= ('1994') -- CHANGED COLUMN HERE!
  ORDER BY CompanyName;