SQL脚本与所示屏幕截图一起使用。

huangapple go评论60阅读模式
英文:

SQL script to with the shown screenshot

问题

I want to write a sql script to as shown in the screenshot image. Thank you.

I've tried MAX() function to aggregate the ESSBASE_MONTH field to make it distinct and display a single month in the output instead of multiple months. I am yet to figure out how to put 0 in any month that EMPID did not perform any sale like in December under "Total GreaterThan 24 HE Account" and "Total_HE_Accounts"

英文:

I want to write a sql script to as shown in the screenshot image. Thank you.

enter image description here

I've tried MAX() function to aggregate the ESSBASE_MONTH field to make it distinct and display a single month in the output instead of multiple months. I am yet to figure out how to put 0 in any month that EMPID did not perform any sale like in December under "Total GreaterThan 24 HE Account" and "Total_HE_Accounts"

答案1

得分: 1

以下是翻译好的部分:

表格的字段不太详细,但根据屏幕截图,这是我能想到的最好答案。

假设表格名字是SALES;

select 
    ADJ_EMPID, 
    ESSBASE_MONTH, 
    MAX(YTD_COUNT) AS YTD_COUNT, 
    SUM(TOTAL_24) AS TOTAL_24, 
    SUM(TOTAL_ACC) AS TOTAL_ACC 
from SALES
group by 
    ADJ_EMPID, 
    ESSBASE_MONTH

以上的查询将按预期汇总每月的“销售”数据。

要添加“缺失”的行,比如12月,可以通过上面的查询与一个虚拟表的联接来实现。

select 
    MAX(MONTH_NUMBER) AS MONTH_NUMBER,
    ADJ_EMPID, 
    ESSBASE_MONTH, 
    MAX(YTD_COUNT) AS YTD_COUNT, 
    SUM(TOTAL_24) AS TOTAL_24, 
    SUM(TOTAL_ACC) AS TOTAL_ACC 
from (
  select 
        1 as MONTH_NUMBER,
        *
    from SALES 
  union all
  select * from (values 
      (1, '300014366', 'January', 0, 0, 0), 
      (2, '300014366', 'Feburary', 0, 0, 0), 
      -- 根据需要添加其他缺失的月份
      (11, '300014366', 'November', 0, 0, 0), 
      (12, '300014366', 'December', 0, 0, 0)
  ) TEMP_TABLE (MONTH_NUMBER, ADJ_EMPID, ESSBASE_MONTH, YTD_COUNT, TOTAL_24, TOTAL_ACC)
) as AGGREGATED_DATA 
group by 
    ADJ_EMPID, 
    ESSBASE_MONTH
order by MONTH_NUMBER;

TEMP_TABLE 是一个虚拟表,其中包含所有月份和销售额都为零的数据。还添加了一个特殊字段 MONTH_NUMBER 以按正确的顺序排序月份。

这不是最容易理解的查询,要求也不是完全可行的。

链接至 fiddledb,提供了适用于 PostgreSQL 15 的工作解决方案。

英文:

The fields of the table are not very informative however based on screenshot, this is the best answer I could come up with.

Assuming the table name is SALES;

select 
	ADJ_EMPID, 
	ESSBASE_MONTH, 
	MAX(YTD_COUNT) AS YTD_COUNT, 
	SUM(TOTAL_24) AS TOTAL_24, 
	SUM(TOTAL_ACC) AS TOTAL_ACC 
from SALES
group by 
	ADJ_EMPID, 
	ESSBASE_MONTH

The above will aggregate the monthly 'sales' data as expected.

To add the 'missing' rows such as the December, it is possible to do it by doing a union of the above query with a vitural table.

select 
    MAX(MONTH_NUMBER) AS MONTH_NUMBER,
	ADJ_EMPID, 
	ESSBASE_MONTH, 
	MAX(YTD_COUNT) AS YTD_COUNT, 
	SUM(TOTAL_24) AS TOTAL_24, 
	SUM(TOTAL_ACC) AS TOTAL_ACC 
from (
  select 
        1 as MONTH_NUMBER,
  		*
    from SALES 
  union all
  select * from (values 
      (1, '300014366', 'January', 0, 0, 0), 
      (2, '300014366', 'Feburary', 0, 0, 0), 
      -- add the other missing months as required
      (11, '300014366', 'November', 0, 0, 0), 
      (12, '300014366', 'December', 0, 0, 0)
  ) TEMP_TABLE (MONTH_NUMBER, ADJ_EMPID, ESSBASE_MONTH, YTD_COUNT, TOTAL_24, TOTAL_ACC)
) as AGGREGATED_DATA 
group by 
	ADJ_EMPID, 
	ESSBASE_MONTH
order by MONTH_NUMBER;

TEMP_TABLE is a vitural tables which contains all the months and sales as zero. There is a special field MONTH_NUMBER added to sort the months in the proper order.

Not the easiest query to understand, the requirement is not exactly feasible either..

Link to fiddledb for a working solution with PostgreSQL 15.

huangapple
  • 本文由 发表于 2023年2月16日 04:48:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/75465298.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定