How do I count distinct values from same id? - sql

How do I count distinct values from a column if there are some duplicates for each ID?
I've tried COUNT, DISTINCT, CASE and everything but I can't count distinct values for each ID. I have a table like this.
ID Symptom
1 Headache
1 Headache
1 Hematuria
1 Leg pain
1 Headache
2 Cough
2 Headache
2 Cough
3 Cough
3 Cough
3 Cough
I want to obtain something like this.
ID Symptom
1 Headache
1 Hematuria
1 Leg pain
2 Cough
2 Headache
3 Cough
Or how do I obtain the total count? Like there are 5 distinct symtomps but not 11 symptoms (If I use DISTINCT I would obtain 4).

You need distinct with select statement :
select distinct id, Symptom
from table t;
If you need unique count then use it inside count() :
select id, count(distinct Symptom)
from table t
group by id;

Just group by those fields :
SELECT ID, Symptom
FROM Symptoms
GROUP BY ID, Symptom
Fiddle : http://sqlfiddle.com/#!18/48dde/2/0

Try this:
SELECT ID, Symptom
FROM MY_TABLE
GROUP BY 1,2

Related

How to get grouping of rows in SQL

I have a table like this:
id name
1 washing
1 cooking
1 cleaning
2 washing
2 cooking
3 cleaning
and I would like to have a following grouping
id name count
1 washing,cooking,cleaning 3
2 washing,cooking 2
3 cleaning 1
I have tried to group by ID but can only show count after grouping by
SELECT id,
COUNT(name)
FROM WORK
GROUP BY id
But this will only give the count and not the actual combination of names.
I am new to SQL. I know it has to be relational but there must be some way.
Thanks in advance!
in postgresql you can use array_agg
SELECT id, array_agg(name), COUNT(*)
FROM WORK
GROUP BY id
in mysql you can use group_concat
SELECT id, group_concate(name), COUNT(*)
FROM WORK
GROUP BY id
or for redshift
SELECT id, listagg(name), COUNT(*)
FROM WORK
GROUP BY id

Count items in column SQL query

Let's say I have a table that looks like,
id
2
2
3
4
5
5
5
How do I get something like,
id count
2 2
3 1
4 1
5 3
where the count column is just the count of each id in the id column?
You want to use the GROUP BY operation
SELECT id, COUNT(id)
FROM table
GROUP BY id
select id, count(id) from table_name group by id
or
select id, count(*) from table_name group by id
This is your query:
SELECT id, COUNT(id)
FROM table
GROUP BY id
What GROUP BY clause does is this:
It will split your table based on ids i.e all your 1's are separated, then the 2's , 3's and so on. You can assume it like new tables are created where in one table all the 1's are stored, 2's in another , 3's in yet another and so on.
Then after that the SELECT query is applied on each of these separate tables and the result is returned for each of these "groups".
Good luck!
Kudos! :)

What exactly does SELECT DISTINCT(COUNT(*)) do?

I used the following query and it returned what I wanted it to return, but I'm having a tough time wrapping my head around what the query is doing.
Query is nothing fancier than what's in the title: select distinct(count(*)) from table1
Distinct is not required in your SQL ,as you are going to get only result, count(*) without group by clause returns, count of all rows within that table.
Hence try this :
select count(*) from table1
Distinct is used for finding distinct values from a group of values:
say you have table1 , with column1 as :
Column1
----------
a
a
b
b
a
c
following sqls are run you will get output as :
1) select count(*) from table1
output :6
2) select distinct(count(*)) from table1
output :6
3) select count( distinct column1) from table1
output :3
Usually distinct is used inside count preferably with a particular column .
select count( distinct column_name_n ) from table1
The distinct is redundant... Select Count(*) with only one table can only generate one value, so distinct (which would eliminate duplicates) is irelelvant.
If you had multiple outputs, (if for example you were grouping on something) then it would cause the query to only display one output row for every distinct value of count(*) that would other wise be generated...
if, for example, you had
name
Bob
Bob
Bob
Bob
Mary
Mary
Mary
Mary
Dave
Dave
Al
George
then
select count(*)
From table
group By name
would result in
4
4
2
1
1
but
select distinct count(*)
From table
group By name
would result in
4
2
1

Number of times one row column equals another row's other column in SQL

The confusing question is best asked through an example. Say we have the following result set:
What I want to do is count how many times one number appears from both columns.
So the returning data set might look like:
ID Counted
0 4
1 2
9 1
13 1
My original thought was to do some sort of addition between the counts on both IDs, but I'm not exactly sure how to GROUP them in SQL in a way that is working.
You can do this with a subquery, GROUP BY, and a UNION ALL, like this:
SELECT ID, COUNT(*)
FROM(
SELECT ID1 AS ID FROM MyTable
UNION ALL
SELECT ID2 AS ID FROM MyTable
) source
GROUP BY ID
ORDER BY ID ASC

How to add sequence number for each element in a group using a SQL query without temp tables

My question is quite similar to the one posted in this link - How to add sequence number for groups in a SQL query without temp tables
But, I need to enumerate the occurrence of group. The final output to be like this:
Record
Group
GroupSequence
1
Chickens
1
2
Chickens
2
3
Cows
1
4
Horses
1
5
Horses
2
6
Horses
3
Plus this has to be done in Oracle SQL. Any ideas?
Maybe something like this:
SELECT
ROW_NUMBER() OVER(PARTITION BY [Group] ORDER BY Record) AS GroupSequence1,
RANK() OVER(PARTITION BY [Group] ORDER BY Record) AS GroupSequence2,
DENSE_RANK() OVER(PARTITION BY [Group] ORDER BY Record) AS GroupSequence3,
Table1.Group,
Table1.Record
FROM
Table1
GroupSequence1, GroupSequence2 and GroupSequence3 will get you the output you want.