Thanks for your suggestions.I have changed the SP as below and it works fine, though i am surprised why its working.This is similar to the one posted in my question with a minor change **
Alter proc [dbo].[SD_GetProfitibilityPerItemDetails]
@fromdate datetime,
@todate datetime
as
Begin
with p as (
select b.ItemCode,b.ItemDesc as [Description],
sum(c.BatchSellQty) as Quantity,(sum(b.SellPrice)/sum(c.BatchSellQty)) as AvgSellPrice,
(sum(b.NetPrice)/sum(c.BatchSellQty)) as AvgNetPrice
from
InvoiceDetails a inner join ItemDetails b on a.QuoteID=b.QuoteID
inner join BatchDetails c on b.QuoteID=c.QuoteID and b.ItemID=c.ItemID
where b.IsDeleted=0 and a.InvoiceDate between @fromdate and @todate
group by b.ItemCode,b.ItemDesc)
select p.ItemCode,p.Description,p.Quantity,p.AvgNetPrice as [Avg Net Price],
**AvgSellPrice as [Avg Sell Price],**
(p.AvgSellPrice - p.AvgNetPrice) as [ProfitAmount]
from p
End
In this SP when i include P (p.AvgSellPrice as [Avg Sell Price]) it gives me a error.But when I removed P (AvgSellPrice as [Avg Sell Price]) it works like a charm,though it is strange.