рассчитывать на основе условий mysql 5,7 - PullRequest
0 голосов
/ 15 апреля 2020

я использовал mysql версия 5.7, у меня есть таблица производства для каждого продукта, количества, и я использую много таких экспедиций, как это

+---------+-----------------+------+--------+---------+
| 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 |
+---------------------------------------------------+---+----+

1 Ответ

1 голос
/ 15 апреля 2020

Вы можете использовать UNION ALL для последних 2 строк:

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

См. Демоверсию . Результаты:

| 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

См. Демонстрационную версию . Результаты:

| Total Amount and Weight for expedition B     | 4           | 6           |
| Total Amount and Weight for expedition not B | 8           | 12          |
...