1

so I have this kind of table:

img

What you can see is that I have a missing month number which is 1. What I want is to insert a value that is like this to my table:

Month  Year  Col1        Forecast   Customer
1      2015  HD/FB/BK/      0          AFC

since that was a missing month, so I want the forecast to be 0 value.

And my parameters for such insert would be by year,col1 and customer.

2
  • I dont get your missing number .. there is month 1 after 12 , what do you mean by missing number ? Commented Sep 29, 2015 at 2:16
  • as I said, the missing month depends on per row and by year, col1, and customer's value. So you can see there that I want to insert the missing month which is 1 and that is according to the year 2015, col1: HD/FB/BK/ and by customer: AFC Commented Sep 29, 2015 at 2:28

2 Answers 2

1

First, generate rows of all month-year combination based on your parameters. Then use NOT EXISTS to insert missing rows:

DECLARE @yr         INT,
        @col1       VARCHAR(50),
        @customer   VARCHAR(50);

WITH Cte(mnt, yr, col1, customer) AS(
    SELECT *, @yr, @col1, @customer
    FROM(VALUES
        (1), (2), (3), (4), (5), (6), (7), (8), (9), (10), (11), (12)
    )t(N)
)
INSERT INTO tbl(Month, Year, Col1, Forecast, Customer)
    SELECT
        mnt,
        yr,
        col1,
        0,
        customer
    FROM Cte c
    WHERE
        NOT EXISTS(
            SELECT 1 
            FROM tbl
            WHERE
                Col1 = c.col1
                AND Customer = c.customer
                AND Month = c.mnt
                AND Year = c.yr
        )
Sign up to request clarification or add additional context in comments.

Comments

0

Approach here is to generate combinations of month and col1 and match with the current table and generate the insert for missing rows. You may extend this with other required combinations of customer and year. This works well if there is no single entry for a particular combination.

Step 1: Create combinations table

SELECT 
    *
FROM
    (SELECT 1 month UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9 UNION ALL SELECT 10 UNION ALL SELECT 11 UNION ALL SELECT 12) months
        JOIN
    (SELECT 'HD/FB/BK' colname UNION ALL SELECT 'HD/FB/BL') col1

Step 2: Find missing items

select expected.* from data_table dt right join (SELECT 
    *
FROM
    (SELECT 1 month UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9 UNION ALL SELECT 10 UNION ALL SELECT 11 UNION ALL SELECT 12) months
        JOIN
    (SELECT 'HD/FB/BK' colname UNION ALL SELECT 'HD/FB/BL') col1) expected on (dt.month = expected.month and dt.Col1 = expected.colname) where dt.col1 is null

Step 3: Insert missing values (All combined)

insert into data_table
select datainsert.month, 2015, datainsert.colname, 0.0, 'AFC' FROM
(select expected.* from data_table dt right join (SELECT 
    *
FROM
    (SELECT 1 month UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9 UNION ALL SELECT 10 UNION ALL SELECT 11 UNION ALL SELECT 12) months
        JOIN
    (SELECT 'HD/FB/BK' colname UNION ALL SELECT 'HD/FB/BL') col1) expected on (dt.month = expected.month and dt.Col1 = expected.colname) where dt.col1 is null) datainsert;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.