Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
是否有一个2d函数可以满足两列上的group by要求,或者有更多的sql方法可以实现?_Sql_Sqlite_Math_Group By - Fatal编程技术网

是否有一个2d函数可以满足两列上的group by要求,或者有更多的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

我有一个表,需要对两列进行分组,或者对那些产生预期分组标准的列进行函数分组。这成为一个令人惊讶的有趣的数学挑战,但我也很高兴看到如何在标准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);
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)