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

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;

Related

What is the proper way to complete cross-tab on the following segment in SQL?

I create frequencies on one column in SQL in a standard way.
My code is
select id , count(*) as counts
from TABLE
group by id
order by counts desc
Suppose the output is as follows for six id
id counts
-- -----
1 3 two id have 3 counts per
2 3
---------
3 6 three id have 6 counts per
4 6
5 6
---------
6 2 one id has 2 counts
How can I produce the following?
nid counts
--- ------
1 2
2 3
3 6
I am writing in a hive environment, but that should be standard SQL.
Thanks in advance for answering.
You want two levels of aggregation:
select counts, count(*)
from (select id , count(*) as counts
from TABLE
group by id
) c
group by counts
order by counts;
I call this a "histogram-of-histograms" query. I usually include min(id) and max(id) in the outer select, so I have examples of ids with given frequencies.

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

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

Find duplicate sql record

I have a database table in vbulletin, where a table has just 2 columns:
Col 1 : userid
Col 2 : relationid
Col1 may have multiple entries, like:
userid relationid
1 A
1 B
1 C
2 B
2 T
I would like to extract a csv or just manage to order them, so i could end up with some thing like this:
userid entries
1 100
2 12
3 44
4 33
5 33
Where userid is the repeating number in col1, and entries is how many times the userid is repeating itself.
Try like this;
select userid, count(*) as entries from table group by userid
SELECT userid, COUNT(userid) FROM TableName GROUP BY userid;
This query will return userid and a count of all userids from TableName. Because we are using COUNT, we also must GROUP BY any additional columns we are selecting.

SQL - Order by amount of occurrences

It's my first question here so I hope I can explain it well enough,
I want to order my data by amount of occurrences in the table.
My table is like this:
id Daynr
1 2
1 4
2 4
2 5
2 6
3 1
4 2
4 5
And I want it to sort it like this:
id Daynr
3 1
1 2
1 4
4 2
4 5
2 4
2 5
2 6
Player #3 has one day in the table, and Player #1 has 2.
My table is named "dayid"
Both id and Daynr are foreign keys, together making it a primary key
I hope this explains my problem enough, Please ask for more information it's my first time here.
Thanks in advance
You can do this by counting the number of times that things occur for each id. Most databases support window functions, so you can do this as:
select id, daynr
from (select t.*, count(*) over (partition by id) as cnt
from table t
) t
order by cnt, id;
You can also express this as a join:
select t.id, t.daynr
from table as t inner join
(select id, count(*) as cnt
from table
group by id
) as tg
on t.id = tg.id
order by tg.cnt, id;
Note that both of these include the id in the order by. That way, if two ids have the same count, all rows for the id will appear together.

How do I use a select query to get the least of one value for each unique second value?

There are groups like this;
USER_ID SEQ_ID NAME
1 2 Armut
1 3 Elma
1 4 Kiraz
2 1 Nar
2 2 Uzum
4 3 Sheftali
4 4 Karpuz
4 5 Kavun
After select query I want to see only;
USER_ID SEQ_ID NAME
1 2 Armut
2 1 Nar
4 3 Karpuz
That is, I want the row with the least SEQ_ID for each USER_ID. What SQL query will give me this result?
Best regards
SELECT USER_ID, SEQ_ID, NAME
FROM table
WHERE NAME IN ('Armut', 'Nar', 'Karpuz')
ORDER BY USER_ID
If you have something else in mind, please clarify your question.
Looks to me like it should be:
SELECT USER_ID, MIN(SEQ_ID) AS SEQ_ID, NAME
FROM table
GROUP BY USER_ID, NAME
ORDER BY USER_ID;