根据附加图像,我有三个表“PROJECTS”、“PROJECTMANAGER”和“MANAGERS”。
我需要一个查询,它可以列出由“产品”类型管理器管理的项目,这些管理器与项目状态分组。如果 STATUS=2 则显示为 Completed Projects 或 In Progress Projects。
结果表应如附图所示。期望的结果:http ://www.dbasupport.com/forums/attachment.php?attachmentid=588&d=1336691473
我需要的查询应该是通用的,以便可以在任何数据库(MySQL/Oracle/MSSQL/DB2)中使用
请帮忙。
顺便说一句,这不是作业......!!!!我使用了示例表。
我尝试过 case when 语句,但不知道如何加入并同时使用 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 |