2014-02-12 274 views
0

嗨我想創建一個嵌套的SQL Statment,但無法讓它工作。我想要的表述是從purchaseorderitems表中引入最早的到期日。數量超過> 0。不過,我無法得到這個工作。我想回到這個領域作爲EFFECTIVEDATEMS SQL2008嵌套選擇語句

SELECT 'Past Due Purchase Orders' AS TransactionType , 
     PurchaseOrders.PurchaseOrderId AS TransactionId , 
     (SELECT TOP (1) 
        DueDate 
      FROM  PurchaseOrderItems 
      WHERE  (QuantityOutstanding > 0) 
      ORDER BY DueDate 
     ) AS EffectiveDate , 
     Suppliers.SupplierId , 
     Suppliers.Branch , 
     Suppliers.SupplierName , 
     Currencies.CurrencyDisplay , 
     CASE WHEN PurchaseOrderItems.ItemType = 'P' 
      THEN Products.ProductId 
      WHEN PurchaseOrderItems.ItemType = 'S' THEN Sundries.SundryId 
      WHEN PurchaseOrderItems.ItemType = 'F' 
      THEN FreightMethods.FreightMethodId 
      ELSE PurchaseOrderItems.FreeTextItem 
     END AS Item , 
     PurchaseOrderItems.ItemDescription , 
     PurchaseOrderItems.Quantity , 
     UnitsOfMeasureFactors.UnitOfMeasureFactorPrintText , 
     PurchaseOrderItems.CurItemValue AS TotalValue , 
     'PurchaseOrders' AS ScreenName , 
     CustomValues.CustomFieldValueBit AS AcknowledgementRx , 
     PurchaseOrders.DeliveryDateConfirmed , 
     CASE WHEN PurchaseOrders.DeliveryDateConfirmed = 0 
        AND CustomValues.CustomFieldValueBit = 0 
      THEN 'Not Ack ¦ Not Conf' 
      WHEN PurchaseOrders.DeliveryDateConfirmed = 1 
        AND CustomValues.CustomFieldValueBit = 0 
      THEN 'Not Ack ¦ Conf' 
      WHEN PurchaseOrders.DeliveryDateConfirmed = 0 
        AND CustomValues.CustomFieldValueBit = 1 
      THEN 'Ack ¦ Not Conf' 
      WHEN PurchaseOrders.DeliveryDateConfirmed = 1 
        AND CustomValues.CustomFieldValueBit = 1 
      THEN 'Ack ¦ Conf' 
     END AS Acktxt 
FROM PurchaseOrders 
     LEFT OUTER JOIN PurchaseOrderItems ON PurchaseOrders.PurchaseOrder = PurchaseOrderItems.PurchaseOrder 
     INNER JOIN Suppliers ON PurchaseOrders.Supplier = Suppliers.Supplier 
     INNER JOIN Currencies ON PurchaseOrders.Currency = Currencies.Currency 
     INNER JOIN CustomValues ON PurchaseOrders.PurchaseOrder = CustomValues.RecordNumber 
     INNER JOIN CustomFields ON CustomValues.CustomField = CustomFields.CustomField 
            AND CustomFields.CustomFieldId = 'Acknowledgement' 
     LEFT OUTER JOIN Products ON PurchaseOrderItems.Product = Products.Product 
     LEFT OUTER JOIN Sundries ON PurchaseOrderItems.Sundry = Sundries.Sundry 
     LEFT OUTER JOIN FreightMethods ON PurchaseOrderItems.FreightMethod = FreightMethods.FreightMethod 
     LEFT OUTER JOIN UnitsOfMeasureFactors ON UnitsOfMeasureFactors.UnitOfMeasureFactor = PurchaseOrderItems.UnitOfMeasureFactor 
WHERE (PurchaseOrders.SystemType = 'F') 
     AND (PurchaseOrderItems.DueDate < GETDATE()) 
     AND (PurchaseOrderItems.QuantityOutstanding > 0) 
ORDER BY TransactionType , 
     Currencies.CurrencyDisplay , 
     EffectiveDate , 
     TransactionId 
+0

的就是你得到的錯誤信息? –

+0

嗨它不是一個錯誤,查詢爲每個返回的記錄帶來相同的日期。我希望字段effectivedate是來自purchaseorderitems表的最快日期,對於每個purchaseorder.purchaseorderID –

+0

然後,您需要您的子查詢引用外部語句表,可能是這樣的:'(SELECT TOP(1) DueDate FROM PurchaseOrderItems WHERE PurchaseOrderItems.PurchaseOrder = PurchaseOrders.PurchaseOrder AND QuantityOutstanding> 0 ORDER BY DueDate )AS EffectiveDate' –

回答

0

你需要你的子查詢參考表從外部語句,就像這樣:

(SELECT TOP 1 
    DueDate 
    FROM PurchaseOrderItems 
    WHERE PurchaseOrderItems.PurchaseOrder = PurchaseOrders.PurchaseOrder 
    AND QuantityOutstanding > 0 
    ORDER BY DueDate 
) AS EffectiveDate