2013-07-02 86 views
0

我在sql存儲過程中有一個查詢。我想從其ID的其他查詢獲得記錄,我如何做到這一點。存儲過程中的子查詢

SELECT t.Name ,t.CreatedDate ,t.CreatedBy , t.Amount 
     ,t.Margin ,t.Probability ,t.Id 
FROM (SELECT a = 1) a 
CROSS JOIN 
    (SELECT 
     Name = HirschInternational_MSCRM.dbo.SalesOrderBase.Name 
     ,CreatedDate=HirschInternational_MSCRM.dbo.SalesOrderBase.CreatedOn 
     ,CreatedBy=HirschInternational_MSCRM.dbo.SystemUserBase.FullName 
     ,Amount = totalamount 
     ,Probability=CloseProbability 
     ,Id=SalesOrderId 
     ,Margin=(SELECT (ISNULL(((Sum(Price)-Sum(CurrentCost))/NULLIF(Sum(Price), 0))*100, 0)) 
       FROM HirschInternational_MSCRM.dbo.ProductBase 
       JOIN HirschInternational_MSCRM.dbo.SalesOrderDetailBase 
        ON HirschInternational_MSCRM.dbo.SalesOrderDetailBase.ProductId = HirschInternational_MSCRM.dbo.ProductBase.ProductId 
       JOIN HirschInternational_MSCRM.dbo.SalesOrderBase 
        ON HirschInternational_MSCRM.dbo.SalesOrderBase.SalesOrderId = HirschInternational_MSCRM.dbo.SalesOrderDetailBase.SalesOrderId) 
    FROM HirschInternational_MSCRM.dbo.SalesOrderBase 
    JOIN HirschInternational_MSCRM.dbo.OpportunityBase 
     ON HirschInternational_MSCRM.dbo.SalesOrderBase.Opportunityid = HirschInternational_MSCRM.dbo.OpportunityBase.Opportunityid 
    JOIN HirschInternational_MSCRM.dbo.SystemUserBase 
     ON HirschInternational_MSCRM.dbo.SystemUserBase.SystemUserId = HirschInternational_MSCRM.dbo.SalesOrderBase.CreatedBy 
    WHERE YEAR(HirschInternational_MSCRM.dbo.SalesOrderBase.CreatedOn)=YEAR(GETDATE()) 

我想從我希望輸出像 enter image description here

+0

您的查詢語法錯誤。你可能沒有複製全部內容。編輯你的問題,剪下現有的代碼,並粘貼完整的SELECT語句。 –

回答

0

我想獲取每條記錄的保證金如何爲SalesOrderId過濾保證金查詢 如

Margin=(SELECT (ISNULL(((Sum(Price)-Sum(CurrentCost))/NULLIF(Sum(Price), 0))*100, 0)) 
       FROM HirschInternational_MSCRM.dbo.ProductBase 
       JOIN HirschInternational_MSCRM.dbo.SalesOrderDetailBase 
        ON HirschInternational_MSCRM.dbo.SalesOrderDetailBase.ProductId = HirschInternational_MSCRM.dbo.ProductBase.ProductId 
       JOIN HirschInternational_MSCRM.dbo.SalesOrderBase 
        ON HirschInternational_MSCRM.dbo.SalesOrderBase.SalesOrderId = HirschInternational_MSCRM.dbo.SalesOrderDetailBase.SalesOrderId 
Where HirschInternational_MSCRM.dbo.SalesOrderBase.SalesOrderId= //SalesOrderId that I get in main query) 

如何在此查詢中傳遞SalesOrderId

0

這不是完全清楚你想要的每一條記錄保證金,但你可能來尋找類似

select * 
from (your SQL SELECT statement goes here) t1 
where id = ?;