私はExcel関数を持っています: =Max(0, -Min(A1-B1, C1-B1))
A1、B1、C1 はテーブルの値列です。
日付、キー、値
今のところ、日付は無関係であり、キーは A1、B1、C1 の A、B、C であると仮定します。
このエクセル関数を実装するにはどうすればよいですか? 私が最初に行った方法では、2 つの結合がありました (値をすべて同じ行の別々の列に取得するため) が、これはやり過ぎのように思えました。
私はExcel関数を持っています: =Max(0, -Min(A1-B1, C1-B1))
A1、B1、C1 はテーブルの値列です。
日付、キー、値
今のところ、日付は無関係であり、キーは A1、B1、C1 の A、B、C であると仮定します。
このエクセル関数を実装するにはどうすればよいですか? 私が最初に行った方法では、2 つの結合がありました (値をすべて同じ行の別々の列に取得するため) が、これはやり過ぎのように思えました。
必要なB
のは次のA
部分だけなので、おそらく 1 つの結合に減らすことができます。
select case
when min(Value - isnull(yt2.Value,0)) > 0 then 0
else -min(Value - isnull(yt2.Value,0))
end
from YourTable yt1
join YourTable yt2
on yt1.Key = 'A' and yt2.key = 'B'
or
yt1.Key = 'B' and yt2.key = 'C'
あなたの質問は、各行に個別の値を提案しています。もしそうなら、これはクエリです:
select t.*
(case when -leastval < 0 then 0
else -lesatval
end) as TheValue
end
from (select t.*,
(case when A1 - B1 < C1 and A1 - B2 < B1 then A1 - B1
when C1 < B1 then C1
else B1
end) as leastval
from table t
) t
質問を正しく読んだので、日付ごとに A、B、および C の値が 1 つあると仮定しましょう。次に、最初にデータを非正規化するだけで、上記のバリエーションを使用できます。
select t.*
(case when -leastval < 0 then 0
else -leastval
end) as TheValue
end
from (select t.*,
(case when A - B < C and A - B < B then A - B
when C < B then C
else B
end) as leastval
from (select date,
min(case when key = 'A' then value end) as A,
min(case when key = 'B' then value end) as B,
min(case when key = 'C' then value end) as C
from table t
group by date
) t
) t
これを定式化する方法は他にもあります。私は、Excel の定式化に適度に近づけるように努めました。
ところで、他のいくつかのデータベースは Greatest() および Least() 関数を提供しており、このような式をより簡単に作成できます。