查询来自表和另一个表中多行的总和

时间:2014-06-27 04:38:48

标签: sql oracle select group-by sum

我有一个关于SQL查询的问题,我一直收到错误。因此,我想要做的是从MASTER_DRAWING TABLE中的一行获取一个QTY值,并将其与相同的名称进行比较,但使用相同的键HEAD_MARK进行多次出现的总和。

这是我的SQL

SELECT MASTER_DRAWING.TOTAL_QTY AS TOTALQTY, 
       SUM(MASTER_DRAWING_ASSIGNED.ASSIGNED_QTY) AS SUMASSIGNEDQTY 
FROM MASTER_DRAWING, MASTER_DRAWING_ASSIGNED 
WHERE HEAD_MARK = 'TESTMULTIPLE' AND PROJECT_NAME = 'MILLHOUSE'

我一直得到的是' ORA-00918:列模糊定义'

我在这里做错了吗?最后,我需要将TOTALQTY与SUMASSIGNEDQTY进行比较以进行进一步的操作

这是tabe结构

MASTER_DRAWING,

  HEAD_MARK           VARCHAR2(20 CHAR),
  ENTRY_DATE          DATE,
  COMP_TYPE           VARCHAR2(20 CHAR),
  WEIGHT              NUMBER(11,2),
  SURFACE             NUMBER(15,3),
  PROFILE             VARCHAR2(20 CHAR),
  PROJECT_NAME        VARCHAR2(20 CHAR),
  LENGTH              NUMBER(15,2)              NOT NULL,
  TOTAL_QTY           NUMBER(38),
  SUBCONT_STATUS      VARCHAR2(20 CHAR),
  DISTRIBUTION_COUNT  NUMBER

和MASTER_DRAWING_ASSIGNED,

  ASSIGNED_WEIGHT    NUMBER(12,3),
  ASSIGNED_QTY       NUMBER(38),
  SURFACE            NUMBER(15,3),
  SUBCONT_ID         VARCHAR2(20 CHAR),
  REVISION_NO        NUMBER(2),
  ASSIGNMENT_DATE    DATE,
  PROJECT_NAME       VARCHAR2(20 CHAR),
  ID                 NUMBER(3),
  HEAD_MARK          VARCHAR2(20 CHAR),
  ASSIGNED_DUE_DATE  DATE,
  COMP_TYPE          VARCHAR2(25 CHAR),
  SIGNATURE          VARCHAR2(25 CHAR)

1 个答案:

答案 0 :(得分:1)

试试这个:

SELECT MD.TOTAL_QTY AS TOTALQTY, MDA.SUMASSIGNEDQTY 
FROM MASTER_DRAWING MD 
INNER JOIN (SELECT MDA.HEAD_MARK, MDA.PROJECT_NAME, SUM(MDA.ASSIGNED_QTY) AS SUMASSIGNEDQTY 
            FROM MASTER_DRAWING_ASSIGNED MDA 
            GROUP BY MDA.HEAD_MARK, MDA.PROJECT_NAME
          ) MDA ON MD.HEAD_MARK = MDA.HEAD_MARK AND MD.PROJECT_NAME = MDA.PROJECT_NAME 
WHERE MD.HEAD_MARK = 'TESTMULTIPLE' AND MD.PROJECT_NAME = 'MILLHOUSE';