有没有办法不显示 SQL 中具有重复 ID 的行?
is there a way to not display rows with duplicate ID in SQL?
这是我的查询
SELECT *
FROM O_SMFU WHERE (ID , SMFU_nNEWNAIYO) IN
( SELECT ID , MAX(SMFU_nNEWNAIYO)
FROM O_SMFU
GROUP BY ID
)
当 ID 重复时,仅显示最大行 SMFU_nNEWNAIYO
但它给了我一个错误:
Message 4145, Level 15, State 1, Line 49
A non-Boolean expression was specified near',' for a context that requires a condition.
我拥有的数据:
ID SMFU_nNEWNAIYO
703 1930
703 1920
703 1910
705 1950
725 1900
726 1900
726 1900
886 1900
886 1900
我要获取的数据:
ID SMFU_nNEWNAIYO
703 1930
705 1950
725 1900
726 1900
886 1900
我不知道你的 SQL 风格(SQL 服务器)是否接受这种语法,但无论如何我建议只将你的查询写成一个连接:
SELECT o1.*
FROM O_SMFU o1
INNER JOIN
(
SELECT ID, MAX(SMFU_nNEWNAIYO) AS MAX_SMFU_nNEWNAIYO
FROM O_SMFU
GROUP BY ID
) o2
ON o2.ID = o1.ID AND
o2.MAX_SMFU_nNEWNAIYO = o1.SMFU_nNEWNAIYO;
另一种在 SQL 服务器上编写查询的方法:
SELECT TOP 1 WITH TIES *
FROM O_SMFU
ORDER BY RANK() OVER (PARTITION BY ID ORDER BY SMFU_nNEWNAIYO DESC);
试试这个查询
SELECT id,MAX(SMFU_nNEWNAIYO)
FROM O_SMFU
GROUP BY id
这个查询会给你想要得到的数据
这是我的查询
SELECT *
FROM O_SMFU WHERE (ID , SMFU_nNEWNAIYO) IN
( SELECT ID , MAX(SMFU_nNEWNAIYO)
FROM O_SMFU
GROUP BY ID
)
当 ID 重复时,仅显示最大行 SMFU_nNEWNAIYO
但它给了我一个错误:
Message 4145, Level 15, State 1, Line 49
A non-Boolean expression was specified near',' for a context that requires a condition.
我拥有的数据:
ID SMFU_nNEWNAIYO
703 1930
703 1920
703 1910
705 1950
725 1900
726 1900
726 1900
886 1900
886 1900
我要获取的数据:
ID SMFU_nNEWNAIYO
703 1930
705 1950
725 1900
726 1900
886 1900
我不知道你的 SQL 风格(SQL 服务器)是否接受这种语法,但无论如何我建议只将你的查询写成一个连接:
SELECT o1.*
FROM O_SMFU o1
INNER JOIN
(
SELECT ID, MAX(SMFU_nNEWNAIYO) AS MAX_SMFU_nNEWNAIYO
FROM O_SMFU
GROUP BY ID
) o2
ON o2.ID = o1.ID AND
o2.MAX_SMFU_nNEWNAIYO = o1.SMFU_nNEWNAIYO;
另一种在 SQL 服务器上编写查询的方法:
SELECT TOP 1 WITH TIES *
FROM O_SMFU
ORDER BY RANK() OVER (PARTITION BY ID ORDER BY SMFU_nNEWNAIYO DESC);
试试这个查询
SELECT id,MAX(SMFU_nNEWNAIYO)
FROM O_SMFU
GROUP BY id
这个查询会给你想要得到的数据