发票、颜色、类型、计数

eiee3dmh  于 2021-05-29  发布在  Hadoop
关注(0)|答案(1)|浏览(439)

我有一张table在下面:

Invoice| Colour | Type

In_001 |  Red   | D  
In_001 |  Red   | D  
In_001 | Yellow | E  
In_002 |  Red   | H

我想要的结果是整理出任何发票有两个以上的项目购买和显示计数。
见下文,预期结果:

Invoice | Colour | Type | Count

In_001  |  Red   |  D   | 2  
In_001  | Yellow |  E   | 1

备注:由于只购买了一件物品,所以不包括在002中。
我尝试了以下命令:

select invoice, colour, type, count(invoice) from t   
group by invoice,colour,type   
having count(invoice)>1;

结果是:

Invoice | Colour | Type | Count

In_001  |  Red   |  D   | 2

请帮忙。

klr1opcd

klr1opcd1#

这三种发票、颜色和类型的分组依据都是。所以结果只能是这样。我们可以尝试下面的查询来实现它:-

select invoice,colour,type,count(invoice) from t where invoice in
(select invoice from t group by invoice having count(invoice) >1) 
group by invoice,colour,type

相关问题