Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/70.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/21.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
SQL Server选择查询以根据其中一列的长度排除类似行_Sql_Sql Server - Fatal编程技术网

SQL Server选择查询以根据其中一列的长度排除类似行

SQL Server选择查询以根据其中一列的长度排除类似行,sql,sql-server,Sql,Sql Server,我正在处理一个表,其数据类似于: key1, key2, col1, col2, col3 1, 2, 1, 10, 'bla' 2, 2, 1, 10, 'bla2' 2, 1, 2, 10, 'bla' 键1和键2是复合键 我想要一个返回以下内容的查询: key1, key2, col1, col2, col3 2, 2, 1, 10, 'bla2' 2, 1, 2, 10, 'b

我正在处理一个表,其数据类似于:

key1, key2, col1, col2, col3
1,    2,    1,    10,   'bla'
2,    2,    1,    10,   'bla2'
2,    1,    2,    10,   'bla'
键1和键2是复合键

我想要一个返回以下内容的查询:

key1, key2, col1, col2, col3
2,    2,    1,    10,   'bla2'
2,    1,    2,    10,   'bla'
因此,对于col1和col2,没有两行具有相同的值。返回的col3值是长度最长的值

我想这一定很简单,但我无法解决这个问题


非常感谢,Paul

解析
行数()
函数非常适合此功能:

SELECT key1, key2, col1, col2, col3
FROM   (SELECT key1, key2, col1, col2, col3,
               ROW_NUMBER() OVER 
               (PARTITION BY key1, key2 ORDER BY LEN(col3) DESC) AS rn
        FROM   my_table) t
WHERE  rn = 1

测试数据

DECLARE  @TABLE_NAME TABLE(key1 INT, key2 INT, col1 INT, col2 INT, col3 VARCHAR(10))

INSERT INTO @TABLE_NAME 
SELECT 1,    2,    1,    10,   'bla'
UNION ALL 
SELECT 2,    2,    1,    10,   'bla2'
UNION ALL
SELECT 2,    1,    2,    10,   'bla'
查询

SELECT * FROM
(
SELECT *, ROW_NUMBER() OVER (PARTITION BY col1,col2 ORDER BY LEN(col3) DESC) AS rn
FROM @TABLE_NAME 
)Q
WHERE rn = 1
结果集

╔══════╦══════╦══════╦══════╦══════╦════╗
║ key1 ║ key2 ║ col1 ║ col2 ║ col3 ║ rn ║
╠══════╬══════╬══════╬══════╬══════╬════╣
║    2 ║    2 ║    1 ║   10 ║ bla2 ║  1 ║
║    2 ║    1 ║    2 ║   10 ║ bla  ║  1 ║
╚══════╩══════╩══════╩══════╩══════╩════╝

这是基于一小部分样本数据的。但是,如果有多行具有相同长度的Co1/Col2值和Col3值,则结果集将有一些重复。例如:

2,2,1,10,“bla2”

2,2,1,10,“bla4”

2,2,1,10,“bla6”

--A common table expression that represents the sample data from the OP.
WITH cteSampleData AS
(
    SELECT 1 Key1,    2 Key2,    1 Col1,    10 Col2,   'bla' Col3 UNION 
    SELECT 2,    2,    1,    10,   'bla2' UNION 
    SELECT 2,    1,    2,    10,   'bla'
),
--Another common table expression.  It returns unique combinations of col1 and col2 with the col3 value with the longest length.
cte2 AS
(
    SELECT Col1, Col2, MAX(LEN(Col3)) LongestCol3Val
    FROM cteSampleData
    GROUP BY Col1, Col2
)
SELECT c.*
FROM cteSampleData c
JOIN cte2
    ON cte2.Col1 = c.Col1
    AND cte2.Col2 = c.Col2
    AND cte2.LongestCol3Val = LEN(c.Col3)

您尝试的查询是什么?您甚至不关心我输入的样本数据是否正确。我想从结果集中排除第一行(1,2,1,10,'bla'),因为第二行的col1和col2值相同,但col3中包含的文本更长。如果示例数据包括此行
2,2,1,10,'bla8'
,您希望在结果集中看到哪一行?
bla2
行或
bla8
行?我认为这将为该实例中的每一行提供行编号1。查询需要考虑col3中数据的长度,因此对于cols1和col2中具有相同值的行,只返回其中一行,即col3中具有最长值的行。在我的帖子中,结果集的第一行显示了col3,值为“bla2”-这是我真正需要的。@user3389019我已经更新了我的答案,现在看看。如果有帮助,请接受它作为你的答案,谢谢。这也是一个有效的好答案。不幸的是,我只能选择一个回答作为接受的“答案”。谢谢Paul@user3389019:没什么大不了的。不过,我很感谢您的评论。:)