Count single occurrences of a row item - sql

I would like to count the number of times an item in a column has appeared only once. For example if in my table I had...
Name
----------
Fred
Barney
Wilma
Fred
Betty
Barney
Fred
...it would return me a count of 2 because only Wilma and Betty have appeared once.

Here is SQLFiddel Demo
Below is the Query which you can try:
select count(*) from
(select Name
from Table1
group by Name
having count(*) = 1) T
Till Above my post was for your actual Post.
Below is the post for modified question:
In oracle you can try below query:
select sum(count(rownum))
from Table1
group by "Name"
having count(*) = 1
OR
Here is SQLFiddel Demo
In SQL Server you can try below query:
SELECT COUNT(*)
FROM Table1 a
LEFT JOIN Table1 b
ON a.Name=b.Name
AND a.%%physloc%% <> b.%%physloc%%
WHERE b.Name IS NULL
OR
Here is the SQLFiddel Demo
In Sybase you can try below query:
select count(count(name))
from table
group by name
having count(name) = 1
as per #user2617962's answer.
Thank you

select count(*) from
(select count(*) from Table1
group by Name
having count(*) =1) s
SqlFiddle

Since you just need the count of column values appearing once without the actual value of the column, the query should be:
select count(count(name)) from table group by name having count(name)
= 1

Try following.
select name from (select name, count(name) as num from tblUsers group by name)
tblTemp where tblTemp.num=1
Mark it if this works..:)

Related

sql duplicates showing all data

Given this data
id Name group
1 Jhon 001
2 Paul 002
3 Mary 001
How can I get the duplicates values showing all the fields? The duplicate is only on group, id and name won't be duplicates.
Should end up looking like one of those (any would be valid):
:::::::::::::::::::::::::::::::::::::::::::::::
group count values
001 2 1,3
:::::::::::::::::::::::::::::::::::::::::::::::
id name group
1 Jhon 001
3 Mary 001
I tried with
SELECT
group, COUNT(*)
FROM
people
GROUP BY
group
HAVING
COUNT(*) > 1
But if I try to add id and name to the group by, it won´t find any duplicate.
Thanks in advance.
Try this.
SELECT Id, Name, [Group]
FROM people
WHERE [Group] IN(
SELECT [Group]
FROM people
GROUP BY [Group]
HAVING COUNT(*) > 1)
I would do an inner query to find the groups with more than one member, and then use that inner query to bring back a list of the names.
For example:
SELECT Id, Name, group
FROM people
WHERE group in
(SELECT group
FROM people
GROUP BY group
HAVING count(*) > 1);
Avoid using Group because it is a reserved keyword in SQL :
SELECT *
FROM MyTable
WHERE groups IN(
SELECT groups
FROM MyTable
GROUP BY groups
HAVING COUNT(*) > 1)
Check Execution here
Just use exists:
select p.*
from people p
where exists (select 1
from people p2
where p2.group = p.group and
p2.id <> p.id
);
This should be the most performant solution. With an index on people(group, id), it should have very good performance.
Note: All the advice to avoid using group as a column name is good advice. You should change the name.

sql query to count the rows

i have a table like this
enter image description here
this will return you number of records in a table:
select count(*) from tablename;
SELECT
base.*, aaa.the_count
FROM
TABLE base
INNER JOIN (
SELECT
GROUP,
count(*) AS the_count
FROM
TABLE
GROUP BY
1
) aaa ON aaa. GROUP = base. GROUP

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

SQL Select list of last records with condition

