-- Запрос на получение отношений продавцов с клиентами + сколько каждый клиент заплатил
WITH
cte1 AS (
SELECT t3.amount_value,
t2.id prodavec,
t1.r_w_uid klient
FROM arur t1
JOIN u_af t2 ON t1.af_id = t2.id
JOIN orders t3 ON t1.r_w_uid = t3.uid
)
SELECT prodavec, klient, amount_value
FROM cte1
WHERE prodavec = 52
ORDER BY prodavec;
-- Запрос на сумму и проценты для u_af.id = 52
WITH
cte1 AS (
SELECT t3.amount_value,
t2.id prodavec,
t1.r_w_uid klient
FROM arur t1
JOIN u_af t2 ON t1.af_id = t2.id
JOIN orders t3 ON t1.r_w_uid = t3.uid
)
SELECT prodavec, SUM(amount_value) AS summa, SUM(amount_value * 0.1) AS 10_procentov
FROM cte1
WHERE prodavec = 52
ORDER BY prodavec;