简体   繁体   中英

How do I transpose rows into columns dynamically in SQL?

To give you a brief summary—I would like to:

1. Join two tables both containing Address Book information,

2. Concatenate the 'phone number area code' and 'phone number' (because they are stored in separate columns in our database)

3. If the 'Address Book #' from Column 1 is repeated—have the information appear on the same row (see below for example).


Here are the tables and fields I am using:

Fields in Table 1 (ABC_F0115):
'Address Book#'
'Phone Type'
'Phone Area Code'
“Phone Number'

Fields in Table 2 (ABC_F0101):
'Address Book#'
'Address Book Type'

My current SQL is:

SELECT WPAN8 'Address Book#', WPPHTP 'Phone Type', WPAR1 'Phone Area Code', WPPH1 'Phone Number', ABAT1 'Address Book concat('(',LTRIM(RTRIM(WPAR1)),')', ' ', WPPH1) AS 'Full Phone Number' FROM ABC.F0101 JOIN ABC.F0115 ON WPAN8=ABAN8 WHERE ABAT1='AR';



It returns a table similar to the one below:


|AddressBook#| PhoneType | PhoneAreaCode | Phone Number |
|___________ _ _________ _ _____________ _ ___________|
|2160________| Cell______| 000__________| 000-0000_____ |
|2160________| Fax______ | 111__________| 111-1111______ |
|2161________| Cell______| 222__________| 222-2222_____ |
|2161________| Fax______ | 333__________| 333-3333_____ |
|2162________| Home____ | 444__________| 444-4444_____ |


However, I would like the returned table to look like:


|AddressBook#| PhoneType1 | Phone#1 | PhoneType2 | Phone#2 | PhoneType3 | Phone#3 |
|___________ _ _________ _ _______ _ _________ _ _________ _ _______ _ _______ |
|2160________| Cell_______| 000-0000| Fax _______ |111-1111_|___________|________|
|2161________| Cell_______| 222-2222| Fax _______ |333-3333 |___________|________|
|2163________| Home_____| 444-4444| Fax _______ |333-3333 |___________|________ |


I know I have to transpose my rows into columns dynamically... but I can't quite figure out my SQL. Any suggestions?

I guess that is something you are locking fore?

WITH Result AS (
    SELECT
        WPAN8,
        WPPHTP,
        WPPH1 'Phone Number',
        ROW_NUMBER() OVER (PARTITION BY WPAN8, ORDER BY WPPHTP,WPPH1) AS CallRowNumber
    FROM ABC.F0101
    JOIN ABC.F0115 ON WPAN8=ABAN8
    WHERE ABAT1='AR';
)
SELECT
    WPAN8 'Address Book#',
    Resutl1.WPPHTP AS 'Phone Type1',
    Resutl1.WPPH1 'Phone Number1',
    Resutl2.WPPHTP AS 'Phone Type2',
    Resutl2.WPPH1 'Phone Number2',
    Resutl3.WPPHTP AS 'Phone Type3',
    Resutl3.WPPH1 'Phone Number3',
FROM ABC.F0101
LEFT JOIN Result AS Resutl1 ON Resutl1.WPAN8=ABAN8 AND CallRowNumber = 1
LEFT JOIN Result AS Resutl2 ON Resutl2.WPAN8=ABAN8 AND CallRowNumber = 2
LEFT JOIN Result AS Resutl3 ON Resutl3.WPAN8=ABAN8 AND CallRowNumber = 3

Since I didn't know which SQL syntax you used, I did it with SQL Server (TSQL).

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