1

これは機能する私のクエリです:

Select AsOfDate, Family, Type, DocID, Title, Date1, Date2, Date3, Stat1, Stat2, Stat3
FROM DocumentationData
WHERE Type = @Type 
AND Family = @Family
AND AsOfDate = (SELECT Max(AsOfDate)
                       FROM DocumentationData
                       WHERE AsOfDate <= @CurrentDate )

条件を追加したい。@ActiveOnly が true の場合、Usage = 'Active" という追加の制約が必要です。

これは私が試みていることですが、エラーが発生します:

Select AsOfDate, Family, Type, DocID, Title, Date1, Date2, Date3, Stat1, Stat2, Stat3
FROM DocumentationData
WHERE Type = @Type 
AND Family = @Family
AND AsOfDate = (IF (@ActiveOnly = 'TRUE')
                BEGIN
                SELECT Max(AsOfDate)
                FROM DocumentationData
                WHERE AsOfDate <= @CurrentDate
                AND Usage = 'Active'
                END
                ELSE
                BEGIN
                SELECT Max(AsOfDate)
                FROM DocumentationData
                WHERE AsOfDate <= @CurrentDate
                END
               )
4

3 に答える 3

0

他に選択肢がない場合は、次のようにすべてを 2 つのクエリにコピーするとうまくいくことがわかりました。

IF(@ActiveOnly = 'TRUE')
BEGIN
Select AsOfDate, Family, Type, DocID, Title, Date1, Date2, Date3, Stat1, Stat2, Stat3
FROM DocumentationData
WHERE Type = @Type 
AND Family = @Family
AND AsOfDate =  (SELECT Max(AsOfDate)
                      FROM DocumentationData
                      WHERE AsOfDate <= @CurrentDate
                      AND Usage = 'Active'
                      )
END                          
ELSE
BEGIN
Select AsOfDate, Family, Type, DocID, Title, Date1, Date2, Date3, Stat1, Stat2, Stat3
FROM DocumentationData
WHERE Type = @Type 
AND Family = @Family
AND AsOfDate =  (SELECT Max(AsOfDate)
                          FROM DocumentationData
                          WHERE AsOfDate <= @CurrentDate
                          )
END
于 2013-05-24T19:39:06.203 に答える
0

を使わなくてもできると思いますIF...THEN...ELSE。あなたは最初のクエリの試みに近づきました.サブクエリに IF追加の句を使用して同様のロジックを適用するだけです.WHERE

SELECT AsOfDate, Family, Type, DocID, Title, Date1, Date2, Date3, Stat1, Stat2, Stat3
  FROM DocumentationData
 WHERE Type = @Type 
   AND Family = @Family
   AND AsOfDate =  (SELECT Max(AsOfDate)
                      FROM DocumentationData
                     WHERE AsOfDate <= @CurrentDate
                       AND ( ( @ActiveOnly = 'TRUE' AND Usage = 'Active') 
                             OR (@ActiveOnly <> 'TRUE') 
                           )
                     )
于 2013-05-24T19:56:09.833 に答える
0

I think you need a THEN in there.

AND AsOfDate =  IF (@ActiveOnly = 'TRUE') 
                THEN                 
                SELECT Max(AsOfDate)
                FROM DocumentationData
                WHERE AsOfDate <= @CurrentDate
                AND Usage = 'Active'

                ELSE
                BEGIN
                SELECT Max(AsOfDate)
                FROM DocumentationData
                WHERE AsOfDate <= @CurrentDate

                END IF
于 2013-05-24T19:00:18.520 に答える