I have the following table
ID Name CodSituation
1 John 1
2 Mary 2
3 Mary 3
4 Mary 4
5 John 5
6 John 2
7 Mary 1
I want to select the Names, ID's and CodSituation for all users where their last entry is CodSituation=2
In these results I will get just the id 6 As Mary's last entry was CodeSituation=4
if more than one users have their latest CodSituation=2 I want them too.
[FINAL EDIT]
After seeing what was posted at the end of this answer I figured out that the user was asking the wrong question:
What they were asking was 'show me everyone who has CodSituation=2' when they meant 'Show me all the users who's last entry in CodSituation field=2'
Here is the correct query for that:
select a.ID, a.Name, a.CodSituation
from table_name a
inner join (
select Name, max(ID) as MaxID
from table_name
group by Name
) b on a.Name = b.Name and a.ID = b.MaxID
where a.CodSituation = 2;
Here is the fiddle for that: http://sqlfiddle.com/#!2/a731d
[END]
[here are the previous queries, for reference]
Looks to me like you just need:
select * from table_name where CodSituation=2
To get all of the people with situation 2
To get only the last entry using mysql:
select * from table_name where CodSituation=2 order by id desc limit 1
To get the last entry using sql-server:
select top 1 * from table_name where CodSituation=2 order by ID desc;
See a working example here:
http://sqlfiddle.com/#!6/022fb/4
[edit]
OP supplied an actual dataset:
select Name from table_name where CodSituation=2 group by Name;
This shows all the unique users with a CodSituation of 2 (with one entry per person)
http://sqlfiddle.com/#!3/be404/2
Can be achieved like this, but may not be the best way to do this
Basically what I am doing is Create a temporary table and add the Name and Maximum row number.
Which then match again that the maximum row number row is associated with CodSituation=2
Create Table #Temp2(Name Varchar(10),RowN int)
;WITH CTE AS (SELECT *,RN=ROW_NUMBER() OVER(PARTITION BY Name ORDER BY ID)
FROM TableName)
insert into #Temp2
SELECT Name,MAX(RN)
FROM CTE
Group By Name
select TT.*
from
(
SELECT *,RN=ROW_NUMBER() OVER(PARTITION BY Name ORDER BY ID)
FROM TableName
)TT
cross apply (
select Name
from #Temp2 TB
where TB.Name=TT.Name and TB.RowN= TT.RN
) Tab
Where CodSituation=2
Fiddle Sample
I found an easier way concatenating fieds and using max solve this problem... Thanks!
SELECT
right(
max(
right(('0000000' + CONVERT(varchar,id) + '-'+ convert(varchar,codsituation)),4)),10),
name
FROM TABLE_NAME
GROUP BY name
HAVING
right(
max(
right(
('0000000' + CONVERT(varchar,id) + '-'+ convert(varchar,codsituation))
,4)),1) = 2
http://sqlfiddle.com/#!3/3dc1c/10

How to order the query result by count() of field in postgresql without group by

I have simple database and I thing my answer should be so simple but I can't find my answer the db is look like:
-----------------
name | ip
-----------------
nick |192.168.1.10
john |192.168.1.1
john |192.168.1.2
john |192.168.1.3
lucy |192.168.10.1
lucy |192.168.10.2
I need a query that return all the rows but the result sorted by count(ip) per name
and the result of above list should be something like :
------------------
name |ip
------------------
nick |192.168.1.10
lucy |192.168.10.1
lucy |192.168.10.2
john |192.168.1.1
john |192.168.1.2
john |192.168.1.3
try this query:
SELECT t1.*
FROM table1 t1
INNER JOIN (SELECT Count(name) counter,
name
FROM table1
GROUP BY name)t2
ON t1.name = t2.name
ORDER BY counter;
SQL Fiddle
Here a SQL Fiddle
Try this statement.
SELECT * FROM tblSAMPLE ORDER BY name DESC,ip
SELECT NAME,IP,
COUNT(NAME) KEEP (DENSE_RANK LAST ORDER BY NAME)
OVER (PARTITION BY NAME) "Answer"
FROM PSQL
ORDER BY 3
Ans By
Subash M
Need to tell correct answer Y/N?
This should be faster than a self join on the table.
select name, ip
from (
select name,
ip,
count(*) over (partition by name) as cnt
from the_table
) t
order by cnt;