繁体   English   中英

如何在单表中进行 Group By 和 SELF JOIN

[英]How to do Group By and SELF JOIN in single table

这是我想要完成的工作。 部分我是通过,部分我被卡住了,需要一些帮助。

表结构:

CREATE TABLE UserRole
(
    Id bigint NOT NULL AUTO_INCREMENT,
    Name varchar(100) NOT NULL,
    Description varchar(200) NULL,
    IsEnabled bit NOT NULL, -- 1 if a role is enabled, 0 otherwise
    Created date NOT NULL, -- When a role was created
    CreatedBy varchar(200) NOT NULL, -- Who created a role
    Updated date NULL, -- When a role was updated (if at all)
    UpdatedBy varchar(200) NULL, -- Who updated a role (if at all)
    CONSTRAINT PK_UserRole PRIMARY KEY ( Id ASC )
)

数据:

ID 姓名 描述 已启用 已创建 由...制作 更新 更新者
1 角色_1 无效的 1 2020-04-15 行政 无效的 无效的
2 角色_2 描述 1 2020-04-16 行政 2020-04-17 约翰·史密斯
3 角色_3 描述 0 2020-04-16 约翰·史密斯 2020-04-17 本·史密斯
4 角色_4 描述 1 2020-04-19 贝恩史密斯 2020-04-21 本·史密斯

预期结果和我正在努力获取报告:

获取UserNameNoOFCreatedRolesNoOfCreatedEnabledNoOfUpdated

对于上面的输入,这里是示例输出

在此处输入图像描述

几个假设:

  • 您可以假设每个用户至少创建一个角色。

  • 您的查询应该在 MySQL 8.0 上运行,因为我正在使用它,这是预期的。

  • 查询不应为数字列返回 NULL,而不是 NULL 将其替换为 -1。

我的解决方案

-- Collect NoOfCreatedRoles ( Working )
select  UCASE(CreatedBy), count(*) as 'NoOfCreatedRoles' 
     from UserRole 
     Group By UCASE(CreatedBy) 
     order by  UCASE(CreatedBy) desc;

结果

UCASE(创建者) NoOfCreatedRoles
约翰·史密斯 1
本·史密斯 1
行政 2
-- Collect NoOfUpdatedRoles
select  TRIM(UCASE(UpdatedBy)), count(*) as 'NoOfUpdatedRoles' 
     from UserRole 
     Group By TRIM(UCASE(UpdatedBy))  desc;
修剪(UCASE(更新者)) NoOfUpdatedRoles
约翰·史密斯 1
本·史密斯 2
无效的 1

NULL应该被删除,为什么它来了????

-- Joined Query
select  distinct UCASE(a.CreatedBy) as 'UserName' , 
        count(*) as 'NoOfCreatedRoles'  , 
        count(*) as 'NoOfUpdatedRoles' from UserRole a
    join UserRole b 
    where TRIM(UCASE(a.CreatedBy)) = TRIM(UCASE(b.CreatedBy))
    Group By UCASE(a.CreatedBy), UCASE(a.UpdatedBy) 
    order by UCASE(a.CreatedBy) desc;
用户名 NoOfCreatedRoles NoOfUpdatedRoles
约翰·史密斯 1 1
本·史密斯 1 1(错了,应该是2)
行政 2 2(错误,应该是-1)

另外,为什么我得到错误的结果以及如何获得numberOfCreatedAndEnabled列,需要一些有见地的描述的帮助,将不胜感激。

由于所有已启用角色的IsEnabled列为 1,因此sum()将计算NoOfCreatedEnabled 要计算NoOfUpdatedRoles ,您可以使用子查询。 组合查询如下:

查询一:

  select  UCASE(CreatedBy) as 'UserName', count(*) as 'NoOfCreatedRoles',
      sum(IsEnabled ) as 'NoOfCreatedEnabled ',
      (select count(*) from UserRole u where UCASE(u.UpdatedBy) = UCASE(ur.CreatedBy)  ) as 'NoOfUpdatedRoles'
  from UserRole ur
  Group By ur.CreatedBy
  order by UCASE(CreatedBy) desc;

输出:

用户名 NoOfCreatedRoles NoOfCreatedEnabled NoOfUpdatedRoles
约翰·史密斯 1 0 1
本·史密斯 1 1 2
行政 2 2 0

查询 2(将 0 替换为 -1):

  select  UCASE(CreatedBy) as 'UserName', count(*) as 'NoOfCreatedRoles' ,
      sum(IsEnabled ) as 'NoOfCreatedEnabled ',
      (select COALESCE(NULLIF(count(*),0),-1) from UserRole u where UCASE(u.UpdatedBy) = UCASE(ur.CreatedBy)  ) as 'NoOfUpdatedRoles'
  from UserRole ur
  Group By ur.CreatedBy
  order by UCASE(CreatedBy) desc;

nullif(parameter1,0)如果 parameter1 与第二个参数相同,此处为 0,则返回 null。 coalesce(parameter1,-1)仅当 parameter1 为 null 时才会返回 -1

输出:

用户名 NoOfCreatedRoles NoOfCreatedEnabled NoOfUpdatedRoles
约翰·史密斯 1 0 1
本·史密斯 1 1 2
行政 2 2 -1

db<> 在这里摆弄

先数数,加入结果

select t1.usr, NoOfCreatedRoles, 
        case NoOfCreatedEnabled when 0 then -1 else NoOfCreatedEnabled end NoOfCreatedEnabled,
        coalesce(NoOfUpdatedRoles, -1) NoOfUpdatedRoles
from (
     select TRIM(UCASE(CreatedBy)) usr, count(*) as NoOfCreatedRoles ,
     sum(IsEnabled ) as NoOfCreatedEnabled
     from UserRole ur
     Group By TRIM(UCASE(CreatedBy))
) t1 left join (
     select TRIM(UCASE(UpdatedBy)) usr, count(*) as NoOfUpdatedRoles
     from UserRole 
     Group By  TRIM(UCASE(UpdatedBy))
) t2 on t1.usr = t2.usr

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM