根据预定义份额分配给国家但对渠道均等的固定价值

Posted

技术标签:

【中文标题】根据预定义份额分配给国家但对渠道均等的固定价值【英文标题】:Divided fix value based on pre-defined share to countries but euqally to channels 【发布时间】:2021-05-05 13:38:14 【问题描述】:

DB-Fiddle

CREATE TABLE costs (
    id SERIAL PRIMARY KEY,
    event_date DATE,
    country VARCHAR,
    channel VARCHAR,
    costs DECIMAL
);

INSERT INTO costs
(event_date, country, channel, costs)
VALUES 
('2020-02-08', 'DE', 'channel_01', '400'),
('2020-02-08', 'DE', 'channel_02', '400'),
('2020-02-08', 'FR', 'channel_01', '400'),
('2020-02-08', 'FR', 'channel_02', '400'),
('2020-02-08', 'NL', 'channel_01', '400'),
('2020-02-08', 'NL', 'channel_02', '400'),

('2020-04-15', 'DE', 'channel_01', '300'),
('2020-04-15', 'DE', 'channel_02', '300'),
('2020-04-15', 'FR', 'channel_01', '300'),
('2020-04-15', 'FR', 'channel_02', '300'),
('2020-04-15', 'NL', 'channel_01', '300'),
('2020-04-15', 'NL', 'channel_02', '300');

预期结果:

event_date    |    country   |     channel       |            costs              |
--------------|--------------|-------------------|-------------------------------|---------
2020-02-08    |      DE      |     channel_01    |      120  (=400 x 0.6 / 2)    | 
2020-02-08    |      DE      |     channel_02    |      120  (=400 x 0.6 / 2)    |   
2020-02-08    |      FR      |     channel_01    |       60  (=400 x 0.3 / 2)    |   
2020-02-08    |      FR      |     channel_02    |       60  (=400 x 0.3 / 2)    |   
2020-02-08    |      NL      |     channel_01    |       20  (=400 x 0.1 / 2)    |   
2020-02-08    |      NL      |     channel_02    |       20  (=400 x 0.1 / 2)    |  
--------------|--------------|-------------------|-------------------------------|---------
2020-04-15    |      DE      |     channel_01    |       90  (=300 x 0.6 / 2)    |   
2020-04-15    |      DE      |     channel_02    |       90  (=300 x 0.6 / 2)    |   
2020-04-15    |      FR      |     channel_01    |       45  (=300 x 0.3 / 2)    |   
2020-04-15    |      FR      |     channel_02    |       45  (=300 x 0.3 / 2)    |   
2020-04-15    |      NL      |     channel_01    |       15  (=300 x 0.1 / 2)    |   
2020-04-15    |      NL      |     channel_02    |       15  (=300 x 0.1 / 2)    |   

通过来自this question 的以下查询,我可以根据预定义的份额(DE=0.6, FR=0.3, NL=0.1) 每天将成本分配到每个国家/地区。

SELECT
c.event_date,
c.country,
c.channel,
(CASE WHEN c.country IN('DE') THEN c.costs * 0.6 
      WHEN c.country IN('FR') THEN c.costs * 0.3
      WHEN c.country IN('NL') THEN c.costs * 0.1 END) AS costs
FROM costs c
GROUP BY 1,2,3,4
ORDER BY 1,2,3;

但是,正如您在预期结果中看到的那样,我还想将每个国家/地区的费用平均分配给每个渠道。

我需要如何修改查询来实现这一点?

【问题讨论】:

【参考方案1】:

您只需使用窗口函数:

SELECT 
c.event_date, 
c.country, 
c.channel,
c.costs,

  (CASE WHEN c.country IN ('DE') THEN c.costs * 0.6 
        WHEN c.country IN ('FR') THEN c.costs * 0.3
        WHEN c.country IN ('NL') THEN c.costs * 0.1
  END) * (1.0 / COUNT(*) OVER (PARTITION BY c.event_date, c.country)) AS costs_allocated
  
FROM costs c
GROUP BY 1,2,3,4
ORDER BY 1,2,3;

DB-Fiddle

【讨论】:

以上是关于根据预定义份额分配给国家但对渠道均等的固定价值的主要内容,如果未能解决你的问题,请参考以下文章

SD配置步骤清单

将预定义的数字分配给数据框中的列行值

vmware限制 预留 份额

windows 地址空间分配

全球搜索引擎最新市场份额排名

将两个字母的国家代码与预定义的国家代码数组匹配