Sql 如何仅显示每个项目的最大计数结果
我在显示的每台vc计算机上都有longon(totalcount)上带有nb的Vuser 我只想显示每个vuser的最大totalcount的vcomputer 这样,将只显示具有max logon的vcomputer 谢谢 在SQL Server中: 配合使用 或与Sql 如何仅显示每个项目的最大计数结果,sql,Sql,我在显示的每台vc计算机上都有longon(totalcount)上带有nb的Vuser 我只想显示每个vuser的最大totalcount的vcomputer 这样,将只显示具有max logon的vcomputer 谢谢 在SQL Server中: 配合使用 或与 最简单的方法是:使用groupby和max()将其包装到另一个查询中。 SELECT DISTINCT [vUser] ,[vcomputer] ,vFullName ,COUNT(vUs
最简单的方法是:使用
groupby
和max()
将其包装到另一个查询中。
SELECT DISTINCT [vUser]
,[vcomputer]
,vFullName
,COUNT(vUser) over ( partition by vcomputer, vuser )as totalCount
FROM [PRD_TechnoWin].[dbo].[v_Logonstats_PFB]
Where vComputer Not Like '%-VX%'
And vComputer Not Like '%-V%'
And v_Logonstats_PFB.vComputer Not Like '%XDAS%'
And v_Logonstats_PFB.vComputer Not Like '%XDDS%'
And v_Logonstats_PFB.vComputer Not Like 'VM%'
And v_Logonstats_PFB.vComputer Not Like '%TEST%'
And v_Logonstats_PFB.vComputer Not Like '%FOR%'
And v_Logonstats_PFB.vComputer Not Like '%GPW%%'
And v_Logonstats_PFB.vComputer Not Like '%PW%'
And v_Logonstats_PFB.vComputer Not Like '%DW1%'
And v_Logonstats_PFB.vComputer Not Like '%GRP-PFB-32BIT%'
And vDateTimeLogon > GETDATE()-45
GROUP BY vComputer, vUser, vFullName, vDateTimeLogon
order by vUser, totalcount desc
select top 1 with ties
[vUser]
, [vcomputer]
, vFullName
, count(*) as totalCount
from [PRD_TechnoWin].[dbo].[v_Logonstats_PFB]
where vComputer not like '%-VX%'
and vComputer not like '%-V%'
and v_Logonstats_PFB.vComputer not like '%XDAS%'
and v_Logonstats_PFB.vComputer not like '%XDDS%'
and v_Logonstats_PFB.vComputer not like 'VM%'
and v_Logonstats_PFB.vComputer not like '%TEST%'
and v_Logonstats_PFB.vComputer not like '%FOR%'
and v_Logonstats_PFB.vComputer not like '%GPW%%'
and v_Logonstats_PFB.vComputer not like '%PW%'
and v_Logonstats_PFB.vComputer not like '%DW1%'
and v_Logonstats_PFB.vComputer not like '%GRP-PFB-32BIT%'
and vDateTimeLogon > GETDATE() - 45
group by
vComputer
, vUser
, vFullName
order by row_number() over (partition by vUser, vcomputer order by count(*) desc)
with cte as (
select
[vUser]
, [vcomputer]
, vFullName
, count(*) as totalCount
, row_number() over (partition by vUser, vcomputer order by count(*) desc)
from [PRD_TechnoWin].[dbo].[v_Logonstats_PFB]
where vComputer not like '%-VX%'
and vComputer not like '%-V%'
and v_Logonstats_PFB.vComputer not like '%XDAS%'
and v_Logonstats_PFB.vComputer not like '%XDDS%'
and v_Logonstats_PFB.vComputer not like 'VM%'
and v_Logonstats_PFB.vComputer not like '%TEST%'
and v_Logonstats_PFB.vComputer not like '%FOR%'
and v_Logonstats_PFB.vComputer not like '%GPW%%'
and v_Logonstats_PFB.vComputer not like '%PW%'
and v_Logonstats_PFB.vComputer not like '%DW1%'
and v_Logonstats_PFB.vComputer not like '%GRP-PFB-32BIT%'
and vDateTimeLogon > GETDATE() - 45
group by
vComputer
, vUser
, vFullName
)
select vUser, vComputer, vFullName, TotalCount
from cte
where rn = 1