In my PostgreSQL database I have such table:
x
| 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;