I need to get name and count value in table - sql

I got a table like this
table userReport
id
name
surname
userId
resulId
1
test
test
123
1
2
test
test
123
1
3
test
test
123
2
4
test
test
123
3
5
john
test
124
3
6
john
test
124
2
7
john
test
124
1
8
james
test
125
3
9
james
test
125
2
My sql is trying to get name sunrame and count how many and also needed with resultId=1
SELECT
name+' '+ surname
,count(userId) AS userCount
FROM [userReport]
GROUP BY userId
it gives error
Column 'userReport.name' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Column 'userReport.surname' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
also I want to add a where for resultId = 1 in the same select
So its be like joining the theese three selections in one
select name+' '+ surname from [userReport]
select count(userId) as userCount from [userReport] group by userId
select count(userId) as resultCount from [userReport] where resulId='1' group by userId

The quickest fix might be to just add the name and surname to the GROUP BY clause:
SELECT userId, name + ' ' + surname AS full_name, COUNT(*) AS userCount
FROM userReport
GROUP BY userId, name + ' ' + surname;
Note that I have also added userId to the SELECT clause. You might want to do this in the event that two users happen to have the same first and last name.

Related

count different column values after grouping by

Consider this table:
id name department email
1 Alex IT blah#gmail.com
1 Alex IT blah#gmail.com
2 Jay HR jay#gmail.com
2 Jay Marketing zou#gmail.com
If I group byid,name and count I get:
id name count(*)
1 Alex 2
2 Jay 2
With this query:
select id,name,count(*) from tb group by id,name;
However I would like to count only records that diverge from department,email, so as to have:
id name count(*)
1 Alex 0
2 Jay 1
This time the count for the first group 1,Alex is 0 because department,email have the same values (duplicated) , on the other hand 2,Jay is one because department,email has one different value.
If you meant "two different values" for "Jay", you can use distinct:
select id,name,count(*) from (SELECT distinct * FROM tb) group by id,name;
You can use count(*) - 1 to get similar results in your question.

Finding distinct count of combination of columns values in sql

Currently I have a table this :
Roll no. Names
------------------
1 Sam
1 Sam
2 Sasha
2 Sasha
3 Joe
4 Jack
5 Jack
5 Julie
I want to write a query in which I get count of the combination in another column
Required output
Combination distinct count
-----------------------------
2-Sasha 1
5-Jack 1
5-Julie 1
Basically, you could group by these columns and use a count function:
SELECT rollno, name, COUNT(*)
FROM mytable
GROUP BY rollno, name
You could also concat the two columns:
SELECT CONCAT(rollno, '-', name), COUNT(*)
FROM mytable
GROUP BY CONCAT(rollno, '-', name)

SQL: Adding new column to show count of ID by date

I am hoping someone can help me with my query.
I have a table with the columns, 'Date', 'ID_Num and 'Name'. What I want to do is add a column at the end to show the total amount of times each ID_Num is within the data but based on the date. So although 'ID_Num' 1001 shows 4 times in total, it is twice on the 20/04/2018 and once on both the 21/04/2018 and 22/04/2018.
EDIT: I should have stipulated that I will be pulling several other columns with information, which I cant use a group by on everything.
Date ID_Num Name Count
20/04/2018 1001 John 2
20/04/2018 1001 John 2
20/04/2018 1002 Paul 2
20/04/2018 1002 Paul 2
20/04/2018 1003 David 2
20/04/2018 1003 David 2
20/04/2018 1004 Stephen 1
21/04/2018 1001 John 1
21/04/2018 1002 Paul 3
21/04/2018 1002 Paul 3
21/04/2018 1002 Paul 3
21/04/2018 1004 Stephen 1
22/04/2018 1001 John 1
22/04/2018 1002 Paul 1
22/04/2018 1003 David 1
22/04/2018 1004 Stephen 1
Thanks
Unless I'm missing something here, a simple group by and count should do it:
SELECT Date, ID_Num, Name, Count(*)
FROM TableName
GROUP BY Date, ID_Num, Name
(That is, assuming there can only be one Name for each ID_Num)
Update
Assuming your rdbms supports it, you can use count with an over clause:
SELECT Date, ID_Num, Name, Count(*) OVER(PARTITION BY Date, Id_Num)
FROM TableName
If not, you can use a sub query:
SELECT Date,
ID_Num,
Name,
(SELECT Count(*)
FROM TableName As t1
WHERE t1.Date = t0.Date
AND t1.ID_NUM = t0.ID_NUM)
FROM TableName As t0
Try this:
SELECT
Date,
Id_num,
count(*) count
FROM
tabel_name
GROUP BY
Date,
Id_num
If you want name as well:
SELECT
Date,
Id_num,
Name
count(*) count
FROM
tabel_name
GROUP BY
Date,
Id_num,
Name
You can use a normal select query and then add a sub query to do a group and show the total. Simple example below
SELECT Date, ID_Num, Name,
(SELECT Count(ID_Num) FROM TableName AS CHILD WHERE CHILD.Id_Num = Parent.Id_Num) AS Total
FROM TableName AS Parent

How to select only details of min value only in SQL?

I could get the minimum percentage of two values, but I need only the name, and ID in the select.
ID NAME CITY ONE TWO
--------------------------------------------------
2 Morales Los Angeles 40 10
1 John New York 60 20
4 Mary San Diego 10 10
I need to get the min value of one/two, and to only appear this as a result:
ID NAME
---------
4 Mary
Select ID, NAME
from MYTABLE
where least(ONE,TWO) = (select min(least(ONE,TWO)) from MYTABLE);
If you don't want Morales, then you can do this :
Select ID, NAME
from MYTABLE
where id =
(select id from
(select id from MYTABLE order by least(ONE,TWO), ONE*TWO)
where rownum <= 1);

Count distinct same names

I have table where have over 100k information.
ID FirstName
1 Bob
2 Bob
3 Tom
4 John
5 John
6 John
.. ....
Want procedure which will be count how much names are same, For example it must be like :
FirstName Count
Bob 2
Tom 1
John 3
Please help me to write it
It's very basic SQL example, group by column + aggregating results
select
FirstName, count(*)
from Table1
group by FirstName
Try this
select FirstName,Count(FirstName) From TableA group by FirstName
Try this
SELECT FirstName, COUNT(*) As Count
FROM YourTable
GROUP BY FirstName
HAVING COUNT(*) > 1
ORDER BY COUNT(*) DESC
Create Procedure GetCount
as
BEGIN
Select FirstName,Count(*) from tablename group by FirstName
END