Skip to content
Advertisement

Append Row To Each Group in SQL

Let’s say that I have database table:

Following:

I get:

However, for each group, I’d like to append an additional row with the value column taken from the id column so that the resulting table looks like:

What is ANSI SQL (or most database agnostic) way to accomplish this?

Advertisement

Answer

You don’t want a group by in the initial set as you appear to want all the rows back:

And you can do this with grouping sets for the fun of it:

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