SQL Query每个过滤器检索单个记录

时间:2013-10-08 19:26:04

标签: sql join

我有以下查询:

SELECT min(salesorder.SOM_SalesOrderID) AS salesorder,
    Item.IMA_ItemID,
    Item.IMA_ItemName,
    Customer.CUS_CorpName,
    WK.WKO_WorkOrderID,
    min(WK.WKO_OrigRequiredDate),
    WK.WKO_WorkOrderTypeCode,
    min(WK.WKO_RequiredDate),
    max(WK.WKO_LastWorkDate),
    min(wk.WKO_RequiredQty),
    wk.WKO_MatlIssueDate,
    min(SalesOrderDelivery.SOD_RequiredQty),
    Item.IMA_ItemTypeCode,
    Item.IMA_OnHandQty,
    min(SalesOrderDelivery.SOD_PromiseDate),
    min(WO.woo_operationseqID) AS seqid
FROM SalesOrder
INNER JOIN SalesOrderLine ON SalesOrder.SOM_RecordID = SalesOrderLine.SOI_SOM_RecordID
INNER JOIN SalesOrderDelivery ON SalesOrderLine.SOI_RecordID = SalesOrderDelivery.SOD_SOI_RecordID,
    WO.
INNER JOIN Item ON SalesOrderLine.SOI_IMA_RecordID = Item.IMA_RecordID
INNER JOIN WKO wk ON Item.IMA_ItemID = WK.WKO_ItemID
INNER JOIN Customer ON SalesOrder.SOM_CUS_RecordID = Customer.CUS_RecordID
INNER JOIN woo WO ON WO.WOO_WorkOrderID = WK.WKO_WorkOrderID
WHERE wk.WKO_StatusCode = 'Released'
    AND WO.WOO_StatusCode IS NULL
    AND SalesOrderDelivery.SOD_ShipComplete = 'false'
GROUP BY WK.WKO_WorkOrderID,
    Item.IMA_ItemID,
    Item.IMA_ItemName,
    Customer.CUS_CorpName,
    WK.WKO_WorkOrderTypeCode,
    wk.WKO_MatlIssueDate,
    Item.IMA_ItemTypeCode,
    Item.IMA_OnHandQty

我需要为每个wk.wko_workorderid返回1条记录。有一个字段不包括在内我不知道如何获得。我需要将与min(WO.woo_operationseqID)对应的woo.woo_workcenterid检索为seqid。我不能将它包含在一般查询中,因为表中有多个workcenterids,我只想要特定的一个作为min操作序列记录的一部分。 任何帮助将不胜感激。

0 个答案:

没有答案