середа, 17 квітня 2019 р.

Unpivot the table

Recently I had an interview and interviewer asked me how I can to rotate the following table:
To rotate means to get the following result (example for shop_id = 1):
I suggested using unpivot and e.g. in Oracle it could be smth like that:

But I was interrupted and the additional requirement was what would I do if the use of unpivot is prohibited? I proposed to rewrite the sql-query via UNION ALL:

However, my opponent asked me to figure out one more method via a recursive query. And here it is:

And in general using queries above you can get the following result:

Немає коментарів:

Дописати коментар