SQL: Repeated records by grouping some columns - mysql

I have a data like,
ID Name ItemA ItemB ItemC
OXZ234 Adam 4 4 5
OXZ234 Adam 1 2 3
OXZ345 Tarzen 6 7 8
OXDER2 William 9 8 2
OXDER2 William 0 8 0
I need to find how much of food each person eats. For example by referring first two records I can say, Adam of ID OXZ234 ate ItemA-5, ItemB-6 and ItemC-8. But for small amount of data this kind of manual calculation is affordable. I have a million data records like this. So initially I need to find the records which is having same ID and name but only items count differing.
I have tried the query to find duplicate records by grouping all columns like below,
select ID,Name,ItemA,ItemB,ItemC, COUNT(*)
from DATA_REFRESH
group by ID,Name,ItemA,ItemB,ItemC
having COUNT(*) > 1
But Now I have to identify records having items columns differed.
So the expected output is like,
OXZ234 Adam 2
OXDER2 William 2
OXZ345 Tarzen 1
Any suggestion would be helpful!

You want SUM
select ID,
Name,
sum(ItemA) as ItA,
sum(ItemB) as ItB,
sum(ItemC) as ItC,
count(ID) as Occurrences -- Counts the number of entries per person
from DATA_REFRESH
group by ID,Name
having count(ID) >1 -- restricts this so only those with more than one entry appear

Hi, You can have a simple query without having clause,
select ID,Name,COUNT(*)
from DATA_REFRESH
group by ID,Name order by COUNT(*) desc ;

Simply try like this,
select ID,Name,COUNT(*)
from Sample_Check
group by ID,Name
having COUNT(*) > 1

Related

How to get exact rows count of particular column in MySQL table

I want to get exact row count of specified column
Example: Table
Name Id Age
_______________________________
Jon 1 30
Merry 2 40
William 50
David
There are 4 rows in table but i want to count ID column.
I am using below query to achieve it
select count(Id) from table;
But its returning 4 and I know why it is returning 4 but I want output as 2 because there are only two rows in Id column.
How can i achieve it?
Try this:
select count(Id) from table where id>0;
with the help of #blabla_bingo and #Edwin Dijk finally i have achieved it by below query
select count(Id) from table where Id!="";

How to find only id that has different values in MySQL?

I have a table with two columns:
id
num
1
2
2
8
1
7
7
3
I want to get as an answer to my query only ids that have more than 1 nums.
For example in my table I would want to get as a result:
id
1
How should I express my query?
Thanks in advance.
You might need something like this:
SELECT id
FROM your_table_name
GROUP BY id
HAVING count(DISTINCT num) > 1;
Google 'Aggregate functions'. Here the aggregate function is count() and it works always coupled with a GROUP BY clause. Pretty fun.

Count and sum up all duplicate records in MySQL

I have table with, following structure.
id name
1 john
2 ana
3 john
4 ana
5 peter
6 ana
7 Abrar
8 Raju
Duplicate entries in the table are as follows
john(2) duplicate
ana(3) duplicate
The names which are duplicates are john and ana.
My question is how would I count the records in total which are duplicate here it is '5' records
Note : I also followed the similar question in community but it explains how we can add the number of duplicates exists for that particular name in the table and adds up the third column in table representing the duplicates records with same name but in my case I wanted to know the number of all duplicates exist in the table (here the result of the query is just number "5") irrespective of the names.
Just take a count subquery on the query you already have in mind (or perhaps have already written):
SELECT SUM(cnt) AS total_duplicates
FROM
(
SELECT COUNT(*) AS cnt
FROM yourTable
GROUP BY name
HAVING COUNT(*) > 1
) t;
Demo

ORDER BY and GROUP BY those results in a single query

I am trying to query a dataset from a single table, which contains quiz answers/entries from multiple users. I want to pull out the highest scoring entry from each individual user.
My data looks like the following:
ID TP_ID quiz_id name num_questions correct incorrect percent created_at
1 10154312970149546 1 Joe 3 2 1 67 2015-09-20 22:47:10
2 10154312970149546 1 Joe 3 3 0 100 2015-09-21 20:15:20
3 125564674465289 1 Test User 3 1 2 33 2015-09-23 08:07:18
4 10153627558393996 1 Bob 3 3 0 100 2015-09-23 11:27:02
My query looks like the following:
SELECT * FROM `entries`
WHERE `TP_ID` IN('10153627558393996', '10154312970149546')
GROUP BY `TP_ID`
ORDER BY `correct` DESC
In my mind, what that should do is get the two users from the IN clause, order them by the number of correct answers and then group them together, so I should be left with the 2 highest scores from those two users.
In reality it's giving me two results, but the one from Joe gives me the lower of the two values (2), with Bob first with a score of 3. Swapping to ASC ordering keeps the scores the same but places Joe first.
So, how could I achieve what I need?
You're after the groupwise maximum, which can be obtained by joining the grouped results back to the table:
SELECT * FROM entries NATURAL JOIN (
SELECT TP_ID, MAX(correct) correct
FROM entries
WHERE TP_ID IN ('10153627558393996', '10154312970149546')
GROUP BY TP_ID
) t
Of course, if a user has multiple records with the maximal score, it will return all of them; should you only want some subset, you'll need to express the logic for determining which.
MySql is quite lax when it comes to group-by-clauses - but as a rule of thumb you should try to follow the rule that other DBMSs enforce:
In a group-by-query each column should either be part of the group-by-clause or contain a column-function.
For your query I would suggest:
SELECT `TP_ID`,`name`,max(`correct`) FROM `entries`
WHERE `TP_ID` IN('10153627558393996', '10154312970149546')
GROUP BY `TP_ID`,`name`
Since your table seems quite denormalized the group by name-par could be omitted, but it might be necessary in other cases.
ORDER BY is only used to specify in which order the results are returned but does nothing about what results are returned - so you need to apply the max()-function to get the highest number of right answers.

Retrieve unique data from MYSQL database

I have a table in my database which contains 5 rows. I am trying to write an sql statement that will retrieve all rows which only have 1 agency assigned to them.
case_id agency_ID
1 4
2 4
3 3
4 2
4 4
To clarify I would like to select the required rows (and any further rows) but only if the case_id is unique. Any rows with duplicates would be ommited.
I have tried to use DISTINCT(case_id), COUNT(*) to count all rows but it doesn't work and it's slowly sapping away my soul. It is probably an easy fix, but for the life of me I just can't see it.
Hope this is enough information to go on. Any help would be greatly appreciated.
SELECT * FROM your_table GROUP BY case_id HAVING COUNT(agency_ID) = 1
You can try
SELECT case_id,agency_ID,COUNT(case_id) as c
FROM yourTable
GROUP BY case_id
HAVING (c=1)