0

私はMSSQL2005を使用しており、最後の2つのレコードから値を返すCTEクエリを作成しました。次に、これを使用して、返された2つの数字のデルタを見つけます。ある種の作業クエリがありますが、デルタ数値以外のものを取得するのに問題があります。

これが私の質問です:

;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.

クエリの最後にgroupbyなどを追加しようとすると、「group」という単語についてさらにエラーが発生します。

私はSQLに比較的慣れていないので、作業を進めていきます。どんな助けでもありがたいことに受けられるでしょう。

4

2 に答える 2

0

申し訳ありませんが、私はここで非常に新しく、CTEクエリがわかりませんが、データを定義した後、case ...をDeltaFROM...として選択しているようです。つまり、selectステートメントにはDeltaしかありません。繰り返しになりますが、私がベースから離れている場合は申し訳ありません。

于 2009-05-05T12:23:41.820 に答える