我正在使用Oracle。我正在尝试写这样的东西:
SELECT trans_type,
(SELECT parameter_value FROM transaction_details WHERE id = MAX(t.trans_id))
FROM
(SELECT trans_id, trans_type FROM transactions) t
GROUP BY trans_type
所以,我试图在内部查询中使用分组结果。但我得到的错误是我不能在内部查询中使用组函数:
ORA-00934: group function is not allowed here
除了诉诸另一个外部查询外,您能提供其他选择吗?
更新
根据要求,我发布了一个较短版本的真实查询:
SELECT service_code,
currency,
(SELECT value FROM exchange_rate WHERE date_added = MIN(t.trans_date)) AS exchange_rate,
TRIM(TO_CHAR(SUM(amount), '9999999990.99')) AS amount,
TRIM(TO_CHAR(SUM(tax_amount), '9999999990.99')) AS tax_amount,
TRIM(TO_CHAR(SUM(total_amount), '9999999990.99')) AS total_amount
FROM (SELECT t.amount AS amount,
t.trans_date AS trans_date
t.tax_amount AS tax_amount,
t.total_amount AS total_amount,
td1.string_value AS service_code,
td2.string_value AS currency
FROM transac) t
GROUP BY service_code, currency
答案 0 :(得分:0)
更传统的查询有什么问题:
SELECT t.trans_type, td.parameter_value, MAX(t.trans_id)
FROM transactions t
INNER
JOIN transaction_details td ON td.id = t.trans_id
GROUP BY t.trans_type, td.parameter_value
答案 1 :(得分:0)
我认为你所寻找的是这样的:
SELECT service_code,
currency,
er.value as exchange_rate,
TRIM(TO_CHAR(SUM(amount), '9999999990.99')) AS amount,
TRIM(TO_CHAR(SUM(tax_amount), '9999999990.99')) AS tax_amount,
TRIM(TO_CHAR(SUM(total_amount), '9999999990.99')) AS total_amount
FROM (SELECT t.amount AS amount,
t.tax_amount AS tax_amount,
t.total_amount AS total_amount,
td1.string_value AS service_code,
td2.string_value AS currency,
min(t.trans_date)
over (partition by td1.string,
td2.string) as min_trans_date
FROM transac t, table2 td1, table3 td2) t,
exchange_rate er
WHERE er.date_added = t.min_trans_date
我不确定这是否真的是您的解决方案,因为根据您的查询,我无法确定您想要的最小交易日期基于哪个组。此版本假定您需要服务代码和货币的给定组合的最早交易日期。
你的方法遇到了几个问题:
SELECT子句中的任何列 需要要么在GROUP BY中 条款或受集合约束 功能。你有几列 没有这个标准。
您正在尝试使用聚合 函数来自外部查询 标量子查询。你不能这样做 那。充其量,你可以得到 然后,在嵌套查询中聚合 稍后引用该值。