select * from (
SELECT
805050,
case
when q.identifier = any(array[
'K7308','K7317','K7322','K7328','K7329','K7341','K7S350','K7S369','K7S379'
]) then 67020
when q.mmpidentifier = any(array[
'101a0', '101a114', '101a64', '101a102','101a2','101a4', '101a1','101a3','101a5'
]) then 67010
-- K7308 K7317 K7322 K7328 K7329 K7341 K7S350 K7S369 K7S379
else 67020
end, -- code based on LG v LS
'12/31/2019', --last day of the month, make slashes
q."Cat.No." || '|K7 Shipping / 12 2019', --embed catalog number here
q."Invoice",
--'201912' || substring(q."RechnungNr" from 8 for 6), -- fix to be like '201912983' what is this one? date plus code?
q.identifier as identifier,
' ',
'EUR',
'EUR',
1, -- lookup conversion from last day in month
' ',
--q."AMOUNT" as charge,
round((q."Backcharge")::numeric, 2) as charge,
' ',
' ',
9
FROM (
SELECT
*
FROM k7_shipping
LEFT JOIN
(select
catalogno,
min(mmpidentifier) as mmpidentifier,
min(identifier) as identifier
from catalognew
group by
catalogno
) as catalognew
ON
catalognew.catalogno ilike k7_shipping."Cat.No."
) AS q
) as t
where charge > 0
order by identifier nulls first