合并联合中的行,将空列替换为带有值的列

时间:2019-06-29 02:29:15

标签: postgresql

我有一个查询,它从其他两个查询中得出UNION。我想做的是将这些结果按organizationIduserId进行分组,但这给了我一个错误。

  

错误:列“ user_organizations.admin”必须出现在GROUP BY子句中或在聚合函数中使用   第4行:user_organizations.admin,

要求将 ALL 列添加到GROUP BY

以下是查询:

SELECT
    user_organizations. "organizationId",
    user_organizations. "userId",
    user_organizations.admin,
    user_organizations.owner,
    user_organizations.member
FROM ((
        SELECT
            organizations.id AS "organizationId",
            organizations. "ownedById" AS "userId",
            NULL AS admin,
            NULL AS member,
            TRUE AS OWNER
        FROM
            organizations)
    UNION (
        SELECT
            organizations.id AS "organizationId",
            memberships. "userId",
            memberships.admin,
            TRUE AS member,
            NULL AS OWNER
        FROM
            memberships
        LEFT JOIN organizations ON memberships. "organizationId" = organizations.id)) AS user_organizations;

以下是结果本身: enter image description here

您会看到1, 4有两列,我想将这些列合并,因此只有一行,1, 4, TRUE, TRUE, TRUE在那。是否可以像这样合并行,并向上插入NULL值?

3 个答案:

答案 0 :(得分:1)

这看起来可以简化很多:

SELECT
    coalesce("organizationId", organizations.id) as "organizationId",
    coalesce("ownedById", "userId") as "userId",
    memberships.admin,
    CASE WHEN memberships.id IS NULL THEN TRUE ELSE NULL END AS owner,
    CASE WHEN memberships.id IS NOT NULL THEN TRUE ELSE NULL END AS member
FROM organizations
FULL OUTER JOIN memberships on memberships."organizationId" = organizations.id
            AND memberships."userId" = organizations."ownedById"
;

答案 1 :(得分:0)

您需要在前两列上执行分组依据,并在其余三列上使用汇总。

DatabaseReference reference = FirebaseDatabase.getInstance().getReference("message");

reference.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot dataSnapshots : dataSnapshot.getChildren()){

           if (dataSnapshots.child("sfasf").exists()) {
                    dataSnapshots.child("sfasf").removeValue();
           } 

        }
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {

    }


});

答案 2 :(得分:0)

WITH org AS (
    SELECT
        organizations.id AS "organizationId",
        organizations. "ownedById" AS "userId",
        TRUE AS OWNER,
        NULL AS admin,
        NULL AS member
    FROM
        organizations
),
memberships_free AS (
    SELECT
        memberships. "organizationId" AS "organizationId",
        memberships. "userId" AS "userId",
        CASE WHEN org.owner is NULL THEN FALSE ELSE org.owner END AS owner,
--        org.owner as owner,
        memberships.admin,
        TRUE:: boolean AS member
    FROM
        memberships
    LEFT JOIN org ON memberships. "userId" = org. "userId"
        AND memberships. "organizationId" = org. "organizationId"
),
organizations_not_in_memberships AS (
    SELECT
        *
    FROM
        organizations
WHERE
    NOT EXISTS (
        SELECT
            *
        FROM
            memberships_free
        WHERE
            memberships_free. "organizationId" = organizations.id
            AND memberships_free. "userId" = organizations. "ownedById")
),
org_appendable AS (
    SELECT
        organizations_not_in_memberships.id AS "organizationId",
        organizations_not_in_memberships. "ownedById" AS "userId",
        TRUE:: boolean AS OWNER,
        FALSE AS admin,
        FALSE AS member
    FROM
        organizations_not_in_memberships
)

SELECT * FROM memberships_free
UNION
SELECT * FROM org_appendable

 WITH org AS (
         SELECT organizations.id AS "organizationId",
            organizations."ownedById" AS "userId",
            true AS owner,
            NULL::text AS admin,
            NULL::text AS member
           FROM organizations
        ), memberships_free AS (
         SELECT memberships."organizationId",
            memberships."userId",
                CASE
                    WHEN org.owner IS NULL THEN false
                    ELSE org.owner
                END AS owner,
            memberships.admin,
            true AS member
           FROM memberships
             LEFT JOIN org ON memberships."userId" = org."userId" AND memberships."organizationId" = org."organizationId"
        ), organizations_not_in_memberships AS (
         SELECT organizations.id,
            organizations.uuid,
            organizations."displayName",
            organizations.name,
            organizations."deletedAt",
            organizations."createdAt",
            organizations."updatedAt",
            organizations."createdById",
            organizations."ownedById"
           FROM organizations
          WHERE NOT (EXISTS ( SELECT memberships_free."organizationId",
                    memberships_free."userId",
                    memberships_free.owner,
                    memberships_free.admin,
                    memberships_free.member
                   FROM memberships_free
                  WHERE memberships_free."organizationId" = organizations.id AND memberships_free."userId" = organizations."ownedById"))
        ), org_appendable AS (
         SELECT organizations_not_in_memberships.id AS "organizationId",
            organizations_not_in_memberships."ownedById" AS "userId",
            true AS owner,
            false AS admin,
            false AS member
           FROM organizations_not_in_memberships
        )
 SELECT memberships_free."organizationId",
    memberships_free."userId",
    memberships_free.owner,
    memberships_free.admin,
    memberships_free.member
   FROM memberships_free
UNION
 SELECT org_appendable."organizationId",
    org_appendable."userId",
    org_appendable.owner,
    org_appendable.admin,
    org_appendable.member
   FROM org_appendable;