查询计数有三个连接表

时间:2012-10-05 21:56:52

标签: sql

我整个下午都试图解决这个问题。我甚至不确定当前的数据库结构是否可行。我已经尝试了以下声明,但它没有返回我需要的内容。下面的语句的问题是它只返回projectType(如果存在)。如果projectType不存在,我需要返回零。非常感谢任何帮助。

SELECT r.region, ISNULL(o.totalcount,0) AS TotalCount, o.projectType
  FROM region r 
  LEFT JOIN (
    SELECT COUNT(o2.id) AS TotalCount, o2.regionID, t2.projectType
      FROM projects o2 
      RIGHT JOIN projectTypes t2 on o2.typeID = t2.id
      GROUP BY o2.regionId, t2.projectType
  ) o ON r.id = o.regionId
  ORDER by r.Region
regions:
-------------------------
id  | region    |
-------------------------
1   | East      |
-------------------------
2   | MidWest   |
-------------------------
3   | West      |
-------------------------


Project Type:
-------------------------
id  | projectType   | 
-------------------------
1   | Web Desgin    |
-------------------------
2   | Database  |
-------------------------
3   | Development   |
-------------------------


Projects:
-------------------------------------------------------------------------
id  | projectName   | regionID  | projectTypeID | postedOn  |
-------------------------------------------------------------------------
1   | Project 1 | 1     | 2     | 2012-09-02    |
-------------------------------------------------------------------------
2   | Project 2 | 2     | 2     | 2012-09-02    |
-------------------------------------------------------------------------
3   | Project 3 | 1     | 1     | 2012-09-02    |
-------------------------------------------------------------------------
4   | Project 4 | 3     | 2     | 2012-09-02    |
-------------------------------------------------------------------------
5   | Project 5 | 3     | 1     | 2012-10-02    |
-------------------------------------------------------------------------
6   | Project 6 | 3     | 2     | 2012-10-02    |
-------------------------------------------------------------------------
7   | Project 7 | 3     | 3     | 2012-10-02    |
-------------------------------------------------------------------------
8   | Project 8 | 2     | 3     | 2012-10-02    |
-------------------------------------------------------------------------
9   | Project 9 | 1     | 2     | 2012-10-02    |
-------------------------------------------------------------------------
10  | Project 10    | 1     | 2     | 2012-10-02    |
-------------------------------------------------------------------------



Desired Results:
-----------------------------------------
Region  | project Type  | totalCount    |
-----------------------------------------
East    | Web Desgin    | 1     | 
-----------------------------------------
East    | Database  | 3     |
-----------------------------------------
East    | Development   | 0     |
-----------------------------------------
Midwest | Web Desgin    | 0     | 
-----------------------------------------
Midwest | Database  | 2     |
-----------------------------------------
Midwest | Development   | 1     |
-----------------------------------------
West    | Web Desgin    | 1     | 
-----------------------------------------
West    | Database  | 2     |
-----------------------------------------
West    | Development   | 1     |
-----------------------------------------

2 个答案:

答案 0 :(得分:4)

http://sqlfiddle.com/#!3/6680f/1

SELECT r.region
  , pt.projectType 
  , count(p.id)
FROM region r cross join ProjectTypes pt

  left join projects p on p.regionID = r.id and pt.id = p.TypeID

group by r.region, pt.projectType
order by r.region

答案 1 :(得分:1)

为什么不

SELECT regions.region, projectTypes.projectType,
       (SELECT count(*) FROM projects
        WHERE projects.typeID = projectTypes.id
          AND projects.regionID = regions.id) as totalCount
FROM regions, projectTypes