[英]How to format and print a result of sql query
I recently got this question on interview which I failed to answer. 我最近在面试中遇到了这个问题,但未能回答。 The question was to list the number of duplicates that appear in a column employer like from this table 问题是列出出现在此表中的雇主列中重复项的数量,例如此表
id | employer | employee
1 | isro | dude1
2 | isro | dude 2
3 | cnd | dude 3
4 | df | dsfdef
5 | dfdf | dfdfd
...
so the result should be like 所以结果应该像
isro = 2
df = 4
dfsf = 6
how do i achieve this? 我该如何实现? I know there is count(*) which i could use with select statement with where clause, but how do i achieve the above result. 我知道有count(*)可以与带有where子句的select语句一起使用,但是我如何实现上述结果。
The HAVING
clause can be used to filter on aggregated values: HAVING
子句可用于过滤汇总值:
SELECT employer, COUNT(*)
FROM yourTable
GROUP BY employer
HAVING COUNT(*) > 1
assuming TableName is the name of the table you want to select from, this would be your answer. 假设TableName是您要从中选择表的名称,这就是您的答案。
SELECT employer, count(employer)
FROM TableName
GROUP BY employer
HAVING COUNT(*) > 1
here is an answer to a very similar question that has some more info for you. 这是一个非常类似的问题的答案,为您提供了更多信息。
How to count occurrences of a column value efficiently in SQL? 如何在SQL中有效地计算列值的出现?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.