【问题标题】:Group by a CAST field in the select section?按选择部分中的 CAST 字段分组?
【发布时间】: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


【解决方案1】:

如果您要完成的是在 Group By 子句中转换表达式,我认为这是不允许的。但是,您可能能够通过使用分区来实现所需的结果。我已经稍微修改了代码.....

select
feature_type.feature_type_name,
sum (feat_measurement.feature_quantity) OVER (Partition 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

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 
feat_measurement.feature_quantity,
feat_attrib_type.attrib_value_code,
feature_type.feature_type_name

order by
feature_type.feature_type_name

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-08
    • 2017-06-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多