2009-04-01 11 views
1

昨日質問された質問と同じように、私は別の「カウント」というジレンマを持っています。指定値を超えるDIFFDATE - ステップ3

  1. ConditionLevelEntryID = 189
  2. CheckoffDateがConditionEntryDateTime
  3. から> 14日
  4. CheckoffDateがnullで、ConditionEntryDateTimeが
  5. > 14日齢である:今、私は、次のパラメータを満たす条件エントリの数を引く必要があります

私は次のクエリを試行しましたが、これは動作しません。返されるエラーはクエリの下にあります。正確なカウントを取得するためにクエリを洗練させてください。前もって感謝します。

select Count(*) 
from conditionentrytable c 
where conditionlevelentryid=189 
    and 
    ((c.checkoffdate IS NULL 
    and 
    convert(varchar(12),DATEADD(day,14,c.conditionentrydatetime))) 
    or 
    DATEDIFF(dd,c.checkoffdate,c.conditionentrydatetime)>14) 

サーバー:メッセージ170、レベル15、状態1、 7行7行目: 近くに不適切な構文 ')'。

答えて

2

この試してみてください:あなたはおそらく大幅に改善されますいずれかを持っている場合、後者のクエリは(conditionlevelentryid, conditionentrydatetime)に複合インデックスを使用します

SELECT COUNT(*) 
FROM conditionentrytable c 
WHERE conditionlevelentryid=189 
     AND c.conditionentrydatetime > COALESCE(c.checkoffdate, GETDATE()) - 14 

SELECT COUNT(*) 
FROM conditionentrytable c 
WHERE conditionlevelentryid=189 
     AND DATEDIFF(dd, COALESCE(c.checkoffdate, GETDATE()),c.conditionentrydatetime) > 14 

、またはより良いをクエリのパフォーマンス

1

私は少し錆びますが、変換されたconditionentrydatetimeはIS NULL式では何も実行されていません。

どのようですか?

select Count(*) 
from conditionentrytable c 
where conditionlevelentryid=189 
    and 
    ((c.checkoffdate IS NULL 
    and 
    DATEDIFF(dd,getdate(),c.conditionentrydatetime)>14) 
    or 
    DATEDIFF(dd,c.checkoffdate,c.conditionentrydatetime)>14)) 
関連する問題