简体   繁体   English

SQL:为其显示不同的项目和其他实例的数量

[英]SQL: Display Distinct Items and number of instances of something else for it

I have a table called Inventory that has 2 columns called Item_ID and Color. 我有一个名为Inventory的表,该表具有2列,分别称为Item_ID和Color。 There are many cases where the same Item_ID has multiple listings with different colors: 在许多情况下,相同的Item_ID具有多个具有不同颜色的清单:

Item_ID(1, 1) Color(Blue, Green)

I have to display a listing of all the DISTINCT Item_ID's and also the number of colors each distinct Item_ID comes in. 我必须显示所有DISTINCT Item_ID的清单,以及每个不同Item_ID的颜色数量。

I know to use SELECT DISTINCT Item_ID AS Item_ID FROM Inventory; 我知道要使用SELECT DISTINCT Item_ID AS Item_ID FROM Inventory; in order to get a listing of all the distinct Item_IDs, but I have no clue how to get a listing of the amount of colors each distinct Item_ID comes in. 为了得到所有不同的Item_ID的列表,但是我不知道如何获得每个不同的Item_ID的颜色数量的列表。

For the Table, I would like to display: Item_ID: 1, Colors: 2 对于表,我想显示: Item_ID: 1, Colors: 2

You can do group by on item_ID to count colors per item 您可以对item_ID进行分组以计算每个项目的颜色

    SELECT DISTINCT Item_ID AS    
    Item_ID , COUNT(Color) as   
    ColorCount FROM Inventory
    GROUP BY Item_ID

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM