gpt4 book ai didi

sql - oracle - 案例陈述和分组依据

转载 作者:行者123 更新时间:2023-12-02 17:28:25 27 4
gpt4 key购买 nike

您好 – 我需要您协助解决以下问题。我正在尝试运行下面的查询以进行分组,但出现以下错误:

ORA-00904:” SALES_ID_CO”:invalid identifier

你能帮忙吗?非常感谢。

Select
count(1),
sales_date,
CASE
WHEN sales_id like '1%'
THEN substr(sales_id,2,6)
WHEN sales_id like '3%'
THEN substr(sales_id ,2,6)
WHEN sales_id like '4%'
THEN substr(sales_id ,2,6)
WHEN sales_id like '5%'
THEN substr(sales_id ,2,6)
WHEN sales_id like '9%'
THEN substr(sales_id ,1,6)
WHEN sales_id like '70%' OR sales_id like '90%' OR sales_id like '31%'
THEN substr(sales_id ,2,6)
END AS SALES_ID_CO,

case
WHEN sales_branch like '10%' AND sales_code<> '678 '
THEN substr(sales_branch,2,6)
WHEN sales_branch like '200%' AND sales_code<> '678 '
THEN substr(sales_branch,2,6)
WHEN sales_branch like '300%' AND sales_code<> '678 '
THEN substr(sales_branch,2,6)
WHEN sales_branch like '456%' AND sales_code<> '678 '
THEN substr(sales_branch,2,6)
END AS SALES_BRANCH

from tbl_sales_cde

where sales_country IN (‘USA’,’ASIA’,’EU’)
group by SALES_ID_CO, SALES_BRANCH,SALES_DATE

最佳答案

Oracle 不支持 group by 子句中的列别名。使用子查询:

SELECT SALES_ID_CO, SALES_BRANCH,SALES_DATE, COUNT(*)
FROM (Select sales_date,
(CASE WHEN sales_id like '1%'
THEN substr(sales_id,2,6)
WHEN sales_id like '3%'
THEN substr(sales_id ,2,6)
WHEN sales_id like '4%'
THEN substr(sales_id ,2,6)
WHEN sales_id like '5%'
THEN substr(sales_id ,2,6)
WHEN sales_id like '9%'
THEN substr(sales_id ,1,6)
WHEN sales_id like '70%' OR sales_id like '90%' OR sales_id like '31%'
END) AS SALES_ID_CO,
(case WHEN sales_branchlike '10%' AND sales_code<> '678 '
THEN substr(sales_branch,2,6)
WHEN sales_branchlike '200%' AND sales_code<> '678 '
THEN substr(sales_branch,2,6)
WHEN sales_branchlike '300%' AND sales_code<> '678 '
THEN substr(sales_branch,2,6)
WHEN sales_branchlike '456%' AND sales_code<> '678 '
THEN substr(sales_branch,2,6)
END) AS SALES_BRANCH
from tbl_sales_cde
where sales_country IN ('USA', 'ASIA', 'EU')
) s
group by SALES_ID_CO, SALES_BRANCH,SALES_DATE;

注意:您在 where 子句中也有单引号,但这可能是一个复制问题。

关于sql - oracle - 案例陈述和分组依据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36705988/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com