我的SQL生锈了,所以我需要帮助,我希望我不会对我的解释感到困惑。
我需要一个语句来查找字段A的所有重复值,如果这些行中至少有一行在字段B上具有常量值
字段A |领域B
狗|犬齿
狗| NULL
狗| NULL
因为字段B是Canines,我想要所有复制字段A值的行。我需要一个select语句,但我的意图是在字段C上更改重复项的值,但我可能会在之后解决这个问题。
答案 0 :(得分:2)
您似乎想要所有行,其中FieldB
值不一致为NULL。
这会为您提供具有该特征的FieldA
列表:
select FieldA
from t
group by FieldA
having count(FieldB) > 0;
如果您确实想要具有NULL
值的行,那么:
select FieldA, FieldB
from (select t.*, count(FieldB) over (partition by FieldA) as cnt
from t
) t
where cnt = 0;
要获得所有FieldA,其中一个是犬科动物:
select distinct FieldA
from t
where FieldB = 'canine';
要获取所有行,请在where
子句中使用它:
select *
from t
where t.FieldA in (select distinct FieldA
from t
where FieldB = 'canine'
);
答案 1 :(得分:2)
要获取count(fielda) > 1
和fieldb
之一的值为canine
select t1.fielda
from mytab t1
join (select fielda, count(*) as dups
from mytab
group by fielda) t2 on t2.fielda = t1.fielda
where t1.fieldb = 'canine'
and t2.dups > 1
答案 2 :(得分:1)
如果你想要每个有重复的行,并且字段B中至少有一个项不为空,那么这将拉动它:
SELECT *
FROM Table
WHERE FieldA IN (SELECT DISTINCT FieldA
FROM Table
WHERE FieldB IS NOT NULL)
AND FieldA IN (SELECT FieldA
FROM Table
GROUP BY FieldA
HAVING COUNT(*)>1)
答案 3 :(得分:1)
你的帮助让我找到了..谢谢大家。我在寻找的是:
SELECT FieldA
FROM t
WHERE FieldB = 'Canines'
AND FieldA
IN
(
SELECT FieldA
FROM t
GROUP BY FieldA
HAVING COUNT(FieldA) > 1
)
谢谢!谢谢!