无效的请求“分组依据”(oracle)
Posted
技术标签:
【中文标题】无效的请求“分组依据”(oracle)【英文标题】:Invalid request 'group by' (oracle) 【发布时间】:2021-12-17 03:26:25 【问题描述】:请告诉我如何修复错误。一个错误Expression not in GROUP BY key 'isin'
。
我了解我的分组不正确,但我不知道如何正确重做此请求的代码。这里需要找到end_circ
的最大值和stocks_full_id
键的最小值begin_circ
。必须显示来自select
的所有列以及max
和min
。
SELECT a.isin as id,
a.state_number as number,
a.update_time as valid_from_date,
'2999-12-31 00:00:00' as valid_to_date,
a.operdate as oper,
a.inn as inn_num,
a.name_eng as name,
coalesce(ts.full_name_eng,a.name_eng) as full_nm,
max (stg.end_circ) as end_date,
min (stg.begin_circ) as start_date,
case when sk.name_eng IS NULL then sk.name_uk else sk.name_eng end as subtype_nm
FROM (SELECT s.*, rank() over (PARTITION BY isin,state_number ORDER BY operdate desc) as rn
FROM stocks s
WHERE isin IS NOT NULL and state_number IS NOT NULL) a
JOIN trading_stocks ts ON ts.emission_is=a.id
JOIN stocks_trading_grounds stg ON stg.stocks_full_id=a.id
JOIN stocks_kinds sk ON sk.id=a.kind_id
WHERE stg.end_circ >= "2021-01-01 00:00:00" and a.rn=1
GROUP BY stg.stocks_full_id
【问题讨论】:
sn-p不需要你的代码,请编辑你的代码 【参考方案1】:您必须在 group by 子句中包含所有非聚合列。所以你更新的查询将是 -
SELECT a.isin as id,
a.state_number as number,
a.update_time as valid_from_date,
'2999-12-31 00:00:00' as valid_to_date,
a.operdate as oper,
a.inn as inn_num,
a.name_eng as name,
coalesce(ts.full_name_eng,a.name_eng) as full_nm,
max (stg.end_circ) as end_date,
min (stg.begin_circ) as start_date,
case when sk.name_eng IS NULL then sk.name_uk else sk.name_eng end as subtype_nm
FROM (SELECT s.*, rank() over (PARTITION BY isin,state_number ORDER BY operdate desc) as rn
FROM stocks s
WHERE isin IS NOT NULL and state_number IS NOT NULL) a
JOIN trading_stocks ts ON ts.emission_is=a.id
JOIN stocks_trading_grounds stg ON stg.stocks_full_id=a.id
JOIN stocks_kinds sk ON sk.id=a.kind_id
WHERE stg.end_circ >= '2021-01-01 00:00:00' and a.rn=1
GROUP BY a.isin,
a.state_number,
a.update_time,
a.operdate,
a.inn,
a.name_eng,
coalesce(ts.full_name_eng,a.name_eng),
case when sk.name_eng IS NULL then sk.name_uk else sk.name_eng end;
【讨论】:
【参考方案2】:GROUP BY
用于JOIN
中的单个表:
SELECT a.isin as id,
a.state_number as number,
a.update_time as valid_from_date,
'2999-12-31 00:00:00' as valid_to_date,
a.operdate as oper,
a.inn as inn_num,
a.name_eng as name,
coalesce(ts.full_name_eng,a.name_eng) as full_nm,
stg.end_date,
stg.start_date,
case when sk.name_eng IS NULL then sk.name_uk else sk.name_eng end as subtype_nm
FROM (SELECT s.*,
rank() over (PARTITION BY isin,state_number ORDER BY operdate desc) as rn
FROM stocks s
WHERE isin IS NOT NULL
and state_number IS NOT NULL
) a
JOIN trading_stocks ts ON ts.emission_is=a.id
JOIN stocks_kinds sk ON sk.id=a.kind_id
JOIN (
SELECT stocks_full_id,
max(stg.end_circ) as end_date,
min(stg.begin_circ) as start_date
FROM stocks_trading_grounds
GROUP BY stocks_full_id
) stg
ON stg.stocks_full_id=a.id
WHERE stg.end_date >= DATE '2021-01-01'
AND a.rn=1
此外,双引号不用于字符串文字;它们用于带引号的标识符。使用单引号 '2021-01-01 00:00:00'
表示字符串文字或使用 DATE '2021-01-01'
表示日期文字。
【讨论】:
以上是关于无效的请求“分组依据”(oracle)的主要内容,如果未能解决你的问题,请参考以下文章