gpt4 book ai didi

sql - 将数据附加到拆分行

转载 作者:行者123 更新时间:2023-12-04 21:46:36 25 4
gpt4 key购买 nike

我想知道历史上有多少人在几个月内没空,因为我有一个 historicTable,其中包含从 2012 年到 2018 年的数据,每行包含员工有多少时间没空可用(假期、疾病等)这是一个例子:

idUser startDate    endDate     daysUn    reason    nameEmp
--------------------------------------------------------
123 25/01/2018 09/02/2018 12 Sickness John Doe

这是我需要的每一行

idUser startDate    endDate     daysUn    reason    nameEmp
--------------------------------------------------------
123 25/01/2018 31/01/2018 5 Sickness John Doe
123 01/01/2018 09/02/2018 7 Sickness John Doe

我知道这个问题在这里被问了数百次,但是我在对整个表执行此操作时遇到了麻烦,因为我在不同的答案中尝试过的所有过程都针对特定的给定开始日期和结束日期列,以及我需要的是将所有数据附加到此表并按原样保存,以便分析师能够研究特定案例和特定员工。这是我当前代码的结果:

original_INI            original_FIN            new_INI                 new_FIN
----------------------- ----------------------- ----------------------- -----------------------
2017-10-15 00:00:00.000 2018-01-06 00:00:00.000 2017-10-15 00:00:00.000 2017-10-31 00:00:00.000
2017-10-15 00:00:00.000 2018-01-06 00:00:00.000 2017-11-01 00:00:00.000 2017-11-30 00:00:00.000
2017-10-15 00:00:00.000 2018-01-06 00:00:00.000 2017-12-01 00:00:00.000 2017-12-31 00:00:00.000
2017-10-15 00:00:00.000 2018-01-06 00:00:00.000 2018-01-01 00:00:00.000 2018-01-06 00:00:00.000

这是代码,原始日期没问题,因为我可以更全面地对数据进行排序,但它可以打印并保存其余数据,因此更具可读性:

;WITH n(n) AS 
(
SELECT ROW_NUMBER() OVER (ORDER BY [object_id])-1 FROM sys.all_columns
),
d(n,f,t,md,bp,ep) AS
(
SELECT n.n, d.INI, d.FIN,
DATEDIFF(MONTH, d.INI, d.FIN),
DATEADD(MONTH, n.n, DATEADD(DAY, 1-DAY(INI), INI)),
DATEADD(DAY, -1, DATEADD(MONTH, 1, DATEADD(MONTH, n.n,
DATEADD(DAY, 1-DAY(INI), INI))))
FROM n INNER JOIN archivoFuente AS d
ON d.FIN >= DATEADD(MONTH, n.n-1, d.INI)
)
SELECT original_INI = f, original_FIN = t,
new_INI = CASE n WHEN 0 THEN f ELSE bp END,
new_FIN = CASE n WHEN md THEN t ELSE ep END
FROM d WHERE md >= n
ORDER BY original_INI, new_INI;

感谢您对查询的任何帮助。

最佳答案

其实很简单,我使用了相同的代码来满足我的要求,你需要调用每个select语句中的每一列,这样当你拆分行时它就存在,检查这段代码:

;WITH n(n) AS 
(
SELECT ROW_NUMBER() OVER (ORDER BY [object_id])-1 FROM sys.all_columns
),
d(n,f,t,md,bp,ep,
--CALL YOUR COLUMNS HERE EG: name, id, bla, ble
) AS
(
SELECT n.n,d.INI, d.FIN,
DATEDIFF(MONTH, d.INI, d.FIN),
DATEADD(MONTH, n.n, DATEADD(DAY, 1-DAY(INI), INI)),
DATEADD(DAY, -1, DATEADD(MONTH, 1, DATEADD(MONTH, n.n,
DATEADD(DAY, 1-DAY(INI), INI)))),
--CALL YOUR COLUMNS HERE AGAIN, PAY ATTENTION TO NAMES AND COMMAS
d.id_hr,d.Tipo,d.ID_tip,d.Nom_inc,d.RUT,d.Nombre,d.ID_emp,d.Nom_pos,d.Dias_durac,d.Num_lic,d.ID_usu_ap,d.ult_act
FROM n INNER JOIN archivoFuente AS d
ON d.FIN >= DATEADD(MONTH, n.n-1, d.INI)
)
SELECT --PUT ONCE AGAIN YOUR COLUMNS HERE, THIS WILL WORK FOR THE DISPLAYED RESULT
original_INI = f, original_FIN = t,
new_INI = CASE n WHEN 0 THEN f ELSE bp END,
new_FIN = CASE n WHEN md THEN t ELSE ep END
FROM d
WHERE md >= n
ORDER BY original_INI, new_INI;

现在,要保存表格,我建议对新表格使用 INSERT 语句,你会怎么做,我不知道,我在同一个地方你。希望有人检查这个问题。

关于sql - 将数据附加到拆分行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54516857/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com