0

我需要选择未重复的值,这些值从未被重复,使用 distinct 不是解决方案。我需要从行中显示更多的字段,而不是一个用于分组的字段。

我的查询只显示一个字段:

SELECT NIPL
FROM KONTRAHENT  GROUP BY NIPL HAVING (COUNT(NIPL)=1) 
ORDER BY NIPL DESC

桌子:

id | NIPL
 1 | 230
 2 | 450   <- I want to select only this row
 3 | 230
 3 | 123
 4 | 123

现在我的选择只返回 NIPL。简单地将 id 添加到 statment 是不行的。经过长时间的谷歌搜索,我放弃了。我需要你的帮助:)

4

1 回答 1

2

您只需要使用子查询。

像这样:

SELECT * 
FROM KONTRAHENT
WHERE NIPL IN 
(
   SELECT NIPL
   FROM KONTRAHENT
   GROUP BY NIPL
   HAVING COUNT(NIPL)=1 
)
ORDER BY NIPL DESC

或这个:

SELECT * 
FROM KONTRAHENT
JOIN 
(
   SELECT NIPL
   FROM KONTRAHENT
   GROUP BY NIPL
   HAVING COUNT(NIPL)=1 
) AS SUB ON KONTRAHENT.NIPL = SUB.NIPL
ORDER BY KONTRAHENT.NIPL DESC

或者(在允许 CTE 的系统中)这个:

WITH SUB AS
(
   SELECT NIPL
   FROM KONTRAHENT
   GROUP BY NIPL
   HAVING COUNT(NIPL)=1 
) 
SELECT * 
FROM KONTRAHENT
JOIN SUB ON KONTRAHENT.NIPL = SUB.NIPL
ORDER BY KONTRAHENT.NIPL DESC
于 2013-10-01T17:44:37.943 回答