Do not bring duplicates with a certain condition in a SELECT

Asked

Viewed 42 times

0

I need to build a query that removes duplicates from a custom column (concatenated) if a given column is X value. For example:

SELECT emp, cod, vev, gru, concat(cast(emp as char(20)), cast(cod as char(20))) as RES
from hcov 
  CASE 
    WHEN GRU = 212 THEN GROUP BY RES END 

I tried to assemble this query but error:

SQL Error [700]: [Elevate Software][ElevateDB] ElevateDB Error #700 An error was found in the statement at line 4 and column 4 (Expected end of expression but instead found WHEN)

Basically, what I need is to remove duplicates from the concatenated column only if the GRU column is 212.

1 answer

0

Fernando, I think you can leave Group By with the column res and use Case to add another column that does not duplicate if the GRU column is different from 212. Example allowing the column emp does not duplicate:

from hcov 
GROUP BY RES, CASE WHEN GRU <> 212 THEN emp END 

I hope it helps

  • sorry I’m a little layman on this subject, I don’t know if this way will help me because I posted here an example of the query, in fact what will go into production the concatenated column would have 6 fields and not only 2 as I did in the summary here.. would have some other logic to this case?

  • I believe that the amount of concatenated columns will not affect the result, the important thing is you put a column that does not duplicate where in the example is the emp column. try running some tests to see what happens

  • any column can duplicate... what I need is for the query to use the concatenated column to check if it has duplicity when the GRU is 212.. and if there is only bring one.. for the other GRU should even bring duplicates.. understood?

  • Is there a column or set of columns in the table that does not duplicate? Which database and version are you using?

Browser other questions tagged

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