我有两张桌子
员工
ID | Name | Department
----------------------
121 |Name1 | dep1
223 |Name2 | dep2
分配
ID | EID|
---------
1 |121
2 |223
3 |121
[为简洁省略了其他栏目]
表分配表明哪个工作分配给了谁.EID是表Employee的foriegn密钥。此外,可以将两个工作分配给同一个员工。
现在我要创建一个像这样的视图
EID | Assigned
--------------
121 |true
333 |false
应根据“分配”表中的条目计算分配的列。
到目前为止,我只是成功创建了这样的视图
EID | Assigned
--------------
121 |2
333 |0
使用命令
CREATE VIEW "AssignmentView" AS
SELECT distinct ID ,(select Count(*)
from Assignment where Assignment.EID = Employee.ID) as Assigned
FROM Employee;
由于
答案 0 :(得分:2)
CASE
表达式和GROUP BY
应该可以解决问题:
Select
e.id,
Case When Count(a.eid) > 0 Then 'true' Else 'false' End As assigned
From employee e
Left Join assignment a On ( a.eid = e.id )
Group By e.id;