将数据从聚合级别更改为粒度级别
Change data from aggregated to granular level
我的数据是这样的:
重现:
DROP TABLE IF EXISTS SALARY;
CREATE TEMP TABLE salary
(
Employee varchar(100),
Salary1 numeric(38,12),
Salary2 numeric(38,12)
);
INSERT INTO salary (Employee, Salary1 ,Salary2)
VALUES ('A1',100,300),('A2',200,300),('A3',300,450),('A4',400,600);
我想将它平均划分(因为我们将 2 天的工资数据汇总到 1 列中)并将其转换为如下的每日级别数据:
因此,如果您看到员工 A2 - 第 3 天和第 4 天的薪水总和为 300(第 2 天的 150+150 table)。
任何 help/leads 表示赞赏。
物化 calendar table with the desired dates will facilitate generating the dates needed for the query. Without one, a tally table 或 CTE(如下例所示)是另一种方法。
DECLARE
@StartDate date = '2022-05-01'
, @DaysPerSalary int = 2;
WITH
t10 AS (SELECT n FROM (VALUES(0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) t(n))
,tally AS (SELECT ROW_NUMBER() OVER (ORDER BY (SELECT 0)) AS num FROM t10 AS a CROSS JOIN t10 AS b CROSS JOIN t10 AS c)
,salary1 AS (
SELECT Employee, Salary1 / @DaysPerSalary AS Salary
FROM SALARY
)
,salary2 AS (
SELECT Employee, Salary2 / @DaysPerSalary AS Salary
FROM SALARY
)
SELECT DATEADD(day, tally.num-1, @StartDate), Employee, Salary
FROM tally
CROSS JOIN salary1
WHERE tally.num <= @DaysPerSalary
UNION ALL
SELECT DATEADD(day, tally.num-1 + @DaysPerSalary, @StartDate), Employee, Salary
FROM tally
CROSS JOIN salary2
WHERE tally.num <= @DaysPerSalary
ORDER BY Employee, Salary;
我的数据是这样的:
重现:
DROP TABLE IF EXISTS SALARY;
CREATE TEMP TABLE salary
(
Employee varchar(100),
Salary1 numeric(38,12),
Salary2 numeric(38,12)
);
INSERT INTO salary (Employee, Salary1 ,Salary2)
VALUES ('A1',100,300),('A2',200,300),('A3',300,450),('A4',400,600);
我想将它平均划分(因为我们将 2 天的工资数据汇总到 1 列中)并将其转换为如下的每日级别数据:
因此,如果您看到员工 A2 - 第 3 天和第 4 天的薪水总和为 300(第 2 天的 150+150 table)。 任何 help/leads 表示赞赏。
物化 calendar table with the desired dates will facilitate generating the dates needed for the query. Without one, a tally table 或 CTE(如下例所示)是另一种方法。
DECLARE
@StartDate date = '2022-05-01'
, @DaysPerSalary int = 2;
WITH
t10 AS (SELECT n FROM (VALUES(0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) t(n))
,tally AS (SELECT ROW_NUMBER() OVER (ORDER BY (SELECT 0)) AS num FROM t10 AS a CROSS JOIN t10 AS b CROSS JOIN t10 AS c)
,salary1 AS (
SELECT Employee, Salary1 / @DaysPerSalary AS Salary
FROM SALARY
)
,salary2 AS (
SELECT Employee, Salary2 / @DaysPerSalary AS Salary
FROM SALARY
)
SELECT DATEADD(day, tally.num-1, @StartDate), Employee, Salary
FROM tally
CROSS JOIN salary1
WHERE tally.num <= @DaysPerSalary
UNION ALL
SELECT DATEADD(day, tally.num-1 + @DaysPerSalary, @StartDate), Employee, Salary
FROM tally
CROSS JOIN salary2
WHERE tally.num <= @DaysPerSalary
ORDER BY Employee, Salary;