は、与えられたライセンス数の連続した増加の最大桁数を決定するための効率的な方法であります。私はYES/NO列を気にしませんでした。例えば、MAX_COUNT
とlength(license_number)
を比較するなど、簡単に得られます。それを運動として残す。
ライセンス番号を個々の文字に分割し、位置を追跡する必要があります。オラクルはmod(-1, 10)
が-1であり、9ではないと信じているので(MODULO
関数を再学習するためには、実際に小学校に戻る必要があります)、10を加算する必要がありますすべてのケースで正しい結果が得られます(以下のソリューションでは「奇妙」を説明します)。今問題は次のようになります。各ライセンス番号に対して、最長の "等価"残基mod 10を探します。
"Tabibitosanメソッド"(CTEのrow_number()
の2つの違い、私の解決策ではprep
と呼ばれています)を使用して、シーケンス内の連続した「何か」を見つけることが最も効率的です。それで、それはすべてグループ化、数えられ、max()
を取ることの問題になります。
with
-- begin of test data; not part of the solution
test_data (license_number) as (
select '123456789' from dual union all
select '123455678' from dual union all
select '456789012' from dual union all
select '12345' from dual union all
select '123451234' from dual union all
select '402023488' from dual union all
select '4189012' from dual
),
-- end of test data; solution (SQL query) continues below this line
tokenized (license_number, idx, res) as (
select license_number, level,
mod(10 + to_number(substr(license_number, level, 1)) - level, 10)
from test_data
connect by level <= length(license_number)
and prior license_number = license_number
and prior sys_guid() is not null
),
prep (license_number, res, grp) as (
select license_number, res,
row_number() over (partition by license_number order by idx) -
row_number() over (partition by license_number, res order by idx)
from tokenized
),
grouped (license_number, res, grp, ct) as (
select license_number, res, grp, count(*)
from prep
group by license_number, res, grp
)
select license_number, max(ct) as max_count
from grouped
group by license_number
;
出力:
LICENSE_NUMBER MAX_COUNT
-------------- ---------
123455678 5
123456789 9
456789012 9
123451234 5
4189012 5
12345 5
402023488 3
あなたは「順次文字数」および実施例はその点では有用ではありません説明しませんでした。具体的には:文字列の先頭または文字列のどこに何桁の数字が連続しているかを知りたいですか? 3621234の連続する文字の数は何ですか?それは1か4か? – mathguy