如何在sql中加入两个查询?

时间:2015-01-08 14:29:52

标签: sql sql-server jointable

我想加入这两个查询。

我想要列FulliteNametextBeginningBalanceQuantityReceivedDOSLossRequestedQuantity

我尝试了以下查询。但只有第一部分被执行。

SELECT CI.ItemID,
    FullItemName,
    iub.UnitOfIssueID,
    us.TEXT
FROM Commodity.ItemRRFGroup CI
INNER JOIN dbo.vwGetAllItems y
    ON y.ID = ItemID
INNER JOIN ItemUnitBase iub
    ON y.ID = iub.ID
INNER JOIN UnitOfIssue us
    ON iub.UnitOfIssueID = us.ID
LEFT JOIN (
    SELECT RD.RequestID,
        y.RRGroupID,
        y.PeriodID,
        ItemID,
        CONCAT (
            Convert(DATE, StartDate),
            ' to ',
            Convert(DATE, EndDate)
            ) Period,
        BeginningBalance,
        QuantityReceived,
        DOS,
        Loss,
        RequestedQuantity
    FROM RRF.RequestDetail RD
    INNER JOIN RRF.Request y
        ON y.RequestID = RD.RequestID
    INNER JOIN RRF.Period x
        ON x.PeriodID = y.PeriodID
    INNER JOIN OrderDetail o
        ON o.OrderID = y.orderID
    ) AS x
    ON CI.ItemID = x.ItemID

1 个答案:

答案 0 :(得分:3)

也许你改变了这个:

Select 
   CI.ItemID, 
   FullItemName,
   iub.UnitOfIssueID,
   us.text
   .....

对此:

Select 
   CI.ItemID, 
   FullItemName,
   iub.UnitOfIssueID ,
   us.text,
   x.BeginningBalance,
   x.QuantityReceived,
   x.DOS,
   x.Loss, 
   x.RequestedQuantity
   ....

<强>更新

如果您不使用子查询的别名x。然后你不会从x获取列。