【发布时间】:2020-05-26 17:12:25
【问题描述】:
我的主要 SELECT 部分中有一个字段,我想在我的报告中进行分组。
CAST((SELECT attribute_value.attrib_value_name
FROM attribute_value,
attribute_type
WHERE attribute_type.attrib_type_code = 'SC17'
AND attribute_type.attrib_type_code = attribute_value.attrib_type_code
AND attribute_value.attrib_value_code = feat_attrib_type.attrib_value_code ) as VARCHAR (30)) as ZONE_SPEC,
我尝试将相同的代码放在 SQL 的 group 部分,但它不断返回错误消息
“ORA-00933: SQL 命令未正确结束”
到目前为止我得到的代码是:
select
CAST((SELECT attribute_value.attrib_value_name
FROM attribute_value,
attribute_type
WHERE attribute_type.attrib_type_code = 'SC17'
AND attribute_type.attrib_type_code = attribute_value.attrib_type_code
AND attribute_value.attrib_value_code = feat_attrib_type.attrib_value_code ) as VARCHAR (30)) as ZONE_SPEC,
feature_type.feature_type_name,
sum (feat_measurement.feature_quantity)
from
feature
inner join feature_type on feature.feature_type_code = feature_type.feature_type_code
inner join area on feature.area_code = area.area_code
inner join feat_measurement on feature.plot_number = feat_measurement.plot_number
and feature.site_code = feat_measurement.site_code
inner join measurement_type on feat_measurement.measurement_code = measurement_type.measurement_code
inner join feat_attrib_type on feature.site_code = feat_attrib_type.site_code AND
feature.plot_number = feat_attrib_type.plot_number
where
measurement_type.measurement_code in ('AREA') and
feature.feature_deadflag = 'N'
group by
CAST((SELECT attribute_value.attrib_value_name
FROM attribute_value,
attribute_type
WHERE attribute_type.attrib_type_code = 'SC17'
AND attribute_type.attrib_type_code = attribute_value.attrib_type_code
AND attribute_value.attrib_value_code = feat_attrib_type.attrib_value_code ) as VARCHAR (30)) as ZONE_SPEC,
feature_type.feature_type_name
order by
feature_type.feature_type_name
是否可以将此 CAST 字段添加到按字段分组中?如果有,怎么做?
【问题讨论】:
-
今日提示:切换到现代、明确的
JOIN语法。更容易编写(没有错误),更容易阅读和维护,如果需要更容易转换为外连接。 -
跳过 GROUP BY 的列别名尝试
as ZONE_SPEC。 -
当我尝试 GROUP BY ZONE_SPEC, feature_type.featrue_type_name 我收到错误消息 ORA-00904: "ZONE_SPEC": invalid identidier
-
使用
JOIN。我认为您不能通过子查询进行聚合。
标签: sql oracle group-by casting