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

WITH ExampleQuery AS
(
    SELECT
        KPI, Value, Date
    FROM
        Date
    LEFT JOIN 
        KPI ON Date.DateID = KPI.DateID
)
SELECT
    KPI, 
    SUM(Value),
    Date
FROM
    ExampleQuery
GROUP BY
    Date, KPI

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

WITH ExampleQuery AS
(
    SELECT
        CASE WHEN KPI = 'A' THEN Value ELSE 0 END AS ValueA,
        CASE WHEN KPI = 'B' THEN Value ELSE 0 END AS ValueB,
        Date,
        KPI
    FROM
        Date
    LEFT JOIN 
        KPI ON Date.DateID = KPI.DateID
)
SELECT
    KPI, ValueA, Date
FROM
    ExampleQuery
WHERE
    KPI = 'A'
UNION ALL
SELECT
    KPI, ValueB, Date
FROM
    ExampleQuery
WHERE
    KPI = 'B'

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.

select
  date.date,
  type.kpi,
  coalesce(sum(kpi.value), 0) as total
from date
cross join (select distinct kpi from kpi) type
left join kpi on kpi.dateid = date.dateid and kpi.kpi = type.kpi
group by date.date, type.kpi
order by date.date, type.kpi;
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement