我目前正在运行以下查询(见下文。)但是,当我执行相同的值时,会为 totalusers、activeusers 和suspendedusers 中的多行返回相同的值。但是,当涉及到总登录时,这些值是唯一的。这是他们可能发生这种情况的一个原因,并且是他们解决问题的一种方法。如果它有助于我使用带有 postgre sql 驱动程序的工具 sql workben。
干杯
SELECT
company.companyStatus,
company.CompanyId,
company.companyName,
select
count(distinct UserID)
From Users
inner join company
on Users.CompanyID = Company.CompanyId
where Users.Companyid = company.Companyid
as TotalUsers,
select sum(case when userstatusid =2 then 1 else 0 end)
from users
inner join company
on users.companyid = company.companyid
where users.companyid = company.companyid)
as ActiveUsers,
select sum(case when userstatusid = 3 then 1 else 0 end)
from users
inner join company
on users.companyid = company.companyid
where users.companyid = company.companyid)
as SuspendedUsers,
(Select COUNT (distinct usersessionid)
From UserSession
inner join users
on usersession.UserID=users.UserID
where usersession.UserID=users.UserID
and users.companyid= company.CompanyID)
as TotalLogin,
from Company