简体   繁体   中英

Select distinct on result

I have a stored procedure and it has this case:

CASE
WHEN ED.DESCRIPCION IS NOT NULL THEN ED.DESCRIPCION
WHEN  adjuntos > 0 THEN 'PRES' 
WHEN adjuntos=0 AND obligatory = 'N' THEN 'NO'
ELSE 'PEN'
END AS STATE

I need to select distinct values of that result STATE , something like placing the DISTINCT before the case, this stored procedure currently returns:

PRES
NO
PEN
PEN
PEN
PEN

You literally just need to add the DISTINCT keyword:

SELECT DISTINCT CASE
WHEN ED.DESCRIPCION IS NOT NULL THEN ED.DESCRIPCION
WHEN  adjuntos > 0 THEN 'PRES' 
WHEN adjuntos=0 AND obligatory = 'N' THEN 'NO'
ELSE 'PEN'
END AS STATE
FROM ...

db<>fiddle with some made-up data.

If you are selecting other columns at the same time then you need DISTINCT before the first column expression in the select list. It applies to the entire row of the result set, not to individual columns; and it isn't a function. From the documentation :

Specify DISTINCT or UNIQUE if you want the database to return only one copy of each set of duplicate rows selected. These two keywords are synonymous. Duplicate rows are those with matching values for each expression in the select list.

But you have only shown one column expression, so this doesn't seem to apply.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM