1

SQLでクエリを問題なく実行しましたが、Access Caseで機能しないため、Access権限で使用できないため、IIfを使用する必要があります...

select
    sum(case when date between '2012-05-01' AND '2012-06-01' then value else 0 end) as sum1,
    sum(case when date between '2012-05-01' AND '2012-10-01' then value else 0 end) as sum2,
    sum(case when situation like 'Urgent' then 1 else 0 end) as urgent,
    sum(case when situation like 'Anual' then 1 else 0 end) as anual,
    sum(1) as total
from mytable

これを行うためのより良い方法はありますか?このクエリは機能しますか?

  select
        sum(IIf date between '2012-05-01' AND '2012-06-01',value,0) as sum1,
        sum(IIf date between '2012-05-01' AND '2012-10-01',value,0) as sum2,
        sum(IIf situation like 'Urgent',1,0 ) as urgent,
        sum(IIf situation like 'Anual' ,1,0 ) as anual,
        sum(1) as total
    from mytable
4

1 に答える 1

2

IIf()のように括弧が必要ですIIf(expression, truepart, falsepart)

どちらも予約語であるため、角かっこでdate囲みます。value

Like比較にはパターンマッチングが含まれていなかったため、代わりに置き換えまし=た。

日付は日付/時刻データ型であると想定したため、値を#区切り文字で囲みました。

SELECT
    Sum(IIf([date] BETWEEN #2012-05-01# AND #2012-06-01#,[value],0)) AS Sum1,
    Sum(IIf([date] BETWEEN #2012-05-01# AND #2012-10-01#,[value],0)) AS Sum2,
    Sum(IIf(situation = 'Urgent',1,0 )) AS urgent,
    Sum(IIf(situation = 'Anual' ,1,0 )) AS anual,
    Sum(1) AS total
FROM mytable;
于 2012-10-16T17:59:10.610 に答える