Query with a different filter for each line

Asked

Viewed 30 times

0

I’m doing a query where, I have 3 columns related by a text filter Where '.' Like %xxx%', where each row of the column has a different filter (a Like %xxx% different). I tried to mount cases but I could not, follows below as I thought:

Select `Estruturada_Duplicata`.`duplicata_comment`, `Estruturada_Duplicata`.`duplicata_value` 
from(
Select `Estruturada_Duplicata`.`duplicata_comment`, `Estruturada_Duplicata`.`duplicata_value`
Case when (duplicata_comment = '%restri%') then count(`Estruturada_Duplicata`.`duplicata_comment`), sum(`Estruturada_Duplicata`.`duplicata_value`) end restricao,
Case when (duplicata_comment = '%protesto%') then count(`Estruturada_Duplicata`.`duplicata_comment`), sum(`Estruturada_Duplicata`.`duplicata_value`) end protesto
from(`dataform`.`estruturada_duplicata`)
  )
LIMIT 1;

I also tried with Queries, but I couldn’t structure it because you can only bring one column per sub. If not clear in the code: a query column will only bring the texts duplicata_comment, another count how many comments there are with specified filter, and another add the duplicate value with the specified filter.

1 answer

1

If I understand, try:

SELECT  COUNT(CASE WHEN (duplicata_comment = '%restri%') THEN 1 ELSE 0 END) cont_restricao,
            SUM(CASE WHEN (duplicata_comment = '%restri%') THEN `Estruturada_Duplicata`.`duplicata_value` ELSE 0 END) val_restricao,
            COUNT(CASE WHEN (duplicata_comment = '%protesto%') THEN 1 ELSE 0 END) cont_protesto,
            SUM(CASE WHEN (duplicata_comment = '%protesto%') THEN `Estruturada_Duplicata`.`duplicata_value` ELSE 0 END) val_restricao
FROM `dataform`.`estruturada_duplicata`;
  • I’ve been testing this solution in some ways, but it keeps going wrong. Duplicate Counts always give the same value, regardless of the condition. And the sum of the values always gives zero.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.