Consulta con Query PL-SQL
Tengo un query al que quiero añadir unos cuantos datos pero la estructura no la tengo familiarizada (he sido analista de base de datos en Transct sql pero hace tiempo y no he trabajado con estas estructuras, quería que alguien me pueda explicar a groso modo que tratan de hacer con esto y como podría añadir un campo adicional, les paso el query.
Quiero entenderla un poquito y además agregarle un campo de la tabla xxfc_store, el cual es sotre_name, ya lo revise en la tabla pero cuando lo trato de agregar me marca error, creo que no estoy entendiendo bien el query. Gracias!
WITH tgt AS
( SELECT to_date( &&NUM_SEMANA *7, 'DDD') AS dt FROM dual
) ,
cat AS
(SELECT XS.store AS Tienda,
CR_Tienda,
AR.AREA_NAME AS PLAZA,
CR_plaza,
&&NUM_SEMANA AS Semana,
( SELECT extract(MONTH FROM dt) FROM tgt
) AS Mes,
im. Item AS EAN,
im.item_desc AS descripcion,
il.status AS Status_del_articulo,
u.uda_value_desc AS Marca,
sup_name AS Proveedor,
div_name AS Supergrupo,
group_name AS Categoria,
dept_name AS Subcategoria,
class_name AS Segmento,
sub_name AS Subsegmento,
unit_cost AS Costo_MAct,
unit_Retail AS Precio_MAct
FROM xxfc_store xs,
item_master im ,
item_loc il,
division d,
groups g,
deps de,
class c,
subclass s,
item_loc_soh ils,
sups su,
(SELECT item,
uda_value_desc
FROM uda_item_lov NATURAL
JOIN uda_values
WHERE uda_id IN (200,201,202,203,204,205,206,207)
) u,
STORE_HIERARCHY SH,
AREA AR
WHERE xs.store = il.loc
AND xs.store = ils.loc
AND XS.STORE = SH.STORE
AND SH.AREA = AR.AREA
AND im.item = il.item
AND im.item = ils.item
AND im.item = u.item (+)
AND im.dept = de.dept
AND im.class = c.class
AND im.subclass = s.subclass
AND d.DIVISION = g.DIVISION
AND g.group_no = de.group_no
AND de.DEPT = c.DEPT
AND de.DEPT = s.DEPT
AND c.CLASS = s.CLASS
AND su.supplier = ils.primary_supp
UNION
SELECT XS.store AS Tienda,
CR_Tienda,
AR.AREA_NAME AS PLAZA,
CR_plaza,
&&NUM_SEMANA AS Semana,
(SELECT extract(MONTH FROM dt) FROM tgt
) AS Mes,
im. Item AS EAN,
im. Item_desc AS descripcion,
il.status AS Status_del_articulo,
NULL AS Marca,
NULL AS Proveedor,
div_name AS Supergrupo,
group_name AS Categoria,
dept_name AS Subcategoria,
class_name AS Segmento,
sub_name AS Subsegmento,
unit_cost AS Costo_MAct,
unit_Retail AS Precio_MAct
FROM xxfc_store xs,
item_master im,
item_loc il,
division d,
groups g,
deps de,
class c,
subclass s,
item_loc_soh ils,
STORE_HIERARCHY SH,
AREA AR
WHERE xs.store = il.loc
AND xs.store = ils.loc
AND XS.STORE = SH.STORE
AND SH.AREA = AR.AREA
AND im.item = il.item
AND im.item = ils.item
AND im.dept = de.dept
AND im.class = c.class
AND im.subclass = s.subclass
AND d.DIVISION = g.DIVISION
AND g.group_no = de.group_no
AND de.DEPT = c.DEPT
AND de.DEPT = s.DEPT
AND c.CLASS = s.CLASS
AND ils.primary_supp IS NULL
),
vta AS
(SELECT item,
store,
SUM(UNITS) AS SALES_UNITS,
SUM(total_retail) AS sales_issues
FROM fem_fif_stg
WHERE tran_code = 1
AND tran_date BETWEEN
( SELECT next_day(dt -8, 'LUN') FROM tgt
)
AND( SELECT next_day(dt-1 , 'DOM') FROM tgt)
GROUP BY item,
store
),
comp AS
(SELECT item,
store,
ROUND(SUM(total_cost/(1 + vat_Rate_cost)),4) AS Compra_S,
SUM (units) AS Compra_Und
FROM fem_fif_stg
WHERE tran_code = 20
AND tran_date BETWEEN
( SELECT next_day(dt -8, 'LUN') FROM tgt
)
AND( SELECT next_day(dt-1 , 'DOM') FROM tgt)
GROUP BY item,
store
),
merm AS
(SELECT item,
store,
ROUND(SUM(total_cost/(1 + vat_Rate_cost)),4) AS Merma_S,
SUM (units) AS Merma_Und
FROM fem_fif_stg
WHERE tran_code = 22
AND tran_date BETWEEN
( SELECT next_day(dt -8, 'LUN') FROM tgt
)
AND( SELECT next_day(dt-1 , 'DOM') FROM tgt)
GROUP BY item,
store
)
SELECT TIENDA,
CR_TIENDA,
PLAZA,
CR_PLAZA,
Semana,
MES,
EAN,
Descripción,
STATUS_DEL_ARTICULO,
Marca,
Proveedor,
Supergrupo,
Categoría,
Subcategoría,
Segmento,
subsegmento,
SALES_UNITS AS UNIDADES_VENDIDAS,
SALES_ISSUES AS MONTO_DE_VENTA_S,
COSTO_MACT,
PRECIO_MACT,
merma_und AS MERMA_EN_UNIDADES,
merma_s,
COMPRA_S AS COSTO_DE_COMPRA,
COMPRA_UND AS UNIDADES_COMPRADAS
FROM cat
LEFT JOIN vta
ON cat.ean = vta.item
AND cat.tienda = vta.store
LEFT JOIN comp
ON cat.ean = comp.item
AND cat.tienda = comp.store
LEFT JOIN merm
ON cat.ean = merm.item
AND cat.tienda = merm.store;