需要帮助编写一个 SQL 查询来计算非重复行(不是不同的计数)
Need help writing an SQL query to count non duplicate rows (not a distinct count)
我有一个 table 如下所示。我正在尝试对不重复的 ID 进行计数。我不是说一个独特的计数。不同的计数将 return 结果为 7(a、b、c、d、e、f、g)。我希望它 return 计数为 4(a、c、d、f)。这些是没有多个类型代码的 ID。我尝试了以下查询,但得到的计数为 0(结果应该以百万为单位)。
select ID, count (ID) as number
from table
group by ID
having count (ID) = 1
Select count (distinct ID)
From table
Having count (ID) = 1
ID|type code
a|111
b|222
b|333
c|444
d|222
e|111
e|333
e|555
f|444
g|333
g|444
感谢@scaisEdge!您提供的第一个查询正是我在上述问题中寻找的内容。既然我的领导已经弄清楚了,我的领导要求它更进一步,以显示单个类型代码中有多少次 ID 的计数。比如我们要看
类型代码|计数
111|1
222|1
444|2
有 2 个 ID 具有单一类型代码 444 (c, f),有一个 ID 具有单一类型代码 111 (a) 和 222 (d)。我试过这样修改查询,但是在 运行 查询
时遇到了错误
select 计数(admin_sys_tp_cd) 作为数字
从 (
select cont_id 来自
imdmadmp.contequiv
按 cont_id 分组
有 count(*) =1) t
按 admin_sys_tp_cd
分组
如果你想要计数可以是
select count(*) from (
select id from
my_table
group by id
having count(*) =1
) t
如果你想要 id
select id from
my_table
group by id
having count(*) =1
侯关于这个你在临时做一个循环table?:
select
*
into #control
from tablename
declare @acum as int
declare @code as char(3)
declare @id as char(1)
declare @id2 as int
select @acum=0
while exists (select* from #control)
begin
select @code = (select top 1 code from #control order by id)
select @id = (select top 1 id from #control order by id)
select @id2 =count(id) from #control where id in (select id from tablename where id = @id and code <> @code)
if @id2=0
begin
select @acum = @acum+1
end
delete #control
where id = @id --and code = @code
end
drop table #control
print @acum
我有一个 table 如下所示。我正在尝试对不重复的 ID 进行计数。我不是说一个独特的计数。不同的计数将 return 结果为 7(a、b、c、d、e、f、g)。我希望它 return 计数为 4(a、c、d、f)。这些是没有多个类型代码的 ID。我尝试了以下查询,但得到的计数为 0(结果应该以百万为单位)。
select ID, count (ID) as number
from table
group by ID
having count (ID) = 1
Select count (distinct ID)
From table
Having count (ID) = 1
ID|type code
a|111
b|222
b|333
c|444
d|222
e|111
e|333
e|555
f|444
g|333
g|444
感谢@scaisEdge!您提供的第一个查询正是我在上述问题中寻找的内容。既然我的领导已经弄清楚了,我的领导要求它更进一步,以显示单个类型代码中有多少次 ID 的计数。比如我们要看
类型代码|计数 111|1 222|1 444|2
有 2 个 ID 具有单一类型代码 444 (c, f),有一个 ID 具有单一类型代码 111 (a) 和 222 (d)。我试过这样修改查询,但是在 运行 查询
时遇到了错误select 计数(admin_sys_tp_cd) 作为数字 从 ( select cont_id 来自 imdmadmp.contequiv 按 cont_id 分组 有 count(*) =1) t 按 admin_sys_tp_cd
分组如果你想要计数可以是
select count(*) from (
select id from
my_table
group by id
having count(*) =1
) t
如果你想要 id
select id from
my_table
group by id
having count(*) =1
侯关于这个你在临时做一个循环table?:
select
*
into #control
from tablename
declare @acum as int
declare @code as char(3)
declare @id as char(1)
declare @id2 as int
select @acum=0
while exists (select* from #control)
begin
select @code = (select top 1 code from #control order by id)
select @id = (select top 1 id from #control order by id)
select @id2 =count(id) from #control where id in (select id from tablename where id = @id and code <> @code)
if @id2=0
begin
select @acum = @acum+1
end
delete #control
where id = @id --and code = @code
end
drop table #control
print @acum