隔离来自 LEFT JOIN 的计数,独立于 WHERE 子句过滤器

Isolate the Count that comes from a LEFT JOIN independent of the WHERE clause filters

基本上我想列出所有配置文件名称(配置文件Table),每个配置文件都有一堆与之关联的实体(Profile_Entity)和实体类型(Profile_Entity类型)。因此,对于每个配置文件,我想计算每个配置文件上有多少个实体。

如果我不过滤结果,这个(计数)工作正常。但是,如果我尝试按实体过滤(查看此类实体是否属于配置文件),它会打乱我的实体计数。出现这种情况是因为过滤table时,没有出现EntityIDBP(作为EntityIDBP)的行消失了,count会统计过滤后的table的行,我希望的地方坚持原来的。

所以我尝试使用 LEFT JOIN 来隔离计数,但没有成功。

这是我目前拥有的

SELECT  {Profile}.[Id],
            {Profile}.[Name],
            Count (ProfileCount.IDBP)
     FROM {Profile}
     LEFT JOIN
      ((
     /* Get all the entities that belong to a profile, trough the entity type */
        SELECT  P2.[Id] as Id,
                P2.[Name] as ProfileName,
                {Entity}.[EntityIDBP] as IDBP

         FROM    {Profile} as P2
         LEFT JOIN {Profile_EntityType} ON ({Profile_EntityType}.[ProfileId] = P2.[Id])
         LEFT JOIN    {Entity} ON ({Entity}.[EntityType_IDBP] = {Profile_EntityType}.[EntityType_IDBP] )

          UNION

        /* Get all the entities that belong to a profile directly, trough the Profile_Entity.isToInclude = 1  */
        SELECT   P2.[Id] as Id,
                    P2.[Name] as ProfileName,
                    {Entity}.[EntityIDBP] as IDBP             
         FROM    {Profile} as P2
         LEFT JOIN   {Profile_Entity} ON ({Profile_Entity}.[ProfileId] = P2.[Id] AND {Profile_Entity}.[IsToInclude] = 1)
         LEFT JOIN    {Entity} ON ({Entity}.[EntityIDBP] = {Profile_Entity}.[EntityIDBP] 

          )EXCEPT(

         /* The subquery that gets all the entities that shouldn't be accounted for the Count (Profile_Entity.isToInclude = 0)  */
         SELECT   P2.[Id] as Id,
                  P2.[Name] as ProfileName,
                  {Entity}.[EntityIDBP] as IDBP

          FROM    {Profile} as P2
          JOIN   {Profile_Entity} ON ({Profile_Entity}.[ProfileId] = P2.[Id] AND {Profile_Entity}.[IsToInclude] = 0)
          JOIN    {Entity} ON ({Entity}.[EntityIDBP] = {Profile_Entity}.[EntityIDBP] ))) as ProfileCount ON ({Profile}.[Id] = ProfileCount.Id)

WHERE ProfileCount.IDBP IN (301000044)  
/* The Filter used to know if a profile has a entity or not ; Right now it's a fixed value just to demonstrate*/ 
/*The 301000044 represents the Entity IDBP */

GROUP BY {Profile}.[Name],{Profile}.[Id])

这里的示例是数据模型 tables。 个人资料table:

|---------------------|------------------|
|Id  |    Name        |     (...)        |
|---------------------|------------------|
|10  | Profile1       |        (...)     |
|---------------------|------------------|

Profile_Entity table:

|---------------------|------------------|-----------------------------|
|      ProfileId      |     EntityIDBP   |isToInclude                  |
|                     |serves as the     |/*this representes wheter the| 
|                     |unique id         | entity should be considered | 
|                     |                  |  for the count (=1) or not  |
|                     |                  |   (=0) */                   |
|---------------------|------------------|-----------------------------|
|     10              |       301000044  | 1                           |
|---------------------|------------------|-----------------------------|
|                     |                  |                             |
|     10              |       301000045  | 1                           |
----------------------|------------------|-----------------------------| 
|     10              |       301000043  | 0 /* goes into the EXCEPT   |
|                     |                  |         clause */           |
|---------------------|------------------|   /*thus the EXCEPT clause*/|

个人资料-实体类型table:

|---------------------|------------------|
|Id  |EntityType_IDBP |     (...)        |
|---------------------|------------------|
|10  | ProfileType    |          -----   |
|---------------------|------------------|

/*Then on the EntityTable  I would have all the Entities that belong to this 
type and aggregate them all together. Let's imagine it's 10 */

实体Table

|---------------------|------------------|
|Id  |    EntityIDBP  | EntityType_IDBP  | /* 10 entities (records) with this 
|                     |                  |   TypeCod */
|---------------------|------------------|
|10  | IDBP           |      ProfileType | 
|---------------------|------------------|

预期结果:

|---------------------|------------------|
|Id  |    ProfileName |     EntityCount  |
|---------------------|------------------|
|10  | Profile1       |        11        |
|---------------------|------------------|

计数为 11,因为在 Profile_Entity table 上有两 (2) 个 isToInclude = 1 的实体减去 Profile_Entity 中 isToInclude = 0 的 1 个实体(Except 子句)加上该类型的 10 个实体。

观察。语法可能与您习惯的略有不同,因为这是在 Outsystems 平台中完成的。

最终使用临时 table 我从一个配置文件(The Union 和 the Except)中检索所有实体作为同一查询的条件,唯一的区别是我第二次提供一个我要过滤的实体的 IDBP。 所以我有这样的东西

SELECT A.ProfileName, A.ProfileId, Count(A.IDBP)
 FROM (
       SELECT 'all entities IDBP associated with a profile, as well as its Id and Name' as A
       WHERE A.IDBP IN (A WHERE Entity.IDBP = 'xxxx')
      )

这会保留计数并进行过滤