views:

45

answers:

2

I have a table that lists number of comments from a particular site like the following:

Date                        Site         Comments         Total
---------------------------------------------------------------
2010-04-01 00:00:00.000      1              5               5
2010-04-01 00:00:00.000      2              8               13
2010-04-01 00:00:00.000      4              2               7
2010-04-01 00:00:00.000      7              13              13
2010-04-01 00:00:00.000      9              1               2

I have another table that lists ALL sites for example from 1 to 10

Site
-----
1
2
...
9
10

Using the following code i can find out which sites are missing entries for the previous month:

SELECT s.site
from tbl_Sites s
EXCEPT
SELECT c.site
from tbl_Comments c
WHERE c.[Date] = DATEADD(mm, DATEDIFF(mm, 0, GetDate()) -1,0)

Producing:

site
-----
3
5
6
8
10

I would like to be able to insert the missing sites that is listed from my query into the comments table with some default values, i.e '0's

Date                        Site         Comments         Total
---------------------------------------------------------------
2010-04-01 00:00:00.000      3              0               0
2010-04-01 00:00:00.000      5              0               0
2010-04-01 00:00:00.000      6              0               0
2010-04-01 00:00:00.000      8              0               0
2010-04-01 00:00:00.000      10             0               0

the question is, how did i update/insert the table/values?

cheers,

Lee

A: 

A Left Join from Site to your commenttable should do the work i guess

At least if I did unterstand your intention

EDIT : Sry thought you wanna select all sites with those comments

KroaX
+2  A: 
INSERT INTO CommentTable (Date, Site, Comments, Total)
SELECT '2010-04-01 00:00:00.000', Site, 0, 0
FROM SiteTable
WHERE Site NOT IN
    (SELECT DISTINCT Site FROM CommmentTable
     WHERE [Date] = DATEADD(mm, DATEDIFF(mm, 0, GetDate()) -1,0))
Zarigani
Worked like a charm.
Lee_McIntosh