如果重复,加入 table 获取第一个 table 的第一个数据
Joining table get the first data on first table if duplicate
嗨,我有 table1
和 table2
。
table1
是员工的登录时间table,table2
是员工的组码。
在 table1
上,一些员工重复上班时间,因为他们多次上班只是为了确保他们的时间。
表 1
ID EMPID Time_IN
1 001 7:01 AM
2 004 7:04 AM
3 034 7:10 AM
4 034 7:11 AM
5 019 7:11 AM
6 019 7:12 AM
表 2
ID empID GroupName
1 001 AA
2 004 AB
3 034 AA
4 019 AA
结果
GroupName CNT
AA 5
AB 1
预期结果
GroupName CNT
AA 3
AB 1
当前查询
Select b.GroupName, count(*) as cnt
from table1 a
inner join table2 b
on a.EMPID = b.empID
Group by b.GroupName
我怎样才能达到上面的预期结果?
提前谢谢你。
您可以按如下方式使用distinct count
:
select t2.groupname, count(distinct empid) as cnt
from table1 t1 join table2 t2
on t1.empid = t2.empid
group by t2.groupname
join
对于您提出的问题是多余的:
select t2.GroupName, count(*) as cnt
from table2 t2
group by t2.GroupName;
这比 join
并使用 count(distinct)
更有效。您可能真的有一个不同的问题,应该作为 新 问题提出。
嗨,我有 table1
和 table2
。
table1
是员工的登录时间table,table2
是员工的组码。
在 table1
上,一些员工重复上班时间,因为他们多次上班只是为了确保他们的时间。
表 1
ID EMPID Time_IN
1 001 7:01 AM
2 004 7:04 AM
3 034 7:10 AM
4 034 7:11 AM
5 019 7:11 AM
6 019 7:12 AM
表 2
ID empID GroupName
1 001 AA
2 004 AB
3 034 AA
4 019 AA
结果
GroupName CNT
AA 5
AB 1
预期结果
GroupName CNT
AA 3
AB 1
当前查询
Select b.GroupName, count(*) as cnt
from table1 a
inner join table2 b
on a.EMPID = b.empID
Group by b.GroupName
我怎样才能达到上面的预期结果?
提前谢谢你。
您可以按如下方式使用distinct count
:
select t2.groupname, count(distinct empid) as cnt
from table1 t1 join table2 t2
on t1.empid = t2.empid
group by t2.groupname
join
对于您提出的问题是多余的:
select t2.GroupName, count(*) as cnt
from table2 t2
group by t2.GroupName;
这比 join
并使用 count(distinct)
更有效。您可能真的有一个不同的问题,应该作为 新 问题提出。