Ð
Size: a a a
Ð
2_
TS
2_
2_
Д
TS
Ð
Д
TS
SELECT
jsonb_build_object(
'total',
MAX(sum) FILTER ( WHERE grouping = 15 ),
'days',
MAX(dayCnt) FILTER ( WHERE grouping = 15 ),
'items',
MAX(itemCnt) FILTER ( WHERE grouping = 15 ),
'sales',
MAX(saleCnt) FILTER ( WHERE grouping = 15 ),
'products',
MAX(productCnt) FILTER ( WHERE grouping = 15 ),
'buyers',
MAX(buyerCnt) FILTER ( WHERE grouping = 15 ),
'catalogs',
MAX(catalogCnt) FILTER ( WHERE grouping = 15 )
) as summary,
jsonb_agg(
jsonb_build_object(
'date', date,
'total', sum,
'itemCnt', itemCnt,
'saleCnt', saleCnt,
'productCnt', productCnt,
'products', products,
'buyerCnt', buyerCnt,
'buyers', buyers,
'catalogCnt', catalogCnt,
'catalogs', catalogs
) ORDER BY date) FILTER ( WHERE grouping = 7) AS days,
jsonb_agg(
jsonb_build_object(
'catalog', catalog,
'total', sum,
'itemCnt', itemCnt,
'saleCnt', saleCnt,
'productCnt', productCnt,
'products', products,
'buyerCnt', buyerCnt,
'buyers', buyers
) ORDER BY sum DESC, catalog_id) FILTER ( WHERE grouping = 11) AS catalogs,
jsonb_agg(
jsonb_build_object(
'product', product,
'total', sum,
'itemCnt', "itemCnt",
'saleCnt', saleCnt,
'buyerCnt', buyerCnt,
'buyers', buyers,
'catalogCnt', catalogCnt,
'catalogs', catalogs
) ORDER BY sum DESC, product_id) FILTER ( WHERE grouping = 13) AS products,
jsonb_agg(
jsonb_build_object(
'buyer', buyer,
'total', sum,
'itemCnt', itemCnt,
'saleCnt', saleCnt,
'productCnt', productCnt,
'products', products,
'catalogCnt', catalogCnt,
'catalogs', catalogs
) ORDER BY sum DESC, buyer_id) FILTER ( WHERE grouping = 14) AS buyers
Ð
Д
TS
TS
FROM (
SELECT "cc"."catalog_id" AS "catalog_id",
"c"."id" AS "buyer_id",
"p"."id" AS "product_id",
date_trunc('day', s.created) AS "date",
CASE WHEN p.id IS NOT NULL THEN
jsonb_build_object('id', p.id, 'type_id', p.type_id, 'options',p.options)
END AS product,
CASE WHEN c.id IS NOT NULL THEN
jsonb_build_object('id', c.id, 'options',c.options)
END AS buyer,
CASE WHEN cc.catalog_id IS NOT NULL THEN
jsonb_build_object('id', cc.catalog_id, 'name', cat.name, 'options', cat.options)
END AS catalog,
COALESCE(
jsonb_agg(DISTINCT p.id), '[]') AS "products",
COALESCE(
jsonb_agg(DISTINCT c.id), '[]') AS "buyers",
COALESCE(
jsonb_agg(DISTINCT cc.catalog_id), '[]') AS "catalogs",
COUNT(DISTINCT si) AS "itemCnt",
count(DISTINCT s.id) AS saleCnt,
count(DISTINCT p.id) AS "productCnt",
count(DISTINCT c.id) AS "buyerCnt",
count(DISTINCT cc.catalog_id) AS "catalogCnt",
count(DISTINCT
date_trunc('day', s.created)) AS "dayCnt",
common.
sum_distinct(
CAST((si.price #>> '{value}') AS numeric) *
CAST((si.quantity #>> '{value}') AS numeric) *
CASE WHEN
CAST(si.options #>> '{deliveries}' AS numeric) > 0
THEN
CAST(si.options #>> '{deliveries}' AS numeric)
ELSE 1
END,
si.id) sum,
grouping(
date_trunc('day', s.created), catalog_id, p.id, c.id) AS "grouping"
FROM userspace_10."sale_item" "si"
LEFT JOIN userspace_10."sale" "s" ON s.id=si.sale_id AND s.status_id >= 32 AND s.status_id < 255
LEFT JOIN userspace_10."client" "c" ON c.id=s.buyer_id
LEFT JOIN userspace_10."product" "p" ON p.id=si.product_id
LEFT JOIN userspace_10."product_category_mapping" "pcm" ON pcm.product_id=p.id AND pcm.disabled=false
LEFT JOIN userspace_10."catalog_category" "cc" ON cc.id=pcm.category_id
LEFT JOIN userspace_10."catalog" "cat" ON cat.id=cc.catalog_id
WHERE ("s"."id" IS NOT NULL) AND ("s"."deleted"=FALSE)
AND ("si"."deleted"=FALSE) AND ("si"."created" BETWEEN '2020-03-01 00:00:00.00000' AND '2020-03-31 23:59:59.999999')
GROUP BY GROUPING SETS (
(),
(date),
("c"."id", "c"."options"),
("p"."id", "p"."type_id", "p"."options"),
(catalog_id, "cat"."name", "cat"."options")
)
) "t";
Ð
Д
TS
Ð
Д
Ð