IF OBJECT_ID ('dbo.AllocatedReleasedExpenses','V') IS NOT NULL DROP VIEW dbo.AllocatedReleasedExpenses
GO 
CREATE VIEW dbo.AllocatedReleasedExpenses
AS
SELECT 
    ROW_NUMBER() OVER (
        ORDER BY 
            p.CompanyID, 
            p.ProjectID
    ) AS RowKey,
 p.CompanyID as CompanyID, p.ProjectID, SUM(p.Amount) as amount
 from PMTran p 
 join PMAccountGroup pg on pg.CompanyID = p.CompanyID and p.AccountGroupID = pg.GroupID 
 where pg.IsExpense = 1
 and p.Released =1
 and p.Allocated =1
 group by p.CompanyID,
          p.ProjectID