我有代码
SELECT TOP (100) PERCENT dbo.census.Group_Code, dbo.census.Gender, SUM(dbo.v_Courses.Dur_In_Hours)
AS SumOf_Dur_In_Hours
FROM dbo.v_Courses INNER JOIN
dbo.census ON dbo.v_Courses.Job_Group_Code = dbo.census.Group_Code
WHERE (dbo.v_Courses.System = 'GEMS') AND (NOT (dbo.v_Courses.Course_ID LIKE 'UPS%')) AND
(dbo.v_Courses.First_Access_date BETWEEN CONVERT(DATETIME, '2012-01-01 00:00:00', 102) AND CONVERT(DATETIME, '2012-12-31 00:00:00',
102))
GROUP BY dbo.census.Group_Code, dbo.census.Gender
ORDER BY dbo.census.Group_Code
其目的是为我提供工作水平和性别的总小时数,但每个性别的工作水平都返回相同的小时数。我无法弄清楚如何将它们分开。
结果看起来如此,但每种性别都没有办法相同的小时数。
Job Gen Hours
11 F 10886.50417
11 M 10886.50417
16 F 33494.17222
16 M 33494.17222
16 U 33494.17222
21 F 19267.76361
21 M 19267.76361
正在导致重复的性别
答案 0 :(得分:1)
如果您在没有该组的情况下运行查询,您是否看到重复的行?如果是这样,你可能在join / where中需要另一个限制。
答案 1 :(得分:1)
你可以在内部联接中包含性别吗? 或者v_courses没有这个字段?
SELECT TOP (100) PERCENT dbo.census.Group_Code, dbo.census.Gender, SUM(dbo.v_Courses.Dur_In_Hours)
AS SumOf_Dur_In_Hours
FROM dbo.v_Courses
INNER JOIN dbo.census
ON dbo.v_Courses.Job_Group_Code = dbo.census.Group_Code
AND dbo.v_Courses.Gender = dbo.census.Gender
WHERE (dbo.v_Courses.System = 'GEMS') AND (NOT (dbo.v_Courses.Course_ID LIKE 'UPS%')) AND
(dbo.v_Courses.First_Access_date BETWEEN CONVERT(DATETIME, '2012-01-01 00:00:00', 102) AND CONVERT(DATETIME, '2012-12-31 00:00:00',
102))
GROUP BY dbo.census.Group_Code, dbo.census.Gender
ORDER BY dbo.census.Group_Code