使用 Pivot 删除重复结果添加新列

Using Pivot to remove duplicated results adding new columns

我有以下table

[_teste_calendario_Table]

当“Monstrengo”具有相同的值时,我想做一个枢轴,添加新列“De1”、“Ate1”、“Vencimento1”, “De2”、“Ate2”、“Vencimento2”以及来自重复行的“De”、“Ate”、“Vencimento”的值(当我说重复行时,我指的是具有相同“Monstrengo”值的行)。预期结果类似于:

[expected_output]

。我试图以两种不同(但相似)的方式实现这一结果。首先,我尝试在 [BBLA] <> 1 时选择 [BBLA] 并使用重复列的值创建新列。代码是这样的:

select *
,MAX(CASE WHEN BBLA = 2 THEN de END) AS De1
,MAX(CASE WHEN BBLA= 2 THEN Ate END) AS Ate1
,MAX(CASE WHEN BBLA= 2 THEN Vencimento END) AS Vencimento1
,MAX(CASE WHEN BBLA= 3 THEN de END) AS De2
,MAX(CASE WHEN BBLA= 3 THEN Ate END) AS Ate2
,MAX(CASE WHEN BBLA= 3 THEN Vencimento END) AS Vencimento2
from _TESTE_calendario_cliente_habilitado
GROUP BY pessoaid,BBLA,monstrengo,BBLA,DiasParaCliente,DiaInicioSemana
,De,Ate,Vencimento

我预计重复行的值将被转换为新列,旧行将被删除;问题是他们不是。我设法将值添加到新列中,但没有删除重复项,而是将值添加到它们,而不是原始列。结果是这样的:

[first_Test]

我考虑过使用 pivot,但就我所见,我需要一个单一的值来进行 pivot(例如,仅 [De]);因为我想用 De、Ate、Vencimento 来做,所以我不知道如何使用 apply pivot。 由于它不起作用,我创建了第二个 table,与第一个非常相似,但第二个 BBLA 变量 (BBLA_Verificador) 采用 BBLA 的 LEAD 值(行中的 BBLA 值在当前行的下方),这样我就可以知道下一行是否重复。然后我使用它们之前的相同函数来尝试获取正确行上的值。显然它没有用。我设法将值放在第一行(不是重复项),但值与第一行相同,而不是重复项的值。我也没有设法删除旧的。结果是这样的:[_teste_calendario_Table_2]

我应该怎么做?

编辑:为了更清楚,这是我的查询返回的示例:Monstrengo 中同一项目的 3 行,每行都有一个单独的块用于 De、Ate、Vencimento;De1、ate1、Vencimento1和 De2、Ate2、Vencimento2。 What I have 我希望的是:每个 Monstrengo 一行,如果 De、Ate、Vencimento 有第二组或第三组值,它们将输入单个 Monstrengo 行。 What I wish

我设法找到了解决办法;我已经尝试过类似的东西,但我可能在语法上犯了错误。 我更改了代码

select *
,MAX(CASE WHEN BBLA = 2 THEN de END) AS De1
,MAX(CASE WHEN BBLA= 2 THEN Ate END) AS Ate1
,MAX(CASE WHEN BBLA= 2 THEN Vencimento END) AS Vencimento1
,MAX(CASE WHEN BBLA= 3 THEN de END) AS De2
,MAX(CASE WHEN BBLA= 3 THEN Ate END) AS Ate2
,MAX(CASE WHEN BBLA= 3 THEN Vencimento END) AS Vencimento2
from _TESTE_calendario_cliente_habilitado
GROUP BY pessoaid,BBLA,monstrengo,BBLA,DiasParaCliente,DiaInicioSemana
,De,Ate,Vencimento

SELECT *
,CASE WHEN LEAD(BBLA,1) OVER (PARTITION BY MONSTRENGO ORDER BY BBLA) = 2 THEN LEAD(de,1) OVER
(PARTITION BY MONSTRENGO ORDER BY BBLA) END AS De1
,CASE WHEN LEAD(BBLA,1) OVER (PARTITION BY MONSTRENGO ORDER BY BBLA)= 2 THEN LEAD(Ate,1) OVER
(PARTITION BY MONSTRENGO ORDER BY BBLA) END AS Ate1
,CASE WHEN LEAD(BBLA,1) OVER (PARTITION BY MONSTRENGO ORDER BY BBLA)= 2 THEN LEAD(Vencimento,1) OVER
(PARTITION BY MONSTRENGO ORDER BY BBLA) END AS Vencimento1
--
,CASE WHEN LEAD(BBLA,2) OVER (PARTITION BY MONSTRENGO ORDER BY BBLA)= 3 THEN LEAD(de,2) OVER
(PARTITION BY MONSTRENGO ORDER BY BBLA) END AS De2
,CASE WHEN LEAD(BBLA,2) OVER (PARTITION BY MONSTRENGO ORDER BY BBLA)= 3 THEN LEAD(Ate,2) OVER
(PARTITION BY MONSTRENGO ORDER BY BBLA) END AS Ate2
,CASE WHEN LEAD(BBLA,2) OVER (PARTITION BY MONSTRENGO ORDER BY BBLA)= 3 THEN LEAD(Vencimento,2) OVER
(PARTITION BY MONSTRENGO ORDER BY BBLA) END AS Vencimento2
FROM _TESTE_calendario_cliente_habilitado
GROUP BY _TESTE_calendario_cliente_habilitado.pessoaid, _TESTE_calendario_cliente_habilitado.Monstrengo
,_TESTE_calendario_cliente_habilitado.BBLA,_TESTE_calendario_cliente_habilitado.DiasParaCliente,
_TESTE_calendario_cliente_habilitado.DiaInicioSemana,_TESTE_calendario_cliente_habilitado.De
,_TESTE_calendario_cliente_habilitado.Ate,_TESTE_calendario_cliente_habilitado.Vencimento

通过这种方式,我设法让所有内容都在同一行;我用这些信息创建了一个 table,然后创建了一个视图,通过 BBLA = 1 过滤,给我所有 non-duplicate 行。