如何从两个表创建第三个表?

时间:2010-08-13 09:08:44

标签: sql visual-studio-2008 sql-server-2005

我有两张很棒的桌子,我想用它作为更大的第三张桌子的基础!

输出取决于日期间隔,如09/01/2010 - 09/03/2010

来自表A <的输出示例 - - (已分配测试人员)

Date            Country        Allocated testers
09/01/2010      Nigeria           0
09/02/2010      Nigeria           1
09/03/2010      Nigeria         134

09/01/2010      China             2
09/02/2010      China             0
09/03/2010      China            14

09/01/2010      Chile             3
09/02/2010      Chile             4
09/03/2010      Chile             0

*************

来自表B 的输出示例 - - (缺席测试人员)

Date            Country        Absent testers
09/01/2010      Nigeria           0
09/02/2010      Nigeria           7
09/03/2010      Nigeria           0

09/01/2010      China             2
09/02/2010      China             0
09/03/2010      China             0

09/01/2010      Chile             1
09/02/2010      Chile             0
09/03/2010      Chile             0

*************

表C中的WANTED输出示例(已分配和未分配的测试人员)

Date            Country        Allocated testers    Absent testers
09/01/2010      Nigeria           0                 0
09/02/2010      Nigeria           1                 7
09/03/2010      Nigeria         134                 0

09/01/2010      China             2                 2
09/02/2010      China             0                 0
09/03/2010      China            14                 0

09/01/2010      Chile             3                 2
09/02/2010      Chile             4                 0
09/03/2010      Chile             0                 0

这里有一些SQL代码生成上面显示的输出......(是的,它们可以工作)

表A

WITH Calendar AS (SELECT     CAST(@StartDate AS datetime) AS Date
     UNION ALL
     SELECT     DATEADD(d, 1, Date) AS Expr1
     FROM         Calendar AS Calendar_1
     WHERE     (DATEADD(d, 1, Date) < @EndDate))
    SELECT     C.Date, C2.Country, COALESCE (SUM(R.[Amount of people per day needed]), 0) AS [Allocated testers]
     FROM         Calendar AS C CROSS JOIN
                            Country AS C2 LEFT OUTER JOIN
                            Requests AS R ON C.Date BETWEEN R.[Start date] AND R.[End date] AND R.CountryID = C2.CountryID
     GROUP BY C.Date, C2.Country OPTION (MAXRECURSION 0)

表B

WITH Calendar AS (SELECT     CAST(@StartDate AS datetime) AS Date
     UNION ALL
     SELECT     DATEADD(d, 1, Date) AS Expr1
     FROM         Calendar AS Calendar_1
     WHERE     (DATEADD(d, 1, Date) < @EndDate))
    SELECT     C.Date, C2.Country, COALESCE (COUNT(PA.PeopleID), 0) AS [Absent testers]
     FROM         Calendar AS C CROSS JOIN
                            Country AS C2 INNER JOIN
                            Roles AS R INNER JOIN
                            People AS P ON R.RolesID = P.RolesID ON C2.CountryID = P.CountryID LEFT OUTER JOIN
                            PeoplesAbsence AS PA ON C.Date BETWEEN PA.StartDate AND PA.EndDate AND P.PeopleID = PA.PeopleID
     WHERE     (R.Role = 'Tester')
     GROUP BY C.Date, C2.Country OPTION (MAXRECURSION 0)

表C 是我需要帮助创建的: - )

注意:我想看一些示例代码,开始使用!

另请注意:我不想用SQL Server中的Views解决这个问题,因为它无法工作...... - 我需要3:rd表; - )


已解决! tdammers 获得解决方案(谢谢!),这就是实施时的样子:

WITH Calendar AS (SELECT     CAST(@StartDate AS datetime) AS Date
UNION ALL
SELECT
    DATEADD(d, 1, Date) AS Expr1
FROM
    Calendar AS Calendar_1
WHERE
    (DATEADD(d, 1, Date) < @EndDate))


SELECT a.Date, a.Country, a.[Allocated testers], b.[Absent testers] FROM ( SELECT
    C.Date, C2.Country, COALESCE (SUM(R.[Amount of people per day needed]), 0) AS [Allocated testers]
FROM
    Calendar AS C
CROSS JOIN
    Country AS C2
LEFT OUTER JOIN
    Requests AS R 
    ON
        C.Date BETWEEN R.[Start date] AND R.[End date] AND R.CountryID = C2.CountryID
GROUP BY
    C.Date, C2.Country ) as a LEFT OUTER JOIN ( SELECT     C.Date, C2.Country, COALESCE (COUNT(PA.PeopleID), 0) AS [Absent testers]
     FROM         Calendar AS C CROSS JOIN
                            Country AS C2 INNER JOIN
                            Roles AS R INNER JOIN
                            People AS P ON R.RolesID = P.RolesID ON C2.CountryID = P.CountryID LEFT OUTER JOIN
                            PeoplesAbsence AS PA ON C.Date BETWEEN PA.StartDate AND PA.EndDate AND P.PeopleID = PA.PeopleID
     WHERE     (R.Role = 'Tester')
     GROUP BY C.Date, C2.Country ) as b ON a.date = b.date AND a.country = b.country

1 个答案:

答案 0 :(得分:2)

表C不应该是表格,而是视图。