List all distinct values of column and their count - sql

I have a column with different text values. How can I get a list of all the unique values and the count of the appearance of them in the column?

Simplest way is to use GROUP BY
select text_column, count(*) from text_table group by text_column
more info - http://www.w3schools.com/sql/sql_groupby.asp

SELECT column_name
, COUNT(*)
FROM table_name
GROUP BY column_name
;

Related

joining two columns sql query

world!
I'm currently stuck on this problem where i want to join two columns and run the select statement of the two, but i'm getting errors; these are the columns i want to join:
SELECT DISTINCT column_name FROM owner_name.table_name ORDER BY column_name;
and
SELECT DISTINCT * FROM (SELECT count(column_name) OVER (partition by column_name) Amount from owner_name.table_name order by column_name);
where in the second, for every row, i count how many equal rows i have for each value.
the two columns values:
first column
second column
i dont know how to have both of them next to each other as a normal select statement:
SELECT column_1, column_2 FROM table;
You do not want to use an analytic function for this as you will find the COUNT for all the rows and then use DISTINCT to discard rows which involves lots of unnecessary calculation.
Instead, it is much more efficient GROUP BY the column_name and then aggregate so that you only generate a single row for each group to start with:
SELECT column_name,
COUNT(column_name) AS amount
FROM owner_name.table_name
GROUP BY column_name
ORDER BY column_name;
SELECT DISTINCT
column_name,
COUNT(column_name) OVER (PARTITION BY column_name) Amount
FROM owner_name.table_name
ORDER BY column_name;

How to grouping postgresql when id is same into new add value

I have table like this:
And I would like to bring value item in one row when user_input_id and question_id is duplicate.
The result that I wish is this:
Can anyone tell me how to querying it?
Thank You
You can easily do taht with string_agg(value,',') in postgresql.
select user_input_id,question_id,
string_agg(value,',') as other_names
from table_name
group by user_input_id,question_id
order by user_input_id,question_id
Output:
You can also have array_aggr() in postgres:
select user_input_id,question_id,
array_agg(value::text ) as other_names
from table_name
group by user_input_id,question_id
order by user_input_id,question_id
Output:
Just fill a temp table with data of this table and truncate it. Then use aggregate functions like this:
CREATE TEMP TABLE temp_tbl(
columns ...
);
insert into temp_tbl
select * from main_tbl
truncate table main_tbl
insert into main_tbl
select user_input_id,question_id, string_agg(value, ',')
from temp_tbl
group by user_input_id,question_id

count all the distinct records in a table

I need to count all the distinct records in a table name with a single query and also without using any sub-query.
My code is
select count ( distinct *) from table_name
It gives an error:
Incorrect syntax near '*'.
I am using Microsoft SQL Server
Try this -
SELECT COUNT(*)
FROM
(SELECT DISTINCT * FROM [table_name]) A
I'm afraid that if you don't want to use a subquery, the only way to achieve that is replacing * with a concatenation of the columns in your table
select count(distinct concat(column1, column2, ..., columnN))
from table_name
To avoid undesired behaviours (like the concatenation of 1 and 31 becoming equal to the concatenation of 13 and 1) you could add a reasonable separator
select count(distinct concat(column1, '$%&£', column2, '$%&£', ..., '$%&£', columnN)
from table_name
You can use CTE.
;WITH CTE AS
(
SELECT DISTINCT * FROM TableName
)
SELECT COUNT(*)
FROM CTE
Hope this query gives you what you required.
As others mentioned, you cannot use DISTINCT with *. Also it is good practice to use a column name instead of the *, like a unique key / primary key of the table.
SELECT COUNT( DISTINCT id )
FROM table
select distinct Name , count(Name) from TableName
group by Name
having count(Name)=1
select ##rowcount
I had the same issue involving a query that had multiple joins to tables and I could not simply do count(distinct ) or count(distinct alias.).
My solution was to create a string made up of the key columns I cared about and count them.
SELECT Count(DISTINCT person.first || '~' || person.last)
from person;
If you want to use DISTINCT keyword, you need to specify column name on which bases you want to get distinct records.
Example:
SELECT count(DISTINCT Column-Name) FROM table_name

Find duplicated rows that are not exactly same

Can i select all rows that have same column value (for example SSN field) but display them all separably. ?
I've searched for this answer but they all have "count(*) and group by" section that demands the rows to be exactly same.
Try This:
SELECT A, B FROM MyTable
WHERE A IN
(
SELECT A FROM MyTable GROUP BY A HAVING COUNT(*)>1
)
I have done with SQL server. But hope this is what you need
Here is another approach, which only references the table once, using an analytic function instead of a subquery to get the duplicate counts It might be faster; it also might not, depending on the particular data.
SELECT * FROM (
SELECT col1, col2, col3, ssn, COUNT(*) OVER (PARTITION BY ssn) ssn_dup_count
)
WHERE ssn_dup_count > 1
ORDER BY ssn_dup_count DESC
SELECT
*
FROM
MyTable
WHERE
EXISTS
(
SELECT
NULL
FROM
MyTable MT
WHERE
MyTable.SameColumnName = MT.SameColumnName
AND MyTable.DifferentColumnName <> MT.DifferentColumnName)
This will fetch the required data and show them in order so that we can see the grouped data together.
SELECT * FROM TABLENAME
WHERE SSN IN
(
SELECT SSN FROM TABLENAMEGROUP BY SSN HAVING COUNT(SSN)>1
)
ORDER BY SSN
Here SSN is the column names fro which similar value check is done.

COUNT of DISTINCT items in a column

Here's the SQL that works (strangely) but still just returns the COUNT of all items, not the COUNT of DISTINCT items in the column.
SELECT DISTINCT(COUNT(columnName)) FROM tableName;
SELECT COUNT(*) FROM tableName
counts all rows in the table,
SELECT COUNT(columnName) FROM tableName
counts all the rows in the table where columnName is not null, and
SELECT (DISTINCT COUNT(columnName)) FROM tableName
counts all the rows in the table where columnName is both not null and distinct (i.e. no two the same)
SELECT DISTINCT(COUNT(columnName)) FROM tableName
Is the second query (returning, say, 42), and the distinct gets applied after the rows are counted.
You need
SELECT COUNT(DISTINCT columnName) AS Cnt
FROM tableName;
The query in your question gets the COUNT (i.e. a result set with one row) then applies Distinct to that single row result which obviously has no effect.
SELECT COUNT(*) FROM (SELECT DISTINCT columnName FROM tableName);