gpt4 book ai didi

mysql select column outside group by

转载 作者:行者123 更新时间:2023-11-29 00:23:56 26 4
gpt4 key购买 nike

我有以下数据集

ID  P   C1    C2    XR  
1 1 GBP USD 1.6
2 2 GBP USD 1.7
3 1 GBP EUR 0.84
4 2 GBP EUR 0.83
5 1 GBP CHF 1.8
6 2 GBP CHF 1.8

我正在尝试获得每对的最小 XR,因此结果应该是

ID  P   C1    C2    XR  
1 1 GBP USD 1.6
4 2 GBP EUR 0.83
5 1 GBP CHF 1.8

但是当我 SELECT MIN(XR) GROUP BY C1,C2 时,我要么没有得到正确的 ID,要么收到一条警告说 ID not in GROUP BY if 'ONLY_FULL_GROUP_BY' enabled

我得到的最接近的是:

SELECT t1.id, t1.C1, t1.C2, MIN(t2.XR) FROM xrates t1 
LEFT JOIN xrates AS t2 ON t1.id = t2.id
GROUP BY t1.id, t1.C1, t1.C2
HAVING t1.C1 = 'GBP' AND t1.C2 IN ('USD','EUR')

最佳答案

select rates.*
from xrates rates
where id in
(
select min(xrates.id) as minid
from xrates
inner join
(
select C1, C2, MIN(XR) as minxr
from xrates
GROUP BY C1, C2
HAVING C1 = 'GBP'
) x on x.c1 = xrates.c1 and x.c2 = xrates.c2 and x.minxr = xrates.xr
group by xrates.C1, xrates.C2
)

SQLFiddle demo

关于mysql select column outside group by,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19948793/

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