CHanges identifier codes into catalog numbers. Extracts the catno where it can, but is guessing on other entries.
copy
(select
"cost type",
date,
coalesce(
( select catalogno from catalognew where catalogno like description_array[3] limit 1)
, catalogno) as catalogno,
details_identifier,
description,
"USt%",
"cost-id",
amount,
soll,
"haben ",
"WKZ",
"Eingabebetrag",
"Kurs"
from
(select
catalogno,
details_identifier,
description,
string_to_array(description, ' / ') as description_array,
"cost type",
date,
"USt%",
"cost-id",
amount,
soll,
"haben ",
"WKZ",
"Eingabebetrag",
"Kurs"
from
(select
(select catalogno from catalognew
where identifier like t.info and format != 'Digital ALbum' and format != 'Digital Single' and format != 'Digital One Track'
limit 1) as catalogno,
t.info as details_identifier,
t.description,
"cost type",
date,
"USt%",
"cost-id",
amount,
soll,
"haben ",
"WKZ",
"Eingabebetrag",
"Kurs"
from
(select
"cost type",
date,
info,
description,
"USt%",
"cost-id",
amount,
soll,
"haben ",
"WKZ",
"Eingabebetrag",
"Kurs"
from datev_to_details
) as t
) as q
) as p
order by catalogno nulls first
) to '/tmp/services_kost_repair.csv' csv