想要根据项目按项目和金额总和进行分组

时间:2015-05-26 05:30:15

标签: group-by subquery correlated-subquery

按项目分组和总金额 我有外部查询从临时表中选择字段如何在这里获得相同项目的总和? 这是查询

 Select temp.Item,temp.TotalReceivable,temp.Bal  from (
              select (
              (Select SUM(io.TotalAmount) from Inventory_Out io where 
               io.CampusID = cr.CampusID ) - SUM(cr.AmountReceived) Over()) as TotalReceivable,

              (Select ISNULL(SUM(Inventory_Out.TotalAmount),0) - (Select SUM(AmountReceived) from CampusRecovery where CampusRecovery.BillNo = cr.BillNo )
               from Inventory_Out where Inventory_Out.CampusID = cr.CampusID and Inventory_Out.BillNo = cr.BillNo
              )as Bal,

             (Select Item_name from Product where Product.ID = cr.ItemID) as Item

             from CampusRecovery cr

             where 
             cr.CampusID = @CampusID AND  
             cr.RecvDate >= Cast(@StartDate as datetime) AND cr.RecvDate <= Cast(@EndDate as datetime)
            ) as temp
  group by 
  temp.Item,
  temp.TotalReceivable,
  temp.bal

0 个答案:

没有答案