是否有一个2d函数可以满足两列上的group by要求,或者有更多的sql方法可以实现?
我有一个表,需要对两列进行分组,或者对那些产生预期分组标准的列进行函数分组。这成为一个令人惊讶的有趣的数学挑战,但我也很高兴看到如何在标准SQL解决方案中解决它 我有下表:是否有一个2d函数可以满足两列上的group by要求,或者有更多的sql方法可以实现?,sql,sqlite,math,group-by,Sql,Sqlite,Math,Group By,我有一个表,需要对两列进行分组,或者对那些产生预期分组标准的列进行函数分组。这成为一个令人惊讶的有趣的数学挑战,但我也很高兴看到如何在标准SQL解决方案中解决它 我有下表: create table temp (a integer, x integer, y integer); insert into temp values (3, 0, 1); insert into temp values (3, 1, -1); insert into temp values (4, 0, 1); inse
create table temp (a integer, x integer, y integer);
insert into temp values (3, 0, 1);
insert into temp values (3, 1, -1);
insert into temp values (4, 0, 1);
insert into temp values (4, 1, -1);
insert into temp values (4, 0, -1);
insert into temp values (4, 1, 1);
我想将行分组:
1和2
3和4
5和6
因此,选择将是:
select a
from temp
group by a, f(x, y)
为此,我需要一个函数fx,y,它基于可用的SQLite整数运算符,即.*,+,-,/构建,因此我需要一个函数,以便:
f0,1=f1,-1和
f0,-1=f1,1
我已经尝试了多种可能性,但无法找出这样的分组函数。。。有什么好主意吗
是否有其他SQL解决方案解决此问题?您可以使用case表达式定义此函数:
(case when x = 0 and y = 1 then 1
when x = 1 and y = -1 then 1
when x = 0 and y = -1 then 2
when x = 1 and y = 1 then 2
end)
(2 * x * y - y)
如果需要算术表达式:
(case when x = 0 and y = 1 then 1
when x = 1 and y = -1 then 1
when x = 0 and y = -1 then 2
when x = 1 and y = 1 then 2
end)
(2 * x * y - y)