查找: max(abs(c(a = 2)-c(a = 1))) 按b排序
给定表T
a b c
-- -- --
1 1 2
1 2 4
1 3 5
2 1 10
2 2 11
2 3 20
3 1 40
3 2 40
3 3 40
数学示例:
c(@2) - c(@1) in order of b
10 - 2 = 8
11 - 4 = 7
20 - 5 = 15 <-- max found
我可以获得b = 1差异的答案,但我想简化查询而不剪切并粘贴其他内容,其中b = 1,b = 2,b = 3等。 不确定cte是否适用于此,还是使用递增b的递归查询?
最终工作查询:
SELECT
coalesce(max(abs(rn.c - r0.c)), -999) AS rd --> RETURN nulls = -999.
FROM
(SELECT b, c FROM t WHERE a = 1) AS r0
JOIN
(SELECT b, c FROM t WHERE a = 2) AS rn
ON r0.b = rn.b;
答案 0 :(得分:1)
create table stk_test(a int ,b int, c int)
insert into stk_test
select 1,1,2
union
select 1,2,4
union
select 1,3,5
union
select 2,1,10
union
select 2,2,11
union
select 2,3,20
union
select 3,1,40
union
select 3,2,40
union
select 3,3,40
select max(abs(a.a_c-b.b_c)) as Max_Diff_C from
(
select c as a_c,b from stk_test ss
where a=1
) a join
(
select c as b_c,b from stk_test ssr
where a=2
)b on a.b=b.b
答案 1 :(得分:0)
我还没明白你的意思。 可能有帮助。
int clickcount = 0;
private void OnMouseOver()
{
if (Input.GetMouseButtonDown(0))
{
if (clickcount == 0)
{
line.enabled = true;
}
else
{
line.enabled = !line.enabled;
}
if (line.enabled)
CreatePoints();
clickcount ++;
}
}
的测试
答案 2 :(得分:0)
这是使用Lead函数对数据进行排名的一种尝试
create table t(a int, b int , c int)
insert into t values(1,1,2)
insert into t values(1,2,4)
insert into t values(1,3,5)
insert into t values(2,1,10)
insert into t values(2,2,11)
insert into t values(2,3,20)
insert into t values(3,1,40)
insert into t values(3,2,40)
insert into t values(3,3,40)
with data
as (select * /*First rank the results on the basis of columns a and b*/
,row_number() over(order by a,b) as rnk
from t
)
,temp_data
as (select a
,b
,c
,rnk
,abs(c-lead(c,3) over(order by rnk)) as max_rnk
from data
)
select a,max(max_rnk) /*Gets the max value of rank grouped by each a*/
from temp_data
group by a
使用dbfiddle进行演示
https://dbfiddle.uk/?rdbms=sqlserver_2017&fiddle=b7db648a2512b623a7df4351326fa25d