【问题标题】:How do I fill in null values with all rows based on group in Oracle SQL 19?如何在 Oracle SQL 19 中根据组填充所有行的空值?
【发布时间】:2020-07-22 19:55:38
【问题描述】:

我在 Oracle SQL 19 中有下表:

+--------------+--------------+-------------------+------------------+-------------------+
|     Team     | Team Ranking | Team Ranking Date | Player Join Date |      Player       |
+--------------+--------------+-------------------+------------------+-------------------+
| FC Barcelona |            1 | 15-Jul-20         | 15-Jul-20        | Lionel Messi      |
| FC Barcelona |            1 | 15-Jul-20         | 15-Jul-20        | Luis Suarez       |
| FC Barcelona |            1 | 15-Jul-20         | 15-Jul-20        | Antoine Griezmann |
| FC Barcelona |            1 | 15-Jul-20         | 15-Jul-20        | Ousmane Dembele   |
| FC Barcelona |            3 | 16-Jul-20         | (null)           | (null)            |
| FC Barcelona |            5 | 17-Jul-20         | (null)           | (null)            |
| Real Madrid  |            2 | 15-Jul-20         | 15-Jul-20        | Eden Hazard       |
| Real Madrid  |            2 | 15-Jul-20         | 15-Jul-20        | Sergio Ramos      |
| Real Madrid  |            2 | 15-Jul-20         | 15-Jul-20        | Karim Benzema     |
| Real Madrid  |            2 | 15-Jul-20         | 15-Jul-20        | Gareth Bale       |
| Real Madrid  |            2 | 15-Jul-20         | 16-Jul-20        | James Rodriguez   |
| Real Madrid  |            5 | 25-Jul-20         | (null)           | (null)            |
| Real Madrid  |            2 | 28-Jul-20         | (null)           | (null)            |
+--------------+--------------+-------------------+------------------+-------------------+

我想用球队的所有球员信息填写上表中的空值。换句话说,我希望结果如下所示:

+--------------+--------------+-------------------+------------------+-------------------+
|     Team     | Team Ranking | Team Ranking Date | Player Join Date |      Player       |
+--------------+--------------+-------------------+------------------+-------------------+
| FC Barcelona |            1 | 15-Jul-20         | 15-Jul-20        | Lionel Messi      |
| FC Barcelona |            1 | 15-Jul-20         | 15-Jul-20        | Luis Suarez       |
| FC Barcelona |            1 | 15-Jul-20         | 15-Jul-20        | Antoine Griezmann |
| FC Barcelona |            1 | 15-Jul-20         | 15-Jul-20        | Ousmane Dembele   |
| FC Barcelona |            3 | 16-Jul-20         | 15-Jul-20*       | Lionel Messi*     |
| FC Barcelona |            3 | 16-Jul-20         | 15-Jul-20*       | Luis Suarez*      |
| FC Barcelona |            3 | 16-Jul-20         | 15-Jul-20*       | Antoine Griezmann*|
| FC Barcelona |            3 | 16-Jul-20         | 15-Jul-20*       | Ousmane Dembele*  |
| FC Barcelona |            5 | 17-Jul-20         | 15-Jul-20*       | Lionel Messi*     |
| FC Barcelona |            5 | 17-Jul-20         | 15-Jul-20*       | Luis Suarez*      |
| FC Barcelona |            5 | 17-Jul-20         | 15-Jul-20*       | Antoine Griezmann*|
| FC Barcelona |            5 | 17-Jul-20         | 15-Jul-20*       | Ousmane Dembele*  |
| Real Madrid  |            2 | 15-Jul-20         | 15-Jul-20        | Eden Hazard       |
| Real Madrid  |            2 | 15-Jul-20         | 15-Jul-20        | Sergio Ramos      |
| Real Madrid  |            2 | 15-Jul-20         | 15-Jul-20        | Karim Benzema     |
| Real Madrid  |            2 | 15-Jul-20         | 15-Jul-20        | Gareth Bale       |
| Real Madrid  |            2 | 15-Jul-20         | 16-Jul-20        | James Rodriguez   |
| Real Madrid  |            5 | 25-Jul-20         | 15-Jul-20*       | Eden Hazard*      |
| Real Madrid  |            5 | 25-Jul-20         | 15-Jul-20*       | Sergio Ramos*     |
| Real Madrid  |            5 | 25-Jul-20         | 15-Jul-20*       | Karim Benzema*    |
| Real Madrid  |            5 | 25-Jul-20         | 15-Jul-20*       | Gareth Bale*      |
| Real Madrid  |            5 | 25-Jul-20         | 16-Jul-20*       | James Rodriguez*  |
| Real Madrid  |            2 | 28-Jul-20         | 15-Jul-20*       | Eden Hazard*      |
| Real Madrid  |            2 | 28-Jul-20         | 15-Jul-20*       | Sergio Ramos*     |
| Real Madrid  |            2 | 28-Jul-20         | 15-Jul-20*       | Karim Benzema*    |
| Real Madrid  |            2 | 28-Jul-20         | 15-Jul-20*       | Gareth Bale*      |
| Real Madrid  |            2 | 28-Jul-20         | 16-Jul-20*       | James Rodriguez*  |
+--------------+--------------+-------------------+------------------+-------------------+

