根据条件计数mysql 5.7
count based on conditions mysql 5.7
我使用了 mysql 5.7 版,我有一个 table 生产每个产品,数量,并使用许多这样的探险
+---------+-----------------+------+--------+---------+
| Product | Type_Expedition | Pack | Amount | Weight |
+---------+-----------------+------+--------+---------+
| Chicken | A | 1 | 2 | 2 |
| Beef | A | 1 | 2 | 2 |
| Lamb | B | 1 | 2 | 2 |
| Beef | B | 2 | 2 | 4 |
| Chicken | A | 3 | 2 | 6 |
| Lamb | A | 1 | 1 | 1 |
| Lamb | A | 1 | 1 | 1 |
+---------+-----------------+------+--------+---------+
如何计算 type_expedition B 和非 B(除 B 以外的所有类型的探险)的重量和数量之和?
我假设使用这种语法(抱歉我想使用 dbfiddle.uk 但这是错误的)
select product, type_expedition, pack, amount, weight, (sum(amount) where type_expedition = B), (sum(weight) where type_expedition = B) from my_table
预期结果
+---------------------------------------------------+---+----+
| Total amount and weight for type_expedition B | 4 | 6 |
+---------------------------------------------------+---+----+
| Total amount and weight for type_expedition NON B | 8 | 12 |
+---------------------------------------------------+---+----+
您可以对最后 2 行使用 UNION ALL:
select t.Product, t.Type_Expedition, t.Pack, t.Amount, t.Weight
from (
select *, 0 sort from my_table
union all
select 'Total Amount and Weight for expedition B', null, null,
sum(amount),
sum(weight), 1
from my_table
where Type_Expedition = 'B'
union all
select 'Total Amount and Weight for expedition not B', null, null,
sum(amount),
sum(weight), 2
from my_table
where Type_Expedition <> 'B'
) t
order by t.sort
参见demo。
结果:
| Product | Type_Expedition | Pack | Amount | Weight |
| -------------------------------------------- | --------------- | ---- | ------ | ------ |
| Beef | A | 1 | 2 | 2 |
| Chicken | A | 3 | 2 | 6 |
| Lamb | B | 1 | 2 | 2 |
| Lamb | A | 1 | 1 | 1 |
| Chicken | A | 1 | 2 | 2 |
| Beef | B | 2 | 2 | 4 |
| Lamb | A | 1 | 1 | 1 |
| Total Amount and Weight for expedition B | | | 4 | 6 |
| Total Amount and Weight for expedition not B | | | 8 | 12 |
如果您只需要最后 2 行的总计:
select
case Type_Expedition
when 'B' then 'Total Amount and Weight for expedition B'
else 'Total Amount and Weight for expedition not B'
end type,
sum(amount),
sum(weight)
from my_table
group by type
参见demo。
结果:
| Total Amount and Weight for expedition B | 4 | 6 |
| Total Amount and Weight for expedition not B | 8 | 12 |
我使用了 mysql 5.7 版,我有一个 table 生产每个产品,数量,并使用许多这样的探险
+---------+-----------------+------+--------+---------+
| Product | Type_Expedition | Pack | Amount | Weight |
+---------+-----------------+------+--------+---------+
| Chicken | A | 1 | 2 | 2 |
| Beef | A | 1 | 2 | 2 |
| Lamb | B | 1 | 2 | 2 |
| Beef | B | 2 | 2 | 4 |
| Chicken | A | 3 | 2 | 6 |
| Lamb | A | 1 | 1 | 1 |
| Lamb | A | 1 | 1 | 1 |
+---------+-----------------+------+--------+---------+
如何计算 type_expedition B 和非 B(除 B 以外的所有类型的探险)的重量和数量之和?
我假设使用这种语法(抱歉我想使用 dbfiddle.uk 但这是错误的)
select product, type_expedition, pack, amount, weight, (sum(amount) where type_expedition = B), (sum(weight) where type_expedition = B) from my_table
预期结果
+---------------------------------------------------+---+----+
| Total amount and weight for type_expedition B | 4 | 6 |
+---------------------------------------------------+---+----+
| Total amount and weight for type_expedition NON B | 8 | 12 |
+---------------------------------------------------+---+----+
您可以对最后 2 行使用 UNION ALL:
select t.Product, t.Type_Expedition, t.Pack, t.Amount, t.Weight
from (
select *, 0 sort from my_table
union all
select 'Total Amount and Weight for expedition B', null, null,
sum(amount),
sum(weight), 1
from my_table
where Type_Expedition = 'B'
union all
select 'Total Amount and Weight for expedition not B', null, null,
sum(amount),
sum(weight), 2
from my_table
where Type_Expedition <> 'B'
) t
order by t.sort
参见demo。
结果:
| Product | Type_Expedition | Pack | Amount | Weight |
| -------------------------------------------- | --------------- | ---- | ------ | ------ |
| Beef | A | 1 | 2 | 2 |
| Chicken | A | 3 | 2 | 6 |
| Lamb | B | 1 | 2 | 2 |
| Lamb | A | 1 | 1 | 1 |
| Chicken | A | 1 | 2 | 2 |
| Beef | B | 2 | 2 | 4 |
| Lamb | A | 1 | 1 | 1 |
| Total Amount and Weight for expedition B | | | 4 | 6 |
| Total Amount and Weight for expedition not B | | | 8 | 12 |
如果您只需要最后 2 行的总计:
select
case Type_Expedition
when 'B' then 'Total Amount and Weight for expedition B'
else 'Total Amount and Weight for expedition not B'
end type,
sum(amount),
sum(weight)
from my_table
group by type
参见demo。
结果:
| Total Amount and Weight for expedition B | 4 | 6 |
| Total Amount and Weight for expedition not B | 8 | 12 |