基于条目相同性的MySQL大表计数

基于条目相同性的MySQL大表计数,mysql,Mysql,我有一张这样的桌子: Key . ClerkID . CustomerID . ProductCode . Date Type . Color 1 1010 123 AAA 2018-01-01 . 1 . Red 2 1010 123 BBB 2018-01-01 . 2 . Blue 3 1010

我有一张这样的桌子:

Key .  ClerkID . CustomerID .  ProductCode .  Date          Type .   Color
1      1010      123           AAA            2018-01-01 .  1 .      Red
2      1010      123           BBB            2018-01-01 .  2 .      Blue
3      1010      123           AAA            2018-01-02 .  1        Red
4      1010      456           CCC            2018-01-01 .  1 .      Red
5 .    1010      456           DDD            2018-01-02 .  2        Red
6      1010      456           DDD            2018-01-02 .  3        Blue
7      11 .      456           AAA            2018-01-02 .  3        Blue
Products .    Count
AAA .         2
AAA, BBB      1
CCC, DDD      1
DDD           1
大约30万整。我正在尝试运行一个查询,对同一客户、同一职员在同一日期订购的多个产品进行计数

所以它会报告这样一个表:

Key .  ClerkID . CustomerID .  ProductCode .  Date          Type .   Color
1      1010      123           AAA            2018-01-01 .  1 .      Red
2      1010      123           BBB            2018-01-01 .  2 .      Blue
3      1010      123           AAA            2018-01-02 .  1        Red
4      1010      456           CCC            2018-01-01 .  1 .      Red
5 .    1010      456           DDD            2018-01-02 .  2        Red
6      1010      456           DDD            2018-01-02 .  3        Blue
7      11 .      456           AAA            2018-01-02 .  3        Blue
Products .    Count
AAA .         2
AAA, BBB      1
CCC, DDD      1
DDD           1
实际上,我希望忽略任何仅为单一产品的条目:

Products .    Count
AAA, BBB      1
CCC, DDD      1
编辑:这里有一把小提琴:

最后的表格是: 文员10101030;产品:AAA、BBB、CCC;cnt:3 文员10101020;产品:AAA、BBB;cnt:2 文员:101010030;产品:AAA;cnt:3

实际上忽略最后一行是可行的

或者更容易阅读的小提琴: 目标是: 职员:卡尔、鲍勃、戴夫;产品:AAA、BBB、CCC;cnt:4

根据您的数据,对同一客户、同一职员和同一日期订购的不同产品进行计数,并忽略仅出现一次的产品的查询是忽略此处的类型和颜色的查询,尽管单个产品代码可能有多种类型和颜色:

SELECT
ClerkID,
CustomerID,
Date,
ProductCode,
COUNT ( * ) as cnt 
FROM <your table name>
GROUP BY
ClerkID,
CustomerID,
Date,
ProductCode 
HAVING cnt > 1
;

有点像,但这不报告产品代码列表,每个计数只有1个产品代码。一张便条,我不得不把COUNT*改成COUNT*。我不太熟悉,不知道你的要求有点不清楚,你能再详细一点吗,如果可能的话,把产品代码改为:group\u concatProductCode separator',“ProductCode哦,等等,不,那不行。它只是重复列出相同的产品代码。据我所知,您想根据ClerkID、CustomerID、Date和total count,使用逗号分隔符对产品进行分组吗?是吗?这是一个sqlfiddle:对于任何绊倒的人:这是一个工作的fiddle:代码选择产品,group_concatdistinct ClerkID,COUNTPRODUCTS as cnt FROM SELECT ClerkID,CustomerID,Date,group_concatdistinct ProductCode as Products,countdistinct ProductCode as cnt FROM Table1 group by ClerkID,CustomerID,按产品分类cnt>1 t组的日期