Skip to content
Advertisement

How to select multiple cells in one specific column from a table

                              TABLE
____________________________________________________________________

UID |               KEY                   |             VALUE             |
6              txtPaymentDate                    Shared Uderlying RSUS
6              txtTerminationReason1               Please Input Data
6              txtUnvestedTest                      Unvested RSUs

My goal is to SELECT Each VALUE to where it has it’s own cell for the input of a report… For example.

SELECT VALUE as txtPaymentDate WHERE Key = 'txtPaymentdate', VALUE as txtTerminationReason1 WHERE key ='txtTerminationReason1' 

ETC…. The end result I am looking for is to have all cells from the value column as their own column individually so I can input them in a telerik report using field inputs.

Thanks

Advertisement

Answer

Your table is a typical unnormalized key-value store. You may achieve what you want by aggregating on the UID column and the pivoting out the values, given each key:

SELECT
    UID,
    MAX(CASE WHEN "KEY" = 'txtPaymentDate'        THEN VALUE END) AS txtPaymentDate,
    MAX(CASE WHEN "KEY" = 'txtTerminationReason1' THEN VALUE END) AS txtTerminationReason1,
    MAX(CASE WHEN "KEY" = 'txtUnvestedTest'       THEN VALUE END) AS txtUnvestedTest
FROM yourTable
GROUP BY
    UID;

Note the KEY is usually a reserved keyword in most versions of SQL. Hence, I placed your KEY column in double quotes. You should avoid naming your database objects using keywords.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement