我有5列:c1 = Item,c2 = LHrs,c3 = WHrs,c4 = Lead,c5 = Worker,构造如下:
ITEM LHRS WHRS LEAD WORKER
123 25 10 John Gary
456 15 20 John John
我希望结果看起来像这样:
RESOURCE ITEM HRS
John 123 25
Gary 123 10
John 456 35
连接名称并执行结果“AS Resource”不会按照我想要的方式分解它。基本上我想知道每个人,每个项目,他们工作了多少小时,他们是铅(LHrs)或工人(WHrs)。
思想?
答案 0 :(得分:3)
UNION查询怎么样?
SELECT item, person, sum(hrs) AS sumhours
FROM (
SELECT Item, lhrs as hrs,lead as person from wk
UNION ALL
SELECT Item, whrs, worker AS person from wk) a
GROUP BY item, person
其中wk是表的名称。