如何将语法结果转换为变量 mysql
how to transform results of syntax to become variable mysql
我有一个table,交易id为order_buyer_id,买家id为createdby,交易发生日期为createdAt,每笔交易的权重为quantity。
在我的 table 上,我将买家分为 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
重点是如何使语法的每个结果都变成可变的,这样我就可以像预期的结果一样计算 percentage_1 和 percentage_2。
要测试更大的查询,您必须提供一些数据,以正确测试查询see
我在您的描述中找不到您需要 result_c 的原因,但您现在可以使用它了。
顺便说一句,这是算法或查询,而不是 syntax.。
SELECT
result_a / result_b * 100 AS percentage_1,
100 - (result_a / result_b * 100) AS percentage_2
FROM
(SELECT
(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)) result_a,
(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) result_b,
(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))) result_c
) a
我有一个table,交易id为order_buyer_id,买家id为createdby,交易发生日期为createdAt,每笔交易的权重为quantity。
在我的 table 上,我将买家分为 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
重点是如何使语法的每个结果都变成可变的,这样我就可以像预期的结果一样计算 percentage_1 和 percentage_2。
要测试更大的查询,您必须提供一些数据,以正确测试查询see
我在您的描述中找不到您需要 result_c 的原因,但您现在可以使用它了。
顺便说一句,这是算法或查询,而不是 syntax.。
SELECT
result_a / result_b * 100 AS percentage_1,
100 - (result_a / result_b * 100) AS percentage_2
FROM
(SELECT
(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)) result_a,
(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) result_b,
(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))) result_c
) a