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())

我想要每条记录的保证金,我想要输出像 在此处输入图像描述

4

2 回答 2

0

目前还不完全清楚你想要什么,但你可能正在寻找类似的东西

select *
from (your SQL SELECT statement goes here) t1
where id = ?;
于 2013-07-02T10:55:06.780 回答
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

于 2013-07-02T11:22:15.390 回答