Skip to content
Advertisement

How do I replace dynamic values?

In my PostgreSQL database I have such table:

| employee | question                     | answer     |
|----------|------------------------------|------------|
| Bob      | What is your favorite color? | Blue       |
| Alex     | What is your favorite color? | Yellow     |
| Bob      | What is your favorite movie? | The Matrix |
| Alex     | What is your favorite movie? | Shrek      |

This is only a small fraction of the records. I’m trying to create an SQL query that would hide people’s identities. In other words, I need to get this result:

| employee             | question                     | answer     |
|----------------------|------------------------------|------------|
| Anonymous user 1     | What is your favorite color? | Blue       |
| Anonymous user 2     | What is your favorite color? | Yellow     |
| Anonymous user 1     | What is your favorite movie? | The Matrix |
| Anonymous user 2     | What is your favorite movie? | Shrek      |

What do you recommend for this case?

Advertisement

Answer

You can use dense_rank() if you just want to hide the names:

select dense_rank() over (order by employee) as just_an_id,
       . . .
from t;
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement