【问题标题】:SQL Query : Add values to columns based on other columnsSQL 查询:根据其他列向列添加值
【发布时间】:2021-05-31 10:21:24
【问题描述】:

我有一个如下所示的数据集:

Part          Runs          Duration          Date
-------------------------------------------------------
random_1       NULL          20              2020-01-01           
random_2       NULL          1               2020-01-01            
random_3       NULL          4               2020-01-01           
tot_rand       40            NULL            2020-01-01           
random_1       NULL          60              2020-01-02           
random_2       NULL          12              2020-01-02            
random_3       NULL          3               2020-01-02           
tot_rand       100           NULL            2020-01-02           
random_1       NULL          9               2020-01-10           
random_2       NULL          4               2020-01-10            
tot_rand       30            NULL            2020-01-10           

现在我想为相同的Date 添加tot_rand 值,而不是Runs 列中的NULL 值。

所以结果会是这样的:

Part          Runs          Duration          Date     
-------------------------------------------------------
random_1       40           20              2020-01-01           
random_2       40           1               2020-01-01            
random_3       40           4               2020-01-01           
tot_rand       40           NULL            2020-01-01           
random_1       100          60              2020-01-02           
random_2       100          12              2020-01-02            
random_3       100          3               2020-01-02           
tot_rand       100          NULL            2020-01-02           
random_1       30           9               2020-01-10           
random_2       30           4               2020-01-10            
tot_rand       30           NULL            2020-01-10   

这样做的原因是,最后我想创建一个名为All 的新列,它会计数:

(Runs - Duration) / Runs

最终结果

Part          Runs          Duration          Date           All
---------------------------------------------------------------------
random_1       40           20              2020-01-01       0.5
random_2       40           1               2020-01-01       0.975     
random_3       40           4               2020-01-01       0.9    
tot_rand       40           NULL            2020-01-01       NULL    
random_1       100          60              2020-01-02       0.4  
random_2       100          12              2020-01-02       0.88    
random_3       100          3               2020-01-02       0.97    
tot_rand       100          NULL            2020-01-02       NULL    
random_1       30           9               2020-01-10       0.7    
random_2       30           4               2020-01-10       0.86     
tot_rand       30           NULL            2020-01-10       NULL

我在 MariaDB / MySQL 环境中工作。

也许还有其他方法可以做到这一点?我愿意接受建议。

【问题讨论】:

  • 如果您使用的是 MariaDB / MySQL,为什么要添加 Microsoft SQL Server 标签?
  • 我没有管理员权限,但只能查询表。有没有办法通过查询得到结果?

标签: mysql sql mariadb


【解决方案1】:

你可以使用窗口函数:

select t.*,
       coalesce(runs, max(runs) over (partition by date)) as imputed_runs
from t;

如果您特别想要 'tot_rand' 值,可以使用条件聚合:

select t.*,
       coalesce(runs,
                 max(case when part = 'tot_rand' then runs end) over (partition by date)
               ) as imputed_runs
from t;

如果您想要update,那么一种方法是:

update t join
       (select date, max(runs) as max_runs
        from t
        where part = 'tot_rand'
        group by date
       ) tt
       on t.date = tt.date
    set runs = tt.max_runs
    where tt.runs is null;

子查询可能不需要聚合,但不清楚在给定日期是否可以有超过 'tot_rand' 的值。

【讨论】:

    【解决方案2】:
    SELECT t1.Part, t2.Runs, t1.Duration, `Date`, 1 - t1.Duration / t2.Runs `All`
    FROM test t1
    JOIN test t2 USING (`Date`)
    WHERE t2.Part = 'tot_rand'
    

    每个Date 值只有一行必须包含Part = 'tot_rand'

    Runs 的值即使不是 NULL 也会被替换。或者你可以在All 计算中使用原始的t1.Runst2.RunsCOALESCE(t1.Runs, t2.Runs)

    https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=440969fc417980aa3688bb4dac67bab2

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-13
      • 2022-12-17
      • 2019-04-04
      相关资源
      最近更新 更多