Skip to content
Advertisement

SQL – get summary of differences vs previous month

I have a table similar to this one:

It contains the stores that are active at BOM (beginning of month).

How do I query it to get the amount of stores that are new that month – those that where not active the previous month?

The output should be this:

I now how to count the first time that each store is active (nested select, taking the MIN(BOMdate) and then counting). But I have no idea how to check each month vs its previous month.

I use SQL Server, but I am interested in the differences in other platforms if there are any.

Thanks

Advertisement

Answer

How do I query it to get the amount of stores that are new that month – those that where not active the previous month?

One option uses not exists:

You can also use window functions:

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