PostgreSQL 组合输出

问题描述 投票:0回答:1
with type as (
  select
                A.AREA_CODE || '-' || SUBSTR(A.DIAL_NUMBER,1,3) || '-' || SUBSTR(A.DIAL_NUMBER,4) ph,
                A.SOURCE_RECORD_OID,DESCRIPTION
        from
                tel A,
                tclient B
        where
                A.SOURCE_RECORD_TABLE = 'ASSO'
                and B.OID = A.poid and DESCRIPTION in ('Work Cell','Personal Cell','Work Phone','Home Phone'))
select distinct CA.userid,
        Cep.EMPLOYEEID,
        CA.GIVEN_NAME as firstName,
        CA.FAMILY_NAME as lastName,
        CA.MIDDLE_NAME as middleName,
        NVL(CA.BUSINESS_EMAIL_ADDRESS ,
        CA.PERSONAL_EMAIL_ADDRESS) EMAILADDRESS,
        case when type_client.DESCRIPTION = 'Work Cell' then type_client.ph end BUSINESSMOBILE,
        case when type_client.DESCRIPTION = 'Personal Cell' then type_client.ph end PERSONALMOBILE,
        case when type_client.DESCRIPTION = 'Work Phone' then type_client.ph end BUSINESSPHONE,
        case when type_client.DESCRIPTION = 'Home Phone' then type_client.ph end PERSONALPHONE
        from type type_client,
        GTT_TIMECARDEMP P,
        assoc CA,
        cepos cep
        where cep.employeeid = p.supervisorid
        and cep.userid = CA.userid
        and CA.OID = type_client.SOURCE_RECORD_OID;

上述查询的输出:

  firstname  | lastname | middlename |         emailaddress          | businessmobile | personalmobile | businessphone | personalphone
------------+----------+------------+-------------------------------+----------------+----------------+---------------+---------------
 a          | b        |            |                               |                | yyy-xxx-0121   |               |
 a          | b        |            |                               | yyy-xxx-0121   |                |               |
 a          | b        |            |                               |                |                |               | yyy-xxx-0121
 b          | c        |            |                               |                | xxx-xxx-4869   |               |
 d          | e        |            |                               |                | xxx-xxx-2299   |               |
 f          | g        |            |                               |                | xxx-xxx-0496   |               |

预期产出 如果一名员工拥有所有类型的电话号码,那么它应该排成单行,或者如果他/她有多个电话号码,那么它应该排成单行

   firstname  | lastname | middlename |         emailaddress          | businessmobile | personalmobile | businessphone | personalphone
--+------------+----------+------------+-------------------------------+----------------+----------------+---------------+---------------
  | a          | b        |            |                               | yyy-xxx-0121   | yyy-xxx-0121   |               | yyy-xxx-0121
  | b          | c        |            |                               |                | xxx-xxx-4869   |               |
  | d          | e        |            |                               |                | xxx-xxx-2299   |               |
  | f          | g        |            |                               |                | xxx-xxx-0496   |               |

postgresql pivot
1个回答
0
投票

您的行会成倍增加,因为每个数字都是一个单独的行,独立地连接到其目标。正如 O 已经指出。 Jones 在评论中,您可以在加入之前旋转它们。

您可以使用聚合

filter
子句来实现:

with type as (
  select concat_ws('-',A.AREA_CODE,SUBSTR(A.DIAL_NUMBER,1,3)
                                  ,SUBSTR(A.DIAL_NUMBER,4)) as ph
        ,A.SOURCE_RECORD_OID
        ,DESCRIPTION
  from tel A join tclient B 
    on     A.poid        =B.OID
   and A.SOURCE_RECORD_TABLE = 'ASSO'
   and DESCRIPTION in ('Work Cell','Personal Cell','Work Phone','Home Phone') )
,type2 as (
  select SOURCE_RECORD_OID,
         min(ph)filter(where DESCRIPTION = 'Work Cell')     as BUSINESSMOBILE,
         min(ph)filter(where DESCRIPTION = 'Personal Cell') as PERSONALMOBILE,
         min(ph)filter(where DESCRIPTION = 'Work Phone')    as BUSINESSPHONE,
         min(ph)filter(where DESCRIPTION = 'Home Phone')    as PERSONALPHONE
   from type
   group by SOURCE_RECORD_OID )
select distinct CA.userid,
        Cep.EMPLOYEEID,
        CA.GIVEN_NAME as firstName,
        CA.FAMILY_NAME as lastName,
        CA.MIDDLE_NAME as middleName,
        NVL(CA.BUSINESS_EMAIL_ADDRESS ,
        CA.PERSONAL_EMAIL_ADDRESS) EMAILADDRESS,
        BUSINESSMOBILE,
        PERSONALMOBILE,
        BUSINESSPHONE,
        PERSONALPHONE
from type2 as type_client join assoc as ca 
 on type_client.SOURCE_RECORD_OID = ca.OID
join cepos as cep on cep.userid = CA.userid
join GTT_TIMECARDEMP as p on cep.employeeid = p.supervisorid;
© www.soinside.com 2019 - 2024. All rights reserved.