[英]how to add two summed values in mysql
Is it possible to add two summed values in mysql like the query shown below (figure 1)?是否可以像下面显示的查询(图 1)那样在 mysql 中添加两个求和值?
I have the query below, and I am trying to add qty and pro.我有下面的查询,我正在尝试添加 qty 和 pro。
Figure 1图1
SELECT
SUM(CASE WHEN value > 0 AND xp = 0 THEN 1 ELSE 0 END) AS qty,
SUM(CASE WHEN value > 1 AND xp = 2 THEN 1 ELSE 0 END) AS pro
SUM(pro + qty)
FROM OrderDetails;
I will need to use qty and pro many times throughout the query, so I don't want to write the query as shown in figure 2我将需要在整个查询过程中多次使用 qty 和 pro,所以我不想编写如图 2所示的查询
figure 2图2
SELECT
SUM(
SUM(CASE WHEN value > 0 AND xp = 0 THEN 1 ELSE 0 END)
+
SUM(CASE WHEN value > 1 AND xp = 2 THEN 1 ELSE 0 END)
)
FROM OrderDetails;
EDITED已编辑
What I mean by using qty and pro multiple times throughout the query is something like the following.我在整个查询中多次使用qty和pro 的意思如下所示。
Query Example one - is what I am trying to do, where I can reuse qty and pro multiple times throughout the query, in places like sum(pro + qty)
and having (qty > 0 AND pro > 0)
, without having to rewrite the whole sum statement.查询示例一- 是我想要做的,我可以在整个查询中多次重复使用 qty 和 pro,在
sum(pro + qty)
和having (qty > 0 AND pro > 0)
,而无需重写整个 sum 语句。
SELECT
SUM(CASE WHEN value > 0 AND xp = 0 THEN 1 ELSE 0 END) AS qty,
SUM(CASE WHEN value > 1 AND xp = 2 THEN 1 ELSE 0 END) AS pro
SUM((pro + qty)/100)
FROM OrderDetails
GROUP BY value
HAVING (
qty > 2
AND
pro > 2
)
;
query example 2 - is what I have to currently do, and it seems like very poor practice to have to rewrite SUM(CASE WHEN value > 0 AND xp = 0 THEN 1 ELSE 0 END)
multiple times, rather than a simple qty or pro like in the query above查询示例 2 - 是我目前必须做的,并且必须多次重写
SUM(CASE WHEN value > 0 AND xp = 0 THEN 1 ELSE 0 END)
而不是简单的数量或专业似乎是非常糟糕的做法就像上面的查询一样
SELECT
SUM(CASE WHEN value > 0 AND xp = 0 THEN 1 ELSE 0 END) AS qty,
SUM(CASE WHEN value > 1 AND xp = 2 THEN 1 ELSE 0 END) AS pro
SUM((
SUM(CASE WHEN value > 0 AND xp = 0 THEN 1 ELSE 0 END)
+
SUM(CASE WHEN value > 1 AND xp = 2 THEN 1 ELSE 0 END)
)/100)
FROM OrderDetails
GROUP BY value
HAVING (
SUM(CASE WHEN value > 0 AND xp = 0 THEN 1 ELSE 0 END) > 2
AND
SUM(CASE WHEN value > 1 AND xp = 2 THEN 1 ELSE 0 END) > 2
)
;
is there no way to add SUM(pro + qty)
after defining it once in examples like SUM(CASE WHEN value > 0 AND xp = 0 THEN 1 ELSE 0 END) AS qty
在
SUM(CASE WHEN value > 0 AND xp = 0 THEN 1 ELSE 0 END) AS qty
等示例中定义一次后,是否无法添加SUM(pro + qty)
You can compute using the below query.你可以计算使用下面的查询。
SELECT
SUM(CASE WHEN value > 0 AND xp = 0 THEN 1 ELSE 0 END) AS qty,
SUM(CASE WHEN value > 1 AND xp = 2 THEN 1 ELSE 0 END) AS pro,
SUM(CASE
WHEN value > 0 AND xp = 0 THEN 1
WHEN value > 1 AND xp = 2 THEN 1
ELSE 0 END) AS qtyProSum
FROM OrderDetails;
Create View for the below query and use it any of times对于下面的查询创建视图和任何使用它的时候
SELECT
SUM(CASE WHEN value > 0 AND xp = 0 THEN 1 ELSE 0 END) AS qty,
SUM(CASE WHEN value > 1 AND xp = 2 THEN 1 ELSE 0 END) AS pro
FROM OrderDetails;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.