Me gustaría aprender un poco la función de PIVOT en SAP. Actualemnte tengo este QUERY que me gustaría qeu me acomode la información hacia los lados y no hacia abajo.
Será que me pueden ayudar para validar el proceso.
SELECT T1."ItemCode" AS "Código", T1."ItemName" AS "Descripción", T1."U_LD_Precio1",
T1."OnHand" - T1."IsCommited" AS "STOCK TOTAL", T1."MinLevel", T1."MinLevel" * 0.2 AS "20% DEL MINIMO", CONCAT('\\10.10.10.28\sap\VIAS DE ACCESO\Imagenes\' , T1."PicturName"), T1."OnHand", T2."Quantity", T3."DocNum"
FROM OITB T0 INNER JOIN OITM T1 ON T0."ItmsGrpCod" = T1."ItmsGrpCod" INNER JOIN POR1 T2 ON T1."ItemCode" = T2."ItemCode" INNER JOIN OPOR T3 ON T2."DocEntry" = T3."DocEntry"
WHERE T1."ItemType" in('I') and T1."U_LD_Abc2" in('+A' , 'A' , 'B' , 'C' , 'N', 'X', 'Z') and T1."OnHand" - T1."IsCommited" < T1."MinLevel" * 0.20
Hola @LuiskSiles
Lo siguiente te puede servir de referencia
h_tps://blogs.sap.com/2019/10/28/pivoting-data-with-sap-hana/
h_tps://blogs.sap.com/2014/01/02/table-transpose-in-sap-hana-modeling
h_tps://blogs.sap.com/2019/04/21/how-to-pivotunpivot-in-sap-hana/
Primero hay un parámetro que limita el máximo de columnas en el resultado de una consulta y también la cantidad de joins.
Segundo, hay que hacer mucho código dinámico.
Tercero, hay cosas útiles pero están para HANA 2.0 y la mayoría de los clientes tienen 1.0.
Aquí un SP que hice para pivotar dinamicamente, espero te sirva de idea.
CREATE procedure calidad_pivot (
in Fruta varchar(10),
in Tipo varchar(2),
in filter varchar (5000) default ''
)
as
indexBtch int;
indexAttr int;
maxAttr int;
maxBtch int;
sSql nclob;
subSql nclob;
field varchar(128);
db varchar(128);
begin
sSql = '
select
(select max("DocEntry") from "{db}"."@DFO_RQLTY4" where "U_BatchNum"=A."Lote") as "Nro.Reg.Calidad",
A.*,
B.*,
{subSql}
from "{db}"."FRU_MAESTRO_LOTE" A
inner join "{db}"."FRU_STOCK_LOTE" B on A."AbsEntry"=B."AbsEntry"
where A."Fruta"=''{Fruta}'' and A."Estado"=''{Tipo}'' {filter}
';
subSql = '(select max("DocEntry") from "{db}"."@DFO_RQLTY4" where "U_BatchNum"=A."Lote") as "Nro.Reg.Calidad", ';
subSql = subSql || '(select max("U_Value") from "{db}"."@DFO_RQLTY4" where "U_BatchNum"=A."Lote" and "U_Attr"=''{field}'') as "{field}"';
select current_schema into db from dummy;
create local temporary table #Attrs (Linea Int, Attr varchar(255));
insert into #Attrs
select row_number() over(order by "Attr" ), "Attr" from (select distinct "U_Attr" "Attr" from "@DFO_RQLTY4" where "U_BatchNum" in (select distinct "Lote" from "FRU_MAESTRO_LOTE" where "Fruta"=Fruta and "Estado"=Tipo) order by "U_Attr");
select max(Linea) into maxAttr from #Attrs;
for indexAttr in 1 .. maxAttr do
select Attr into field from #Attrs where Linea=indexAttr;
if (indexAttr<=maxAttr) then
SubSql = SubSql || ',';
SubSql = SubSql || '(select max("U_Value") from "{db}"."@DFO_RQLTY4" where "U_BatchNum"=A."Lote" and "U_Attr"=''{field}'') as "{field}"';
else
SubSql = SubSql || '(select max("U_Value") from "{db}"."@DFO_RQLTY4" where "U_BatchNum"=A."Lote" and "U_Attr"=''{field}'') as "{field}"';
end if;
SubSql = replace(subSql,'{field}',field);
end for;
sSql = replace(sSql,'{subSql}',subSql);
sSql = replace(sSql,'{db}',db);
sSql = replace(sSql,'{Fruta}',Fruta);
sSql = replace(sSql,'{Tipo}',Tipo);
sSql = replace(sSql,'{filter}',filter);
drop table #Attrs;
execute immediate sSql;
--select ssql from dummy;
end;