Run-length encoding: Difference between revisions

code sample changed to PL/pgSQL
(sql language example added)
(code sample changed to PL/pgSQL)
Line 2,889:
 
=={{header|SQL}}==
{{works with|T-SQLPL/pgSQL}}
* RLE encoding
<lang tsqlSQL>
-- variable table
declare @string varchar(1000)
drop table if exists var;
set @string = 'WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW'
create temp table var ( value varchar(1000) );
setinsert @stringinto =var(value) select 'WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW';
 
-- select
;with
with recursive
ints(num) as
(
Line 2,902 ⟶ 2,905:
select num+1
from ints
where num+1 <= LENlength(@string(select value from var))
)
,
Line 2,910 ⟶ 2,913:
from (
select num,
substring(@string(select value from var), num, 1) chr,
(select substring(@string(select value from var), num+1, 1)) nextChr
from ints
) tmp
)
select @string(select Inputvalue from var) plain_text, cast((
select string_agg(concat(cast(maxNoWithinGroup as varchar(10)) +, chr), '' order by num)
from (
select *, max(noWithinGroup) over (partition by chr, groupNo) maxNoWithinGroup
Line 2,935 ⟶ 2,938:
) final
where noWithinGroup = 1
) Rle_Compressed
order by num
for xml path('')) as xml).value('.','varchar(max)') RleCompressed
option (maxrecursion 0)
</lang>
 
Anonymous user