简体   繁体   English

使用ROW_NUMBER()将第1个ARRAY与第2个,第3个,第4个等进行比较

[英]Using ROW_NUMBER () to Compare 1st ARRAY to 2nd, 3rd, 4th, etc

I'm using ROW_NUMBER and I'm trying to compare arr in rn 1 to arr in rn 2,3,4 ,etc to see if they overlap. 我使用ROW_NUMBER ,我试图比较arrRN 1arrRN 2,3,4等,看看他们是否重叠。 I can do this with a subquery / simple join . 我可以用一个子查询做到这一点/简单 连接 Is there a way that AVOIDS a join? 有没有一种方法可以避免加入?

rn | id | job | arr    |desired_result
---+----+-----+--------+---------
 1 | 1  | 100 | {1,2}  | {1,2}
 2 | 1  | 101 | {2,3}  | {1,2}
 3 | 1  | 102 | {5,6,8}| {1,2}
 4 | 1  | 103 | {2,7}  | {1,2}

I made a dbfiddle 我做了一个dbfiddle

--USING JOIN 
WITH a AS (
SELECT 
ROW_NUMBER() OVER (PARTITION BY id ORDER by job) as rn
,*
FROM a_table
)
SELECT *
FROM (
SELECT id,arr
FROM a 
WHERE rn = 1 
) x
JOIN a
ON a.id=x.id

You can use first_value() : 您可以使用first_value()

SELECT a.*, first_value(arr) over (partition by id order by job)
FROM a_table a;

row_number() does not seem necessary. row_number()似乎不是必需的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQL Server挑战性问题-寻找第一买家,第二/第三买家,第四或更多买家 - SQL Server Challenging questions - look for 1st buyer, 2nd/3rd buyers, 4th or more buyers 查询第 1、第 2、第 3 名,使用第 2 和第 3 最低的平局,每次在一行 - query 1st, 2nd, 3rd place with tie break using 2nd and 3rd lowest with each time on one row 在一行中查找第 2、3、4、5 个非 NULL 值(SQL) - finding 2nd, 3rd, 4th, 5th not NULL values in a row (SQL) 合并第一行和第二行,第三行和第四行,依此类推 - Merge 1st and Second Row, 3rd and 4th Row and so on SQL-在列表中为第1次,第2次,第3次创建计数 - SQL - Creating counts for 1st, 2nd, 3rd occasion in a list 在Access中为每个人选择第1行,第2行和第3行 - Select 1st, 2nd, and 3rd rows for each person in Access 在SQL中,我需要生成一个排名(第1,第2,第3)列,并停留在“联系”上 - In SQL, I need to generate a ranking (1st, 2nd, 3rd) column, getting stuck on “ties” 我如何 Select 不同列中的第一个、第二个和第三个值 - 访问女士 - How can I Select 1st, 2nd and 3rd values in different columns - Ms Access SQL:如何从表中选择第 1、3、11 和第 n 行? - SQL: How to select 1st, 3rd, 11th and nth row from a table? MySQL-用第二行的第二列减去第一行的第一列 - MySQL - Subtract 1st column of 1st row with 2nd column of 2nd row
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM