I have a table called StockList which is used to keep track of available stock details
I have inserted all the available stocks initially (inserted on 04-12-2022). My stockList table looks like this.
After initial load, Everyday(12 AM) I will insert records in this table based on the stock availability. i.e)On next day, my table looks like this.
Means, StockId - 12121 size is changed
StockId - 13131 is deleted
StockId - 14141 colour is changed
StockId - 15151 is same as yesterday
stockId - 16161 is newly added stock
Similarly I will keep on updating this table everyday 12AM with action date as that particular date. Iam not maintaining any action type(inserted/updated/deleted) and is_deleted column also.
So now I want a report like inserted, updated and deleted stock on particular day. E.g) On 04-13-2022 I need a report like this
Is it possible to get this in a single query? If yes, how can I get this in single optimized query? I am using Google - Bigquery.
I am trying like this,
WITH
today AS (
SELECT
*
FROM
`myProject.Mydataset.StockList`
WHERE
ActionDate = '04-13-2022' ),
yesterday AS (
SELECT
*
FROM
`myProject.Mydataset.StockList` WHERE
ActionDate = '04-12-2022' )
.
.
.
.
I am not sure on this. Can anyone help on this? Thank you :)
This query can produce the desired report
The idea is to get the latest row for each StockId and then have a CASE-WHEN clause for each Action: