SQL Group by Query with join

时间:2012-05-10 23:16:27

标签: sql group-by pivot

Please click here for sample tables and description.

根据附图,我有三张桌子'PROJECTS','PROJECTMANAGER'和'MANAGERS'。

我需要一个查询,可以列出由PROJECT STATUS分组的“PRODUCT”类型管理器管理的PROJECTS。 如果STATUS = 2,则显示为已完成项目或正在进行中项目。

结果表应如附图所示。 期望的结果:http://www.dbasupport.com/forums/attachment.php?attachmentid=588&d=1336691473

我需要的查询应该是通用的,以便它可以在任何数据库中使用(MySQL / Oracle / MSSQL / DB2)

请帮助。

顺便说一句,这不是作业...... !!!! 我用了样本表。

我曾尝试使用case语句但不知道如何加入并同时使用group by。

CREATE TABLE PROJECTS 
(       
    PROJECT_ID  varchar(20), 
    PROJECT_NAME  varchar(30),
    STATUS int
);

CREATE TABLE PROJECTMANAGER
(
    PROJECT_ID  varchar(20), 
    MANAGER_ID varchar(20)
);

CREATE TABLE MANAGERS
(           
    MANAGER_ID  varchar(20), 
    MANAGER_NAME  varchar(20), 
    TYPE varchar(20)
);


INSERT INTO PROJECTS (PROJECT_ID, PROJECT_NAME, STATUS) VALUES
    ('project_001', 'Project 001', 0),
    ('project_002', 'Project 002', 1),
    ('project_003', 'Project 003', 2),
    ('project_004', 'Project 004', 0),
    ('project_005', 'Project 005', 2),
    ('project_006', 'Project 006', 0),
    ('project_007', 'Project 007', 1);


INSERT INTO PROJECTMANAGER (PROJECT_ID , MANAGER_ID) VALUES
    ('project_001', 'mgr_001'),
    ('project_002', 'mgr_001'),
    ('project_001', 'mgr_002'),
    ('project_002', 'mgr_003'),
    ('project_001', 'mgr_003'),
    ('project_005', 'mgr_001'),
    ('project_004', 'mgr_002');

INSERT INTO MANAGERS (MANAGER_ID, MANAGER_NAME, TYPE) VALUES
    ('mgr_001', 'Manager 001', 'PRODUCT'),
    ('mgr_002', 'Manager 002', 'HR'),
    ('mgr_003', 'Manager 003', 'PRODUCT'),
    ('mgr_004', 'Manager 004', 'FINANCE'),
    ('mgr_005', 'Manager 005', 'PRODUCT');



Resulted Table:

MANAGER_ID  | MANAGER _NAME | COMPLETED_PROJECTS |  IN_PROGRESS_PROJECTS |
 mgr_001    | Manager 001   |        1       |            2          |
 mgr_003    | Manager 003   |        0       |            1          |
 mgr_005    | Manager 005   |        0       |            0          |

2 个答案:

答案 0 :(得分:2)

由于我看不到你想要的最终结果(没有张贴图片),你可以这样做:

select p.project_id
  , p.project_name
  , m.manager_name
  , case when p.status = 2 then 'completed' else 'in progress' end as projectstatus
from projects p
left join projectmanager pm
  on p.project_id = pm.project_id
left join managers m
  on pm.manager_id = m.manager_id
WHERE m.type = 'Product'

SQL Fiddle with Demo

我认为你想要以下内容:

SELECT *
  FROM
  (
    select m.manager_id
      , m.manager_name
      , status
      , case when status = '2' then 'Completed' else 'in progress' end as pstatus
    from projects p
    left join projectmanager pm
      on p.project_id = pm.project_id
    left join managers m
      on pm.manager_id = m.manager_id
    WHERE m.type = 'Product'
  ) x
  pivot
  (
    count(status)
    for pstatus in ([completed], [in progress])
  ) p

DEMO

答案 1 :(得分:0)

您需要将这些表与INNER JOIN一起使用(Project to Projectmanager使用project_id,projectmanager使用manager_id进行管理)。

然后将它们与GROUP BY一起分组

另请阅读T-SQL CASE语句。您需要它(或IF)将STATUS字段转换为“已完成”或“未完成”。