0

我正在尝试添加列中的所有值以获得该列的总数,有人知道这个技巧吗?

这是我的查询:

select T.*,
sum(nvl(BIN1,0))+sum(nvl(BIN2,0))+sum(nvl((BIN3,0)) as TOTAL
from
(
  SELECT CSM_FLASKS.FLASK_CODE,
    MN_ASSAYS_STAGES.ASSAY_STAGE_ID,
    VITRO_REP.TREATMENT_DOSE(CSM_FLASKS.GROUP_ID)                                               AS TREATMENT_DOSE,
    MN_RI_REP.FLASK_BIN_VALUE_BY_INDEX(MN_ASSAYS_STAGES.ASSAY_STAGE_ID, CSM_FLASKS.FLASK_ID, 1) AS BIN1,
    MN_RI_REP.FLASK_BIN_VALUE_BY_INDEX(MN_ASSAYS_STAGES.ASSAY_STAGE_ID, CSM_FLASKS.FLASK_ID, 2) AS BIN2,
    MN_RI_REP.FLASK_BIN_VALUE_BY_INDEX(MN_ASSAYS_STAGES.ASSAY_STAGE_ID, CSM_FLASKS.FLASK_ID, 3) AS BIN3,
    MN_RI_REP.CBPI_FLASK(MN_ASSAYS_STAGES.ASSAY_STAGE_ID, CSM_FLASKS.FLASK_ID, 1, 2, 2)         AS CBPI,
    CSM_EXPERIMENTS.EXPT_CODE
  FROM CSM_TREATMENT_GROUPS_EXPTS
  INNER JOIN CSM_ASSAYS
  ON CSM_ASSAYS.EXPT_ID = CSM_TREATMENT_GROUPS_EXPTS.EXPERIMENT_ID
  INNER JOIN CSM_FLASKS
  ON CSM_TREATMENT_GROUPS_EXPTS.GROUP_ID = CSM_FLASKS.GROUP_ID
  INNER JOIN MN_ASSAYS
  ON CSM_ASSAYS.ASSAY_ID = MN_ASSAYS.ASSAY_ID
  INNER JOIN MN_ASSAYS_STAGES
  ON MN_ASSAYS.MN_ASSAY_ID = MN_ASSAYS_STAGES.MN_ASSAY_ID
  INNER JOIN CSM_EXPERIMENTS
  ON CSM_EXPERIMENTS.EXPT_ID    = CSM_TREATMENT_GROUPS_EXPTS.EXPERIMENT_ID
  AND CSM_EXPERIMENTS.EXPT_ID   = CSM_ASSAYS.EXPT_ID
  WHERE CSM_ASSAYS.ASSAY_ID     = 1000060
  AND MN_ASSAYS_STAGES.STAGE_ID = 2
  ORDER BY CSM_TREATMENT_GROUPS_EXPTS.ORDER_INDEX,
    CSM_FLASKS.FLASK_ID,
    CSM_FLASKS.IS_PLUS DESC
) T

错误:ORA-00937:不是单组组函数 00937。00000 -“不是单组组函数” *原因:
*操作:行错误:1 列:9

4

2 回答 2

3

使用任何聚合函数时,您需要将非聚合字段添加到GROUP BY子句中。因此,在您的列中,您需要列出子句中聚合函数SUM中未列出的那些字段。GROUP BY因此,您的查询应如下所示:

SELECT FLASK_CODE,
SUM(nvl(BIN1, 0)) + SUM(nvl(BIN2, 0)) + SUM(nvl((BIN3, 0)) AS TOTAL
FROM
(
  ...

) T
GROUP BY FLASK_CODE

这只是一个例子。但是您必须确定要在SELECT子句中选择哪些字段,并且也需要在GROUP BY子句中列出。

于 2012-10-18T11:36:07.347 回答
1

试试这个:如果您在单列中添加所有数据意味着:

select sum( column_name ) from table inner join .........;

最后你找到总数

于 2014-01-31T07:58:33.903 回答