【发布时间】:2020-08-27 12:10:17
【问题描述】:
有一个表名为:assembly
列:
id: number
partId: number
index: number
对于一个程序集,我有一个索引为:1、2、3 的零件。
但我可以替换索引 2 处的部分,所以我有 2 个具有相同索引的部分:
id partId index
1 234 1
1 456 2
1 789 2
1 765 3
我的组件是零件 234、456 或 789、765。
我需要知道我的装配中有多少零件(3 个零件)
我试过了:
select count(assembly.index) from assembly where assembly.id in (1) group by assembly_id
我得到:4
好的,这很正常。
我想通过对索引进行分组,我会得到真正的计数:
我试过了:
select count(assembly.index) from assembly where assembly.id in (1) group by assembly.id and assembly.index
我得到 2 行,计数为 2
我应该得到一个计数为 3 的单行。
另外,如果我更改 where 语句以获取许多 assemble like 的计数
where assembly.id in (1,2,3)
我在每个程序集中得到一排计数错误。
有谁知道我怎样才能得到准确的计数?
谢谢
【问题讨论】:
-
我不明白。如果您有一个程序集的三个索引,这是否意味着需要 3 个产品?为什么你的例子只给出了两个?
标签: sql sqlite group-by count where-clause