我需要选择不重复的值,从不重复,使用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不是woking。经过长时间的谷歌搜索,我放弃了。我需要你的帮助:)
答案 0 :(得分: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