简体   繁体   中英

Need help transposing rows to columns in Oracle SQL query

I have 2 different data sources in an Oracle DB. This is just a SQL connection to the Oralce DB (not PL SQL).

1st source is an application table with an app ID and a large list of attributes 2nd source is a table of contacts. each contact has an app ID (logical_name), a type, a contact an email and some other properties.

I want to put them in an application table in a target location that

I searched here and based on what I learned ended up trying to join my app table to this sub-query:

select 
max(logical_name) logical_name,
MAX(CASE WHEN type='APPLICATION SUPPORT OWNER' THEN contact END) Application_Support_Owner,
MAX(CASE WHEN type='APPLICATION SUPPORT OWNER' THEN email END) App_Support_Owner_EMAIL,
MAX(CASE WHEN type='APPLICATION SUPPORT - PRIMARY' THEN contact END) Application_Support_Primary,
MAX(CASE WHEN type='APPLICATION SUPPORT - PRIMARY' THEN email END) App_Support_Primary_EMAIL,
MAX(CASE WHEN type='BUSINESS CONTACT - PRIMARY' THEN contact END) Business_Contact_Primary,
MAX(CASE WHEN type='BUSINESS CONTACT - PRIMARY' THEN email END) Business_Contact_Primary_EMAIL

from contact_table
group by logical_name

What I discovered when I looked at the output is that in some cases there is more than one contact of a specific type (despite names like "primary" and "owner"). Because contacts are "Last, First" format and emails are First.Last@company.com, the Max function is mixing and matching contacts and emails.

I'm thinking the best answer for multiple values is just to get the contact and email from one record and throw the other one away. It is simpler and we only really want one contact for each role (ie of each type).

I'm a bit of a SQL noob. Is there a function I could use other than Max() or a different syntax that would yield two fields from the same record? I only want one row of data for each app so my join doesn't have multiple records for each app. Thanks in advance for your assistance.

Use KEEP with the aggregation function to correlate the aggregations:

select max(logical_name) logical_name,
       MAX(CASE WHEN type='APPLICATION SUPPORT OWNER' THEN contact END)
         AS Application_Support_Owner,
       MAX(CASE WHEN type='APPLICATION SUPPORT OWNER' THEN email END)
         KEEP(
           DENSE_RANK LAST
           ORDER BY CASE WHEN type='APPLICATION SUPPORT OWNER' THEN contact END NULLS FIRST
         ) AS App_Support_Owner_EMAIL,
       MAX(CASE WHEN type='APPLICATION SUPPORT - PRIMARY' THEN contact END)
         AS Application_Support_Primary,
       MAX(CASE WHEN type='APPLICATION SUPPORT - PRIMARY' THEN email END)
         KEEP(
           DENSE_RANK LAST
           ORDER BY CASE WHEN type='APPLICATION SUPPORT - PRIMARY' THEN contact END NULLS FIRST
         ) AS App_Support_Primary_EMAIL,
       MAX(CASE WHEN type='BUSINESS CONTACT - PRIMARY' THEN contact END)
         AS Business_Contact_Primary,
       MAX(CASE WHEN type='BUSINESS CONTACT - PRIMARY' THEN email END)
         KEEP(
           DENSE_RANK LAST
           ORDER BY CASE WHEN type='APPLICATION SUPPORT - PRIMARY' THEN contact END NULLS FIRST
         ) AS Business_Contact_Primary_EMAIL
from     contact_table
group by logical_name;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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