Skip to content
Advertisement

Compare the values in the successive rows and display newly added or deleted items in a different columns

I have a database with date and list of items. The problem this I do have an event date captured whenever an item is added or deleted. So what I am trying to do is comparing the items for a user ‘A’ on different days (successive rows); here the newly added or deleted items for user ‘A’ should be displayed in new columns ‘Items added’ and ‘Items Removed’

Expected output for user ‘A’:

I’d really appreciate if someone help me do this. Thanks in advance

Advertisement

Answer

Although this is a really bad data model, it is something of a fun question. Because Postgres was one of the original tags, there is a Postgres version:

Here is a db<>fiddle.

In BigQuery, the logic is basically the same, just some nuances for unnesting the string:

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