Hello to all,
My task is to find earnings for each client according to 3 rules:
Find a customer by
custom_id
, ifcustom_id
not with a number, then you need to search for another typecustom_id
.Find a client by
ae_custom_id
but if it’s on the same line ascustom_id
then they have the same amount.Find client by
re_custom_id
but underre_custom_id
should be only one client record in the database.
I am trying to fulfill the following query and I already have a problem and cannot understand why.
select sum(case when exists (select * from test_table where custom_id is not null) exists (select * from test_table join ee_client_id on ee_client_id.custom_id = case when test_table.custom_id ~ '(^[0-9]+)$' then case when substr(test_table.custom_id,1,3)::integer = 901 then substr(test_table.custom_id,1,10)::bigint when substr(test_table.custom_id,1,3)::integer between 100 and 106 then substr(test_table.custom_id,1,3)::bigint end end) then total_sum else 0 end) from test_table
case when exists (select * from test_table where custom_id is not null)
very strange. it will also work if there is at least one entry in the table with a non-empty custom_id. Not seen here cut by customers. Could you on some sqlfiddle.com to make a test label with the data and say what you want to get. In the current form to understand the meaning of the question is unrealistic. Just like how your request is related to it. what the values 901, 100-106 - they are not found in the text description of the question - Mike