你好,stackoverflow的高手。我需要SQL帮助。

情况:需要过滤掉工人并增加他们的工作时间。

数据库:

worker1    worker2    time_spent_in_seconds
X                     60
X          Y          60
Z          X          60


前提:如果worker1和worker2一起工作,则他们的时间除以2
我期望得到的结果:

X=120
Y=30
Z=30


谢谢!

最佳答案

您可以通过使用工会全部尝试以下方式

    select worker,sum(val) from
    (   select worker1 as worker , case when worker2 is not null
     then  time_spent/2 else time_spent end as val from test
    union all
    select worker2 , case when worker2 is not null
     then  time_spent/2 else time_spent end as val from test
    ) t where worker is not null
    group by worker

worker  sum(val)
X       120
Y       30
Z       30


Demo link

关于php - SQL-两个 worker 一次过滤,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53628559/

10-10 16:28