Count items in column SQL query - sql

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! :)

Related

Get Count Based on Combinations of Values from Second Column

I have a table format like below:
Id Code
1 A
1 B
2 A
3 A
3 C
4 A
4 B
I am trying to get count of code combinations like below:
Code Count
A,B 2 -- Row 1,2 and Row 6,7
A 1 -- Row 3
A,C 1 -- Row 4
I am unable to get the combination result. All I can do is group by but I am not getting count of IDs based in combinations.
You need to aggregate the rows, somehow, and do that twice. The code looks something like this:
select codes, count(*) as num_ids
from (select id, group_concat(code order by code) as codes
from t
group by id
) id
group by code;
group_concat() might be spelled listagg() or string_agg() depending on the database.
In SQL Server, use string_agg():
select codes, count(*) as num_ids
from (select id, string_agg(code, ',') within group (order by code) as codes
from t
group by id
) id
group by code;

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

How to count and select records with count = 1 across multiple columns

Hi and thanks in advance.
Trying to simplify two SQL statements without much success.
Count unique records in a table (by unique, I mean the whole record is found only once in the table)
Select unique records in a table (by unique, I mean the whole record is found only once in the table)
Since the table has 30 columns, is there some way to simply specify ALL columns in the table rather than having to include all individually in the SQL?
I got this working where you spell out every column name (where 'col n name' refers to the last column) but it is not ideal since there are just so many columns …
SELECT col 1 name, col 2 name, col 3 name, …, col n name FROM table name
GROUP BY col 1 name, col 2 name, col 3 name, …, col n name
Having Count(*)=1
Thanks
deutz
create view tab_view1 as select DISTINCT * from tab;
select COUNT(*) from tab_view1; -- first desired result
select * from tab_view1; -- second desired result
First occurence is unique, second is not.
If you want to exclude any record that has duplicate:
create view tab_view2 as select tab.*, COUNT(*) AS occurs
from tab group by tab.*
having COUNT(*) = 1;
select COUNT(*) from tab_view2; -- first desired result
select * from tab_view2; -- second desired result

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

select data and if duplicate id add (sum) the data

I am using TSQL to select data from the database. I want to select all data in a table but if the id is a duplicate add (sum) all column 'a' with same ID and make it just 1 row.
There should be no duplicate IDs in the output.
SELECT DISTINCT id,a,b FROM dbo.test WHERE
id not in (select id from dbo.test) CASE a WHEN a + a??
example:
dbo.test
========
id a
1 4
1 5
2 3
3 2
output:
1 9 <-- two ids of 1 so column 'a' is added together.
2 3
3 2
SELECT ID, SUM(a) as a, SUM(b) as B
FROM MyTable
GROUP BY ID
This is what grouping and aggregation is designed for!
FYI, this is how aggregation works:
When you group and aggregate fields, you are combining records into a single row.
In your OP, you wanted to see a SUM of A for every value of ID. That's what the original query I posted does.
You also want to include B, which is a varchar field, according to your comments.
In that case, you need to decide what to do with B. Since you are grouping multiple rows together, there are (potentially) multiple values of B per value of ID. You need to either:
Group by B as well, which will add extra rows
Apply an aggregation such as MAX(), MIN(), etc. to B
Exclude B from the results list.