Skip to content
Advertisement

TSQL – Fill in missing values as 0 for dates without a value

Apologies in advance for the pictures. I did these up as tables in Stack and they displayed fine in Preview but would not display properly once published.

I have a sample dataset like below with many more KPIs.

Picture Of Tables

The dataset currently returned is just a join of these 2 tables and returns date instead of Date ID, like below: Query

DataSet Returned

I need the dataset to return like below (notice the additional record for KPI B on the 02-01-2022

I can achieve this by below query, but it is very time consuming considering there are a large number of KPIs I need to do this for. Would greatly appreciate any assistance in how I can get this to work somewhat dynamically

Any assistance would be very much appreciated before I spend the next day re-writing the query with hard-coded CASE statements

Advertisement

Answer

You want one result per date and KPI type. So cross join the two and then outer join your data.

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