[英]How Do I Count Occurrences in a Column in SQL?
I have a table that lists the following values. 我有一个列出以下值的表。 Some of the values in column 1 show up twice for the same customer (for example apple
could show up 2 or more times.) 对于同一位客户,第1列中的某些值显示两次(例如, apple
可能显示2次或更多次)。
Column 1
Apples
Oranges
Bananas
I know I can use this to get the count for each of the values. 我知道我可以使用它来获取每个值的计数。
SELECT column 1, COUNT(*)
FROM table
GROUP BY column 1
Is there a way to write a SQL statement that looks at when the count of one of those values shows up twice for the same customer? 有没有一种方法可以编写一条SQL语句来查看同一客户两次出现这些值之一的计数?
For example, I want to find out how many times apple shows up twice for the same customer. 例如,我想找出苹果为同一位顾客显示两次的次数。
Assuming you have a customer column, your query could look like this: 假设您有一个“客户”列,则查询可能如下所示:
SELECT customer, column1, COUNT(*)
FROM table
GROUP BY customer, column1
HAVING COUNT(*) > 1;
This will show you a customer field, and your column1 (the column with Apple and Banana and so on), and the number of times the combination of these fields occur. 这将向您显示一个客户字段,以及您的column1(包含Apple和Banana等的列)以及这些字段组合发生的次数。 The GROUP BY
will ensure that the count is calculated correctly. GROUP BY
将确保正确计算计数。 The HAVING
will only show records where the COUNT
value is > 1 after the grouping has occurred. HAVING
将仅显示COUNT
值> 1的记录。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.