0

hello we have a small project for making reservations of our assets.
we are holding it as following table 1.
we want to have query result like table 2 for pretty printing
but I couldnt manage to do it,
if you have previously solve similar issues, can you help me like which subjects should I search in oracle database or PLSQL?

existing
TABLE 1
ASSET_NAME, USER_ID,  RESERVATION_PERIOD 
PS1, 41 , AM
PS1, 1  , PM
PS2, -  , - 
PS3, -  , - 
PS4, -  , - 

new table
TABLE 2
ASSET_NAME, AM,  PM
PS1, 41 , 1
PS2, -  , - 
PS3, (user_id)  , (user_id if it is allocated)
PS4, -  , - 
4

2 回答 2

0
SELECT  asset_name
,       NVL(MAX(CASE WHEN reservation_period = 'AM' THEN user_id ELSE NULL END), '-') AS am_user_id
,       NVL(MAX(CASE WHEN reservation_period = 'PM' THEN user_id ELSE NULL END), '-') AS pm_user_id
FROM    table_1
GROUP   BY
        asset_name;
于 2013-09-21T00:34:06.190 回答
0

简单的选择应该可以工作。阅读 #CASE #ANALYTICS #CONNECT BY 等。注意:下面将有 2 行用于 PS1,此 SQL 必须使用 CONNECT BY 子句进行修改

INSERT INTO
      TABLE2 ( ASSET_NAME,
             AM,
             PM )
    ( SELECT
           ASSET_NAME,
           CASE
               WHEN USER_ID IS NOT NULL
                   AND RESERVATION_PERIOD = 'AM'
               THEN
                   USER_ID
               ELSE
                   NULL
           END
               AS AM,
           CASE
               WHEN USER_ID IS NOT NULL
                   AND RESERVATION_PERIOD = 'PM'
               THEN
                   USER_ID
               ELSE
                   NULL
           END
               AS PM
     FROM
           TABLE1 );
于 2013-09-20T13:45:56.063 回答