【发布时间】:2014-03-12 18:42:15
【问题描述】:
以下是使用公用表表达式的存储过程。但我不断收到无效的列名“SellPrice”。你能告诉我我哪里出错了
Create proc [dbo].[GetDetails]
@fromdate datetime,
@todate datetime
as
Begin
with cte as
(
select
b.ItemCode,
b.Description,
sum(c.Quantity) as Qty,
sum(b.NetPrice) as NetPrice,
sum(b.SellPrice) as SellPrice
from Invoicedetails a inner
join ItemDetails b
on a.InvoiceID=b.InvoiceID
inner join BatchDetails c
on b.InvoiceID=c.InvoiceID and b.ItemID=c.ItemID
where a.IsDeleted=0 and a.InvoiceDate between @fromdate and @todate
)
select
cte.ItemCode,
cte.Description,
(NetPrice/Qty) as [AvgNetPrice],
(SellPrice/Qty) as [AvgSellPrice], ((SellPrice/Qty) -(NetPrice/Qty) ) as ProfitAmount
from cte
End
** 有趣的是,当我删除这个“(SellPrice/Qty) as [AvgSellPrice]”时效果很好。但是当我将 (SellPrice/Qty) 包含为 [AvgSellPrice] 时,它给了我错误。 问候, 普拉塔普。
【问题讨论】:
-
推测
ItemDetails.SellPrice不存在。 -
调试此类查询的第一步是正确格式化。
-
已编辑问题。请立即提出建议。
标签: sql-server tsql sql-server-2005 procedure