SQLite-对select

时间:2018-09-17 17:36:15

标签: sql sqlite case

我正在尝试从第4行的一个案例中获得一个总和。我尝试将这个案例以总和包装起来,总和在s.qty周围,但没有任何效果。

select s.part,
max(case s.location when 'stock' then s.qty else 0 end) qty_stock,
max(case s.location when 'material' then s.qty else 0 end) qty_material,
case s.location when not 'stock' or 'material' then s.qty else 0 end qty_wip

from stock as s

group by s.part

输出

Part        qty_stock qty_material qty_wip
"Part 1"    "10"      "25"         "0"
"Part 2"    "12"      "0"          "0"
"Part 3"    "14"      "0"          "0"
"Part 4"    "16"      "0"          "0"
"Part 5"    "0"       "0"          "0"
"Part 6"    "0"       "0"          "0"

很高兴得到任何帮助。

2 个答案:

答案 0 :(得分:1)

您需要使用sum()来代替:

select s.part,
       sum(case when s.location = 'stock' then s.qty else 0 end) as qty_stock,
       sum(case when s.location = 'material' then s.qty else 0 end) as qty_material,
       sum(case when s.location not in ('stock', 'material') then s.qty else 0 end) as qty_wip
from stock as s
group by s.part;

答案 1 :(得分:0)

我想你想要

version: "2"

services:
    mongodb:
    image: mongo:latest
    environment: 
      - MONGO_DATA_DIR=/data/db
      - MONGO_LOG_DIR=/dev/null
      - MONGO_INITDB_ROOT_USERNAME=some_user
      - MONGO_INITDB_ROOT_PASSWORD=some_password
    volumes:
      - ./mongo_data:/data/db
    ports:
      - 27017:27017 

您的问题是select s.part, max(case s.location when 'stock' then s.qty else 0 end) as qty_stock, max(case s.location when 'material' then s.qty else 0 end) as qty_material, sum(case when s.location not in ('stock', 'material') then s.qty else 0 end ) as qty_wip from stock as s group by s.part; 表达式本身。使用完整条件应该可以解决问题。