【问题标题】:Spreading the values to other rows but still respecting the total sum将值传播到其他行但仍然尊重总和
【发布时间】:2021-12-24 09:19:34
【问题描述】:

我有一个数据集如下:

date        employee     products     sales
20210101       ben          5         laptop
20210101       ben         10         monitor
20210201       tim         15         laptop
20210301       tim         10         monitor

我想做的是添加另一个字段/列作为这些员工的工作时间。根据员工当天的行数(最多 5-10 行),工作时间应平均分配,但总时间应始终为每天最多 6 小时。

期望的输出应该是:

date        employee     products     sales        hours
20210101       ben          5         laptop         3
20210101       ben         10         monitor        3
20210201       tim         15         laptop         6
20210301       tim         10         monitor        6

我没有任何好主意来执行此查询。如果有人能给我一个关于解决这个问题的方法或方法的提示,我将不胜感激。

【问题讨论】:

  • @CaiusJard 在所需的输出中应该有另一个名为“小时”的列

标签: sql amazon-web-services group-by sum amazon-redshift


【解决方案1】:

很确定 redshift 支持窗口函数..

SELECT *,
  6.0/COUNT(*) OVER(PARTITION BY date, employee) as hours 
FROM dataset

【讨论】:

    猜你喜欢
    • 2021-04-22
    • 1970-01-01
    • 2017-05-07
    • 1970-01-01
    • 2017-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-26
    相关资源
    最近更新 更多