Reputation: 718
I have a situation where I have to count number of strings of different type in one column of a table, e.g. a column would have values such as :
apple
apple
apple
orange
orange
banana
banana
banana
banana
So I need to count the strings only of different type, that means query should give count result 3. What can be the ideal query for this?
Upvotes: 0
Views: 318
Reputation: 57316
Instead of using simple COUNT(field)
, you could use COUNT(DISTINCT field)
:
SELECT COUNT(DISTINCT fieldname) FROM tablename
If your values can have different upper/lower case variants and you still only want the real unique (e.g. Banana
is the same as banana
), then you can add lower
function into the mix:
SELECT COUNT(DISTINCT lower(fieldname)) FROM tablename
Upvotes: 0
Reputation: 1096
You can count distinct values like this:
SELECT COUNT(DISTINCT column_name) FROM table_name;
Upvotes: 2
Reputation: 37365
So use COUNT
with DISTINCT
of string field stringField
in your table t
:
SELECT COUNT(DISTINCT stringField) FROM t
Upvotes: 2