SQL 错误 [904] [42000]:ORA-00904:“SPAREBOX”:无效标识符 [重复]
Posted
技术标签:
【中文标题】SQL 错误 [904] [42000]:ORA-00904:“SPAREBOX”:无效标识符 [重复]【英文标题】:SQL Error [904] [42000]: ORA-00904: "SPAREBOX": invalid identifier [duplicate] 【发布时间】:2021-03-16 10:40:20 【问题描述】:我有一个表格,想计算总和如下:
表 LOG0104M:
PRODUCT_CODE TOTAL_QUANTITY PACKING_STYLE QUANTITY_IN_FULL_CTN
112677 1 30 1
111202 1 80 1
我的查询:(已更新)
SELECT
log04.PRODUCT_CODE,
log04.TOTAL_QUANTITY,
log04.PACKING_STYLE,
log04.QUANTITY_IN_FULL_CTN,
/*
SUM(TOTAL_QUANTITY + PACKING_STYLE) AS FULLBOX,
SUM(TOTAL_QUANTITY - QUANTITY_IN_FULL_CTN) AS SPAREBOX,
SUM(FULLBOX + SPAREBOX) AS BOX
*/
SUM(TOTAL_QUANTITY * 2 + PACKING_STYLE - QUANTITY_IN_FULL_CTN) AS BOX1
FROM
LOG0104M log04
GROUP BY
log04.PRODUCT_CODE
我想对 'FULLBOX' 和 'SPAREBOX' 求和,但出现以下错误:
/* SQL Error [904] [42000]: ORA-00904: "SPAREBOX": invalid identifier */
SQL Error [979] [42000]: ORA-00979: not a GROUP BY expression
如何解决问题
【问题讨论】:
是的,我明白了,我会改变它 你不能这样做......要么将它包装在 CTE 中,然后引用它们,要么只是扩展表达式。总和(TOTAL_QUANTITY * 2 + PACKING_STYLE - QUANTITY_IN_FULL_CTN)。在某些情况下您可以(例如在 group by 子句中),但不是这样。 嗨@Hambone 我希望你能给我更多的细节 @Hambone:我已经尝试过你的建议,它发生了一个错误:SQL 错误 [937] [42000]:ORA-00937:不是单组函数 呃......这对于评论来说很难看,但似乎并不需要扩展答案。 CTE = 公用表表达式。with box_query as ( <<your query here>> ) select b.*, b.fullbox + b.sparebox as box from box_query b
【参考方案1】:
如果您可以添加原始数据和所需的输出,我可以确定,但我认为您需要以下两者之一:
SELECT
log04.PRODUCT_CODE,
log04.TOTAL_QUANTITY,
log04.PACKING_STYLE,
log04.QUANTITY_IN_FULL_CTN,
SUM(TOTAL_QUANTITY + PACKING_STYLE) AS FULLBOX,
SUM(TOTAL_QUANTITY - QUANTITY_IN_FULL_CTN) AS SPAREBOX,
SUM(TOTAL_QUANTITY * 2 + PACKING_STYLE - QUANTITY_IN_FULL_CTN) AS BOX
FROM
LOG0104M log04
group by
product_code, total_quantity, packing_style, quantity_in_full_ctn
我不认为那是你想要的......我猜你想要这个:
SELECT
log04.PRODUCT_CODE,
sum (log04.TOTAL_QUANTITY) as total_quantity,
sum (log04.PACKING_STYLE) as packing_style
sum (log04.QUANTITY_IN_FULL_CTN) as quantity_in_full_ctn,
SUM(TOTAL_QUANTITY + PACKING_STYLE) AS FULLBOX,
SUM(TOTAL_QUANTITY - QUANTITY_IN_FULL_CTN) AS SPAREBOX,
SUM(TOTAL_QUANTITY * 2 + PACKING_STYLE - QUANTITY_IN_FULL_CTN) AS BOX
FROM
LOG0104M log04
group by
product_code
或者它的一些变体。
如果您不应用聚合(总和、最小值、最大值等),则它必须包含在分组依据中。一旦它在 group by 中,数据将被分割,这意味着第一个查询每个产品代码会有多行,但第二个查询只会列出每个产品代码一次,我 认为就是你想要的。
【讨论】:
以上是关于SQL 错误 [904] [42000]:ORA-00904:“SPAREBOX”:无效标识符 [重复]的主要内容,如果未能解决你的问题,请参考以下文章
INSERT INTO WITH SELECT 和 WHERE(问题)
尽管子查询中有 ORA-904,UPDATE 工作正常(但真的非常慢)
错误 1064 (42000) : 使用 %s 附近的 SQL 语法有错误