I need to count duplicates of two combined columns in MySql.
So for example data looks like this
Id, Name, Address
1, Hary, Street 1
2, Hary, Street 2
3, Hary, Street 1
So results would be
count, Name, Address
2 Hary, Street 1
1 Hary, Street 2
SELECT COUNT(*) AS Cnt, Name, Address
FROM <table>
GROUP BY Name, Address
Related
I have a table with the following values: Name, Street,I'd , Value, Date.
I need to combine Name, Street, Id and make 2 subgroups by date. I want to compare the value in row with the same name, street and id but different date. And write only the ones with different value
Example:
Mike, Street 1 , idtag , 5 , 11.5.2022
Mike, street 1 , idtag , 10 , 10.5.2022
I want to write the difference in value with the name, street, id combination.
All the solutions I have tried take way to long
dYou could use an aggregation approach here. Assuming that you want to flag any name, street, and ID combination which have 2 or more records on different dates, you may try:
SELECT name, street, ID, MAX(val) - MIN(val) AS diff
FROM yourTable
GROUP BY name, street, ID
HAVING MIN(date) <> MAX(date);
To use this logic for a specific pair of records, whose (unique) date values are known, use this version:
SELECT name, street, ID, MAX(val) - MIN(val) AS diff
FROM yourTable
GROUP BY name, street, ID
HAVING MIN(date) = '2022-05-10' AND MAX(date) = '2022-05-11';
I would like to get statistics on my dataset,
for example this is my dataset:
FirstName LastName Country City BirthMonth
Donald Trump England London Jan
Bill Gates England London Sep
Donald Suther England York Sep
Donald Suther Germany Berlin Jan
and this is my 'group_by' list:
[['FirstName', 'LastName'], ['Country', 'City'], ['BirthMonth']]
I would like to get the following statistics:
group by FirstName & LastName:
FirstName LastName Count
Donald Trump 1
Donald Suther 2
Bill Gates 1
group by Country & City:
Country City Count
England London 2
England York 1
Germany Berlin 1
group by BirthMonth:
BirthMonth Count
Jan 2
Sep 2
my query would look like this:
select FirstName, LastName, Country, City, BirthMonth
from my_table
where <some conditions to filter rows only from certain timestamp>
now I have two options:
a. return all values to server and process there (I'm using python) - this query includes many rows and causes overhead
b. query multiple times, each time grouping by the specific fields
select FirstName, LastName, count(*) as group_by
from my_table
where ...
group by FirstName, LastName
c. is there a third option of having one query, and return all different 'group_by's?
and another question:
from (a) and (b) which is better?
I'll note that the group by has limited options - for example 'FirstName' has only 20 options, this means that (b) will result each time less than 20*(num of group by in query rows) < 40, but (a) will result 20^5 rows which is tons of data
to simplify I'll assume there aren't more than 2 columns in a group_by each time,
(which is actually the current situation, it might grow in the future but currently I can use a solution that takes that into account)
You can combine each grouped query with the others using UNION. Fill in the unused columns in each subquery with NULL.
SELECT FirstName, LastName, NULL AS Country, NULL AS City, NULL AS BirthMonth, COUNT(*) AS count
FROM my_table
GROUP BY FirstName, LastName
UNION ALL
SELECT NULL, NULL, Country, City, NULL, COUNT(*)
FROM my_table
GROUP BY Country, City
UNION ALL
SELECT NULL, NULL, NULL, NULL, BirthMonth, COUNT(*)
FROM my_table
GROUP BY BirthMonth
Given the following table:
id, country, name, age
1, Italy, Burke, 21
2, Italy, Yefrem, 20
3, Spain, Valter, 30
4, Spain, Max, 11
How can i get one oldest citizen for each country
For example result should contain only rows 1 and 3
Result should be grouped by country and in each group entry with highest age should be returned
Use an inline view to filter the rows and then a join to the table itself to get all the columns back like so:
select t.id, t.country, t.name, t.age
from test t
join (
select max(age) as age, country
from test
group by country
) s
on t.age = s.age and t.country = s.country;
Tested here: http://sqlfiddle.com/#!2/2523ce/3
I have a table of flights, which have an origin and destination city, represented as a foreign id.
A very simplified example of this table looks like:
id | origin | destination
023 1 3
044 3 2
332 2 1
509 1 3
493 1 4
I need to get the first time that a city shows up as an origin or a destination; a list of all the flights that contain a city that hasn't been flown to or from yet.
What I would like to get for the above example would be:
023: 1, 3
044: 2
493: 4
Flights 332 and 509 aren't in the output because they only visit cities that have already been visited.
Here's what I've tried:
(SELECT distinct(origin), distinct(destination) FROM flights ORDER BY id)
Doesn't work because you can't select more than one distinct column
SELECT (distinct(origin) FROM flights ORDER BY id) UNION (distinct (destination) FROM flights ORDER BY id)
Doesn't work because of syntax errors, but mainly because it doesn't take into account that a city should be unique in the origin and destination columns.
If there's not a quick way to do this in SQL I'm also happy to just iterate through and keep track of cities that have been visited (this app has literally one user, and he doesn't care about a few milliseconds of computation because he's over 80), but I'd love to know just so that I can learn more about SQL!
This does it:
SELECT id, GROUP_CONCAT(city ORDER BY city) cities
FROM (
SELECT city, min(id) id
FROM (
SELECT origin city, MIN(id) id
FROM flights
GROUP BY city
UNION
SELECT destination city, MIN(id) id
FROM flights
GROUP BY city) u
GROUP BY city) x
GROUP BY id
DEMO
Table structure:
Locations: id, name, city
I want to select all the duplicates by name AND city, since multiple locations can exist with same name in each city.
select name, city
from locations
group by name, city
having count(*) >= 2