我需要一个帮助,查询总结果与另一个表的参考 所以我有两个表,MASTER_DRAWING_ASSIGNED和FABRICATION
MASTER_DRAWING_ASSIGNED,
HEAD_MARK SUBCONT_ID PROJECT_NAME
---------------------------------
HEADMARKA BOBBY PROCESSHOUSE
HEADMARKB BOBBY PROCESSHOUSE
HEADMARKC ERIC PROCESSHOUSE
FABRICATION
HEAD_MARK CURRENT_WEIGHT PROJECT_NAME
-------------------------------------
HEADMARKA 19.5 PROCESSHOUSE
HEADMARKA 23.4 PROCESSHOUSE
HEADMARKA 11.2 PROCESSHOUSE
HEADMARKB 23.3 PROCESSHOUSE
HEADMARKB 10.9 PROCESSHOUSE
HEADMARKC 11.0 PROCESSHOUSE
HEADMARKC 10.5 PROCESSHOUSE
所以有一天,我想知道BOBBY从FABRICATION当前体重中提取的总重量是多少。我发现这样做有些困难。请帮帮我
我的查询就像这样
SELECT SUM(CURRENT_WEIGHT)
, MASTER_DRAWING_ASSIGNED.SUBCONT_ID
FROM FABRICATION
, MASTER_DRAWING_ASSIGNED
WHERE MASTER_DRAWING_ASSIGNED.HEAD_MARK = FABRICATION.HEAD_MARK
MASTER_DRAWING_ASSIGNED.PROJECT_NAME = 'PROCESSHOUSE'
AND MASTER_DRAWING_ASSIGNED.SUBCONT_ID = 'BOBBY'
答案 0 :(得分:1)
我认为,你有一个错字和一个错过的GROUP BY
条款。此外,您最好使用JOIN
子句和表别名。
SELECT SUM(FABRICATION.CURRENT_WEIGHT),
MASTER_DRAWING_ASSIGNED.SUBCONT_ID
FROM FABRICATION,
MASTER_DRAWING_ASSIGNED
WHERE MASTER_DRAWING_ASSIGNED.HEAD_MARK = FABRICATION.HEAD_MARK AND MASTER_DRAWING_ASSIGNED.PROJECT_NAME = 'PROCESSHOUSE'
AND MASTER_DRAWING_ASSIGNED.SUBCONT_ID = 'BOBBY'
GROUP BY MASTER_DRAWING_ASSIGNED.SUBCONT_ID
答案 1 :(得分:1)
如果你想总和头部标记的总重量:
select subcont_id, project_name, head_mark, total_weight
from master_drawing_assigned m
left join
(select head_mark hm, sum(current_weight) total_weight
from fabrication group by head_mark) f
on m.head_mark=f.hm
where subcont_id='BOBBY' order by head_mark;
如果你想要总计一个项目的总重量:
select subcont_id, project_name, sum(total_weight) total_weight
from master_drawing_assigned m
left join
(select head_mark hm, sum(current_weight) total_weight
from fabrication group by head_mark) f
on m.head_mark=f.hm
where subcont_id='BOBBY' group by subcont_id, project_name;
您可以在 this fiddle
中查看结果