Skip to content
Advertisement

SQL many-to-one join – how to get blank fields instead of duplicated values

I have 3 tables:

When I join this tables by ticket_id and query, i get result like this:

But I want to blank duplicated values and get result like this:

Is it possible to get result set like this? I’m working in AWS Athena(Presto SQL), but I would really appreciate your ideas/suggestions on this regardless of the dbms. Thanks!

Advertisement

Answer

With the caveat that you should not be doing this in SQL because this is a presentation layer concern, not a data-layer concern – instead you should filter-out repeated column data only at the point where you’re rendering the HTML table or rendering to the screen somehow.

However if for some horrible reason you need to do this in SQL, something like this should work.

  • Note the repeated use of explicit ORDER BY clauses to ensure correctness: never rely on implicit ordering.
  • Also note that CTEs and other sub-queries cannot have an ORDER BY: only top-level SELECT queries can have an ORDER BY clause, hence the need for ROW_NUMBER() OVER ( ORDER BY ...) in the CTE.
  • My query uses ticket_id for the description column because I assume that description is keyed by ticket_id, if it is not then you need to compare description directly.
    • Be-aware that textual comparisons are case-insensitive, which may or may not be desirable, otherwise use an explicit COLLATION to specify a case-sensitive comparison.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement