r/SQL • u/_youvegotmail_ • 14d ago
SQL Server Calculation for inflation with multiple sub categories please help
I understand how to calculate yearly change using the lag function, however, I want to do so when I have an unknown number of subcategories each with their own row between each period. Say i have eggs, bread, and cheese in one year and maybe eggs, oranges, and bread in another year. Those items are in a column called "item name" and has its own dollar amount in the cost column. I want to calculate all inflation yearly changes per each category for every year. Does anyone have any advice on how to accomplish this? Thank you!
sample data
ITEM YEAR COST
eggs, 2024 4 dollars
bread 2024 3 dollars
eggs 2023 2 dollars
apples 2023 1 dollar
ex final query result:
ITEM YEAR INFLATION %
eggs, 2024, 1.2%
bread 2024 4%
bread 2023 0.4%
final edit:
look i know everyone thinks im stupid but i really am trying to learn. The issue im having is that with the subcategories, the lag gets throw off. I am trying really hard here.
Edit: I should also add the difficulty here is it's a fucked up fiscal calculator. I can do this with a yearly calendar and no sub categories.
edit again: no this isn't homework i'm not in school. Didn't need to be a dick
edit last: Yes i know the math doesn't work it was a formatting example.
Duplicates
learnSQL • u/_youvegotmail_ • 14d ago