我在上表中为填写的值加了星标。

在这个例子中,我只展示了每个团队的两个排名变化。但是一个团队的排名可以无限次地变化。

有人可以告诉我如何在 Oracle SQL 19 中执行此操作吗?

任何帮助将不胜感激!

下面是在Oracle中生成表的sql查询,方便大家使用:

with tbl as (
    select 'FC Barcelona' team, 1 team_ranking, to_date('7/15/2020 09:29', 'MM/DD/YYYY HH24:MI') team_ranking_date, to_date('7/15/2020 11:31', 'MM/DD/YYYY HH24:MI') player_join_date,  'Lionel Messi' player from dual union
    select 'FC Barcelona' team, 1 team_ranking, to_date('7/15/2020 09:29', 'MM/DD/YYYY HH24:MI') team_ranking_date, to_date('7/15/2020 11:31', 'MM/DD/YYYY HH24:MI') player_join_date,  'Luis Suarez' player from dual union
    select 'FC Barcelona' team, 1 team_ranking, to_date('7/15/2020 09:29', 'MM/DD/YYYY HH24:MI') team_ranking_date, to_date('7/15/2020 11:31', 'MM/DD/YYYY HH24:MI') player_join_date,  'Antoine Griezmann' player from dual union
    select 'FC Barcelona' team, 1 team_ranking, to_date('7/15/2020 09:29', 'MM/DD/YYYY HH24:MI') team_ranking_date, to_date('7/15/2020 11:31', 'MM/DD/YYYY HH24:MI') player_join_date,  'Ousmane Dembele' player from dual union
    select 'FC Barcelona' team, 3 team_ranking, to_date('7/16/2020 04:18', 'MM/DD/YYYY HH24:MI') team_ranking_date, null player_join_date,  null player from dual union
    select 'FC Barcelona' team, 5 team_ranking, to_date('7/17/2020 06:54', 'MM/DD/YYYY HH24:MI') team_ranking_date, null player_join_date,  null player from dual union
    select 'Real Madrid' team, 2 team_ranking, to_date('7/15/2020 09:29', 'MM/DD/YYYY HH24:MI') team_ranking_date, to_date('7/15/2020 11:31', 'MM/DD/YYYY HH24:MI') player_join_date,  'Eden Hazard' player from dual union
    select 'Real Madrid' team, 2 team_ranking, to_date('7/15/2020 09:29', 'MM/DD/YYYY HH24:MI') team_ranking_date, to_date('7/15/2020 11:31', 'MM/DD/YYYY HH24:MI') player_join_date,  'Sergio Ramos' player from dual union
    select 'Real Madrid' team, 2 team_ranking, to_date('7/15/2020 09:29', 'MM/DD/YYYY HH24:MI') team_ranking_date, to_date('7/15/2020 11:31', 'MM/DD/YYYY HH24:MI') player_join_date,  'Karim Benzema' player from dual union
    select 'Real Madrid' team, 2 team_ranking, to_date('7/15/2020 09:29', 'MM/DD/YYYY HH24:MI') team_ranking_date, to_date('7/15/2020 11:31', 'MM/DD/YYYY HH24:MI') player_join_date,  'Gareth Bale' player from dual union
    select 'Real Madrid' team, 2 team_ranking, to_date('7/15/2020 09:29', 'MM/DD/YYYY HH24:MI') team_ranking_date, to_date('7/16/2020 14:44', 'MM/DD/YYYY HH24:MI') player_join_date,  'James Rodriguez' player from dual union
    select 'Real Madrid' team, 5 team_ranking, to_date('7/25/2020 10:02', 'MM/DD/YYYY HH24:MI') team_ranking_date, null player_join_date,  null player from dual union
    select 'Real Madrid' team, 2 team_ranking, to_date('7/28/2020 06:13', 'MM/DD/YYYY HH24:MI') team_ranking_date, null player_join_date,  null player from dual
)
select *
from tbl
order by team, team_ranking_date, player_join_date

