我使用的是ms sql 2005,我创建了一个cte查询来返回最后两条记录的值。然后我用这个来找到返回的两个数字的增量。我有一个工作问题,但是
除了三角洲的数字我什么都得不到。
我的问题是:
;with data as(
SELECT
NetObjectID,
RawStatus,
RowID,
rn
from(
SELECT
CustomPollerAssignmentID AS NetObjectID,
RawStatus,
RowID,
row_number() over(order by DateTime desc)as rn
FROM CustomPollerStatistics_Detail
WHERE
(CustomPollerAssignmentID='a87f531d-4842-4bb3-9d68-7fd118004356')
) x where rn<=2
)
SELECT
case when
max(case rn when 1 then RawStatus end) > max(case rn when 2 then RawStatus end)
then
max(case rn when 1 then RawStatus end) - max(case rn when 2 then RawStatus end)
else
max(case rn when 2 then RawStatus end) - max(case rn when 1 then RawStatus end)
end as Delta
from data having
(SELECT
case when
max(case rn when 1 then RawStatus end) > max(case rn when 2 then RawStatus end)
then
max(case rn when 1 then RawStatus end) - max(case rn when 2 then RawStatus end)
else
max(case rn when 2 then RawStatus end) - max(case rn when 1 then RawStatus end)
end
from data) >= 1
我要做的是让delta&netobjectid返回。每次我尝试,我都会出错。
data.NetObjectID is invalid in the select list because it is not contained in either an aggregate function or the group by clause.
如果我尝试添加group by等。在查询的最后,我得到了进一步的错误,抱怨“组”这个词。
我对sql还不太熟悉,而且我会边走边捡东西。如有任何帮助,我们将不胜感激。