我如何从此表中获得这些结果?我不完全确定它是否应该是一个联盟或加入。任何帮助将不胜感激。
表格
WorkOrderID Activity PlannedDuration ActualDuration
11 Activity 1 4 3.5
11 Activity 2 4 3.6
11 Activity 3 4 5
11 Activity 4 8 7.2
11 Activity 5 6 6
22 Activity 1 4 3.5
22 Activity 2 4 4
22 Activity 3 4 3.9
22 Activity 4 8 12
22 Activity 6 3 3
结果
Activity ActualDuration11 ActualDuration22
Activity 1 3.5 3.5
Activity 2 3.6 4
Activity 3 5 3.9
Activity 4 7.2 12
答案 0 :(得分:0)
试试这个:
select foo.Activity, foo.ActualDuration as ActualDuration11, foobar.ActualDuration as ActualDuration22
from (select Activity, ActualDuration from TABLENAME where WorkOrderID = 11) as foo
join (Select Activity, ActualDuration from TABLENAME where WorkOrderID = 22) as foobar on (foo.Activity = foobar.Activity);
它使用join。你可能需要调整它(添加一个分组等等)以使它适用于你的实际数据库,但这应该是一个很好的框架。
在我的测试用例中,上面的查询返回了以下内容:
Activity | ActualDuration11 | ActualDuration12
------------+------------------+-----------------
Activity 1 | 3.5 | 3.5
Activity 2 | 3.6 | 4
Activity 3 | 5 | 3.9
(3 rows)
希望它有所帮助!
答案 1 :(得分:0)
您可以通过加入同一个表中的两个SELECT
语句并加入它们来实现此目的。查询可能如下所示:
SELECT * FROM
(SELECT Activity, PlannedDuration, ActualDuration as ActualDuration11
FROM YOUR_TABLE
WHERE WorkOrderID = 11) t1
JOIN
(SELECT Activity, PlannedDuration, ActualDuration as ActualDuration22
FROM YOUR_TABLE
WHERE WorkOrderID = 22) t2
ON t1.Activity = t2.Activity;