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?
You can count distinct values like this:
SELECT COUNT(DISTINCT column_name) FROM table_name;
So use COUNT
with DISTINCT
of string field stringField
in your table t
:
SELECT COUNT(DISTINCT stringField) FROM t
Try this:
SELECT COUNT(DISTINCT colmnname) FROM tablename
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