0

I have a table A that has call count number of employees for last 30 days. However, if the employee does't have call for last 30 days, it won't show up in this table. How can I insert those employee into this table (say employee list is in table B)

Table A:

Employee  Date  Call_count
Jim        20180224   10
Jim        20180223   10
Jim        20180222   10
....
Jock        20180224   12
Jock        20180223   12
Jock        20180222   101
...

Table B only has employee names:

    Employee
    Jim
    Jock
    Ann

Expected result( say Ann is the one who doesn't have calls):

Employee  Date  Call_count
Jim        20180224   10
Jim        20180223   10
Jim        20180222   10
....
Jock        20180224   12
Jock        20180223   12
Jock        20180222   101
...
Ann         20180224   0
Ann         20180223   0
Ann        20180222   0
2
  • did u tried using insert into table ? Commented Feb 26, 2018 at 16:53
  • how are your constructing Table A? Is it not a result of some query? Commented Feb 26, 2018 at 16:54

3 Answers 3

1

If you have a calendar table, then this becomes a pretty basic query:

SELECT  b.Employee,
        c.Date,
        Call_count = ISNULL(a.Call_count, 0) 
FROM    dbo.Calendar AS c
        CROSS JOIN dbo.TableB AS b
        LEFT JOIN dbo.TableA AS a
            ON a.Date = c.[Date]
            AND a.Employee = b.Employee  
WHERE   c.Date >= DATEADD(DAY, -30, CAST(GETDATE() AS DATE))
AND     c.Date < GETDATE();

The key being you first generate a full set of all cominations of dates and names, then LEFT JOIN to your data, so now you are not limited to results that exist in TableA.

If you don't have a calendar table, then you can generate a list of dates on the fly. Much more reading about this is here:

Part 3 is specifically relevant as it deals with dates.

But for simplicity to get a list of the last 30 days you can generate a list of number from 1-30 using this:

SELECT  Date = DATEADD(DAY, -ROW_NUMBER() OVER(ORDER BY n1.n), CONVERT(DATE, GETDATE()))
FROM    (VALUES (1),(1),(1),(1),(1),(1)) n1 (n) 
CROSS JOIN (VALUES (1),(1),(1),(1),(1)) n2 (n); 

There are 3 steps here.

  1. Use table value constructors to create a set of 6 rows and a set of 6 rows, and cross join them to get 30 rows.
  2. Use ROW_NUMBER() to create a sequential id for each row.
  3. Subtract this number from today's date to get a list of the last 30 days.

You can now substitute this set into the query above in place of the calenar table:

WITH Calendar AS
(   SELECT  Date = DATEADD(DAY, -ROW_NUMBER() OVER(ORDER BY n1.n), CONVERT(DATE, GETDATE()))
    FROM    (VALUES (1),(1),(1),(1),(1),(1)) n1 (n) 
    CROSS JOIN (VALUES (1),(1),(1),(1),(1)) n2 (n)
)
SELECT  b.Employee,
        c.Date,
        Call_count = ISNULL(a.Call_count, 0) 
FROM    Calendar AS c
        CROSS JOIN dbo.TableB AS b
        LEFT JOIN dbo.TableA AS a
            ON a.Date = c.[Date]
            AND a.Employee = b.Employee ;
Sign up to request clarification or add additional context in comments.

Comments

0

Use insert with Filter clause. This will be more accurate if you have any id column instead of looking on name column

insert into tableA ( Employee, date,call_count)

select employee, date , 0 call_count from Tableb where employee not in 
(select employee from from tableA)

1 Comment

thanks but Table B only has employee name, it doesn't have any other data
0

You can try this way:

Insert into table A (employee,date,call_count)
Select employee,date,call_count from table B where isnull(call_count,0)<>0

1 Comment

thanks but Table B only has employee name, it doesn't have any other data

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.