SQL- Do I need some kind of Count function? [duplicate] - sql

This question already has answers here:
Selecting COUNT(*) with DISTINCT
(7 answers)
Closed 4 years ago.
I need to query a database that has about 10-11 columns, including a column of id's and a column of role codes. Those are the 2 column that i'm interested in.
ID ROLE
1 a
2 a
2 b
2 c
3 a
4 a
4 b
I need to count how many role codes exist for each ID. (Basically like counting the number of times each id exists in the database)
Output should be something like this:
ID Count
1 1
2 3
3 1
4 2

Use count distinct:
SELECT ID, COUNT(DISTINCT ROLE)
FROM YOURTABLE
GROUP BY ID

Related

Getting top 5 most associated item [duplicate]

This question already has answers here:
How to get highest count of associated model (Rails)?
(3 answers)
Closed 1 year ago.
I have a Postgres database with a many-to-many association table that's similar to what's down below.
id | item_id | item_tag_id
1 101 3
2 102 3
3 103 1
4 104 2
5 105 2
How can I get the top 5 most associated item_tag_id?
Basically, group by item and order by the count of rows (= count of items in a proper many-to-many design):
SELECT item_id, count(*)
FROM assoc_tbl
GROUP BY 1
ORDER BY 2 DESC
LIMIT 5;
There is a remaining corner-case: how to break ties for the top 5? Either define criteria (resulting in more ORDER BY expressions), or consider WITH TIES. See:
Get top row(s) with highest value, with ties
Can I do a max(count(*)) in SQL?

Increment row number based on value 1 and value 2 [duplicate]

This question already has answers here:
How to use RANK() in SQL Server
(8 answers)
Closed 3 years ago.
How can I create a sequential value based on two rows within a table, for example, let's say I have a table containing an employee's ID and work state. I would expect the following values:
ID State Expected Value
-----------------------------
1 NY 1
1 PA 2
1 NY 1
2 NC 1
2 FL 2
2 MN 3
You can use dense_rank():
select t.*,
dense_rank() over (partition by id order by state) as expected
from t;

SQL Group By min value usable as a variable? [duplicate]

This question already has answers here:
MySQL - Fetching lowest value
(7 answers)
Closed 3 years ago.
I have the following table ComponentsB:
CID Name PPP ProcessingCharge LName Typ
1 RadXL 10 5 EisenAG Rad
2 RadXL 15 0 LederGmbH Rad
3 RadL 10 2 LederGmbH Rad
4 RadXL 2 1 RadAG Rad
5 RadXL 4 10 sdfkj Rad
6 SchraubeM 1 2 EisenAG Schraube
7 ZangeS 3 11 EisenAG Zange
8 ZangeM 12 12 sdfkj Zange
I want to get for every "Name" the lowest "ProcessingCharge" with the corresponding LName
select Name, min(ProcessingCharge)
from ComponentsB
group by Name
works perfectly fine but I can't get the corresponding LName because I get the error that it is not part of the aggregate function.
Don't think of this query as an aggregation query. Think of it as a filtering query. You want the row for each Name that has the minimum charge:
select c.*
from ComponentsB c
where c.ProcessingCharge = (select min(c2.ProcessingCharge)
from ComponentsB c2
where c2.Name = c.Name
);

MS SQL count total items, but only once per user [duplicate]

This question already has answers here:
Selecting COUNT(*) with DISTINCT
(7 answers)
Closed 4 years ago.
I want the total unique occurences of NAME from the MS SQL database, but only once for every user. Below is a sample of my data.
USERID NAME
------------------------
1 name 1
1 name 1
1 name 1
2 name 1
2 name 2
2 name 3
3 name 1
3 name 1
3 name 3
4 name 1
If I use the following query
SELECT COUNT(name) AS total, name FROM TestTable GROUP BY name
I get the following results
7 name 1
1 name 2
2 name 3
But what I want is the result below. The query should ignore 2x name 1 from user 1 and 1x name 1 from user 3.
4 name 1
1 name 2
2 name 3
I have insufficient knowledge of SQL to get this to work, or to know the correct terms to use in the query. So there probably is a duplicate and if so, please point me to it.
You can use distinct userid for count()
SELECT COUNT(distinct userid) AS total, name FROM TestTable GROUP BY name
use distinct and count() userid
SELECT COUNT(distinct userid) AS total, name FROM TestTable GROUP BY name
You want DISTINCT inside COUNT() to ignore duplicate counts :
SELECT COUNT(DISTINCT USERID) AS total, name
FROM TestTable
GROUP BY name;

Get rows with single values using SQlite

By using SQlite, I'd like to get all rows that show in a specific column only one single distinct value. Like from following table:
A B
1 2
2 1
3 2
4 3
5 1
6 1
7 2
8 4
9 2
Here I'd like to get only row Nr. 4 an 8 as there values (3 and 4) occur only once in the entire column.
You could use a query like this:
SELECT *
FROM mytable
WHERE B IN (SELECT B FROM mytable GROUP BY B HAVING COUNT(DISTINCT A)=1)
Please see fiddle here.
Subquery will return all B values that are present only once (you could also use HAVING COUNT(*)=1 in this case), the outer query will return all rows where B is returned by the subquery.