Sql 如何在having子句下进行多次计数
一些样本数据:Sql 如何在having子句下进行多次计数,sql,Sql,一些样本数据: Id name value ref 1 ab xy 2 aba z 3 ab xy 4 abc def 5 gxr mdy 我想做的是得到两个列,出现了不止一次 因此,将选择第1行和第3行 select name, value from table_x where value is not null group by name having count(name) >=
Id name value ref
1 ab xy
2 aba z
3 ab xy
4 abc def
5 gxr mdy
我想做的是得到两个列,出现了不止一次
因此,将选择第1行和第3行
select name, value from table_x
where value is not null group by name having count(name) >= 2
and having count(value) >= 2;
被卡住了….您的语法不正确<代码>按名称和值分组,然后检查计数>=2
select name, value
from table_x
where value is not null
group by name, value
having count(*) >= 2;
你的语法不正确<代码>按名称和值分组,然后检查计数>=2
select name, value
from table_x
where value is not null
group by name, value
having count(*) >= 2;
@如果您只关心查找多次出现的不同名称/值对,那么vkp的答案是正确的。但是,如果确实需要满足条件的单个行,请尝试以下操作:
SELECT t1.Name, t1.[Value]
FROM Table_X t1
JOIN
(
SELECT Name, [Value]
FROM Table_X
where [Value] IS NOT NULL
GROUP BY Name, [Value]
HAVING COUNT(1) >= 2
) t2 ON t1.Name = t2.Name AND t1.[Value] = t2.[Value]
@如果您只关心查找多次出现的不同名称/值对,那么vkp的答案是正确的。但是,如果确实需要满足条件的单个行,请尝试以下操作:
SELECT t1.Name, t1.[Value]
FROM Table_X t1
JOIN
(
SELECT Name, [Value]
FROM Table_X
where [Value] IS NOT NULL
GROUP BY Name, [Value]
HAVING COUNT(1) >= 2
) t2 ON t1.Name = t2.Name AND t1.[Value] = t2.[Value]