У меня есть таблица с order_buyer_id в качестве идентификатора транзакции, созданная в качестве идентификатора покупателя, созданная в качестве даты совершения транзакции, а количество в качестве веса каждой транзакции.
в моей таблице i классифицировал покупателя как 3 типа:
- new buyer
- unique buyer
- existing buyer
это синтаксис, чтобы найти нового покупателя, которого я назвал A (новый покупатель):
select
count(distinct om.createdby) as count_buyer
from (select count(xx.count_) as count_
from (select count(createdby) as count_ from order_match
where order_status_Id in (4, 5, 6, 8)
group by createdby
having count(createdby) = 1) xx
) x1,
(select createdby
from order_match
group by createdby
having count(createdby) = 1) yy,
order_match om
where yy.createdby = om.createdby and
order_status_id in (4, 5, 6, 8)
and om.createdAt >= paramdatefrom
and om.createdAt <= paramdateto
and NOT EXISTS (select 1 from order_match om2
where om.createdby = om2.createdby
and order_status_id in (4, 5, 6, 8)
and om2.createdAt < paramdatefrom);
это синтаксис, чтобы узнать повторный покупатель, называемый B (уникальный покупатель):
select
count(distinct om.createdby) as count
from (select count(xx.count_) as count_
from (select count(createdby) as count_ from order_match
where order_status_Id in (4, 5, 6, 8)
group by createdby
) xx
) x1,
(select createdby
from order_match
group by createdby
) yy,
order_match om
where yy.createdby = om.createdby and
order_status_id in (4, 5, 6, 8)
and om.createdAt >= paramdatefrom
and om.createdAt <= paramdateto;
;
, и это синтаксис для определения существующего покупателя, называемый C (существующий покупатель):
select
count(distinct om.createdby) as count
from
order_match om
where om.order_status_id in (4,5,6,8)
and om.createdAt <= paramdateto
and om.createdAt >= paramdatefrom
and EXISTS (select 1 from order_match om2
where om.createdby = om2.createdby
and om2.createdAt < paramdatefrom and
om2.order_status_id in (4, 5, 6, 8))
;
в основном я хочу, чтобы весь этот синтаксис стал переменным A, B, C, чтобы я мог рассчитывать процентную долю для моих потребностей, основываясь на моем объяснении, ожидаемых результатов, как это
select (A (the result of syntax new Buyer) : B (the result of syntax unique buyer)) * 100 as percentage_1
и select (100 - percentage_1) as percentage_2
Дело в том, как сделать так, чтобы каждый результат синтаксиса стал переменным, чтобы я мог считать проценты_1 и процент_2 точно так же, как ожидаемые результаты.