【问题讨论】:

  • 将空值更新为特定值的模式是什么?

标签: sql oracle


【解决方案1】:

您需要分别考虑具有空玩家和非空玩家的集合。 直接取非空玩家的集合。其余部分应用外连接和交叉连接,并通过UNION ALL 组合所有结果,如下所示:

with  tbl2 as
  (    
    select * from tbl where player is not null
    union all
    select tt1.team, tt2.team_ranking, tt2.team_ranking_date, tt1.player_join_date, tt1.player
      from
      (
       (select t2.*
          from ( select * from tbl where player is null ) t1
         right join ( select * from tbl where player is not null ) t2 on t2.player = t1.player ) tt1
       cross join      
       (select t1.*
          from ( select * from tbl where player is null ) t1
         left join ( select * from tbl where player is not null ) t2 on t2.player = t1.player ) tt2
      )          
  )
 select * 
   from tbl2 
  order by team, team_ranking_date, player_join_date

【讨论】:

    【解决方案2】:

    如果您只有 一个 表(正如您在问题中所写),您应该为 TEAMS、PLAYERS、TEAMPLAYERS 和 TEAMRANKINGS 创建表。一旦你有了这些表,你就可以编写一个查询来给你所需的结果。 (您只需要创建一次表!根据需要运行查询...)

    表格

    create table teams
    as
    select unique team from tbl ; -- TBL: your original table
    
    create table players 
    as
    select unique player from tbl where player is not null ;
    
    -- map players <-> teams
    create table teamplayers
    as
    select unique team, player, player_join_date 
    from tbl
    where player is not null ;
    
    -- each team will have multiple rankings
    create table teamrankings
    as
    select unique team, team_ranking, team_ranking_date
    from tbl ;
    
    -- check: 
    select * from teams ;        -- 2 rows
    select * from players ;      -- 9 rows
    select * from teamplayers ;  -- 9 rows
    select * from teamrankings ; -- 6 rows
    

    查询

    select 
      T.team
    , TR.team_ranking
    , TR.team_ranking_date
    , TP.player_join_date
    , P.player
    from teams T
      join teamplayers TP  on T.team = TP.team
      join players P       on TP.player = P.player
      join teamrankings TR on T.team = TR.team
    order by T.team, TR.team_ranking_date, P.player
    ; 
    

    结果

               TEAM    TEAM_RANKING    TEAM_RANKING_DATE    PLAYER_JOIN_DATE               PLAYER 
    _______________ _______________ ____________________ ___________________ ____________________ 
    FC Barcelona                  1 15-JUL-20            15-JUL-20           Antoine Griezmann    
    FC Barcelona                  1 15-JUL-20            15-JUL-20           Lionel Messi         
    FC Barcelona                  1 15-JUL-20            15-JUL-20           Luis Suarez          
    FC Barcelona                  1 15-JUL-20            15-JUL-20           Ousmane Dembele      
    FC Barcelona                  3 16-JUL-20            15-JUL-20           Antoine Griezmann    
    FC Barcelona                  3 16-JUL-20            15-JUL-20           Lionel Messi         
    FC Barcelona                  3 16-JUL-20            15-JUL-20           Luis Suarez          
    FC Barcelona                  3 16-JUL-20            15-JUL-20           Ousmane Dembele      
    FC Barcelona                  5 17-JUL-20            15-JUL-20           Antoine Griezmann    
    FC Barcelona                  5 17-JUL-20            15-JUL-20           Lionel Messi         
    FC Barcelona                  5 17-JUL-20            15-JUL-20           Luis Suarez          
    FC Barcelona                  5 17-JUL-20            15-JUL-20           Ousmane Dembele      
    Real Madrid                   2 15-JUL-20            15-JUL-20           Eden Hazard          
    Real Madrid                   2 15-JUL-20            15-JUL-20           Gareth Bale          
    Real Madrid                   2 15-JUL-20            16-JUL-20           James Rodriguez      
    Real Madrid                   2 15-JUL-20            15-JUL-20           Karim Benzema        
    Real Madrid                   2 15-JUL-20            15-JUL-20           Sergio Ramos         
    Real Madrid                   5 25-JUL-20            15-JUL-20           Eden Hazard          
    Real Madrid                   5 25-JUL-20            15-JUL-20           Gareth Bale          
    Real Madrid                   5 25-JUL-20            16-JUL-20           James Rodriguez      
    Real Madrid                   5 25-JUL-20            15-JUL-20           Karim Benzema        
    Real Madrid                   5 25-JUL-20            15-JUL-20           Sergio Ramos         
    Real Madrid                   2 28-JUL-20            15-JUL-20           Eden Hazard          
    Real Madrid                   2 28-JUL-20            15-JUL-20           Gareth Bale          
    Real Madrid                   2 28-JUL-20            16-JUL-20           James Rodriguez      
    Real Madrid                   2 28-JUL-20            15-JUL-20           Karim Benzema        
    Real Madrid                   2 28-JUL-20            15-JUL-20           Sergio Ramos  
    

    您还应该添加一些约束,例如

    -- team names must be unique
    alter table teams 
    add constraint teams_unique unique( team ) ;
    
    -- player names must be unique
    alter table players 
    add constraint players_unique unique( player ) ;
    
    -- allow the user to add rankings for existing teams only
    alter table teamrankings
    add constraint team_fkey foreign key( team ) 
    references teams( team ) ;
    
    -- (existing) players can be added to existing teams only
    alter table teamplayers
    add (
      constraint teamplayers_team_fkey foreign key( team )
        references teams( team )
    , constraint teamplayers_player_fkey foreign key( player )
        references players( player ) 
    ) ;
    

    如果你想增加更多的排名,你只需要INSERT到TEAMRANKINGS表中, 并运行与以前相同的查询(请参阅DBfiddle)。请注意,您现在不需要插入 PLAYER_JOIN_DATE 和 PLAYER - 这是原始表的问题(即添加新排名时必须插入 NULL)。

    insert into teamrankings( team, team_ranking, team_ranking_date )
    values( 'Real Madrid', 7, sysdate ) ;
    

    替代方案

    如果您出于某种原因无法创建新表,您也可以使用与上述相同的查询,但不要使用表名,而是使用我们用于创建 4 个表的查询:

    -- TBL: your original table
    select 
      T.team
    , TR.team_ranking
    , TR.team_ranking_date
    , TP.player_join_date
    , P.player
    from ( select unique team from tbl ) T
      join ( 
        select unique team, player, player_join_date 
        from tbl
        where player is not null 
      ) TP on T.team = TP.team
      join (
        select unique player from tbl where player is not null 
      ) P  on TP.player = P.player
      join ( 
        select unique team, team_ranking, team_ranking_date
        from tbl
      ) TR on T.team = TR.team
    order by T.team, TR.team_ranking_date, P.player
    ;
    

    DBfiddle here.

    【讨论】:

      【解决方案3】:

      如果我理解正确,您有两个“实体”要合并:一方面是不同球队和排名的列表,另一方面是球员列表。

      我们的想法是为每一个选择不同的值,然后交叉连接它们:

      select *
      from (select distinct team, team_ranking, team_ranking_date from tbl) t  join
           (select distinct team, player_join_date, player
            from tbl
            where player is not null
           ) p
           using (team)
      order by team, team_ranking_date, player_join_date;
      

      Here 是一个 dbfiddle。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-06
        • 2023-01-14
        • 1970-01-01
        • 2021-03-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多