Only n records for every x field - mysql

Given this data set:
ID Name City Birthyear
1 Egon Spengler New York 1957
2 Mac Taylor New York 1955
3 Sarah Connor New York 1959
4 Jean-Luc Picard La Barre 2305
5 Ellen Ripley Nostromo 2092
6 James T. Kirk Nostromo 2233
7 Henry Jones La Barre 1899
How can I get only 2 records from every city?

Try this:
SELECT i1.*
FROM your_table i1
LEFT JOIN your_table i2
ON (i1.City = i2.City AND i1.ID > i2.ID)
GROUP BY i1.ID
HAVING COUNT(*) < 2
ORDER BY city, ID;

Related

How to filter users that have multiple rows where row1="value1" and row2="condition2", ... in SQL?

A have the following data
id user_id visited_country
1 12 Spain
2 12 France
3 14 England
4 14 France
5 16 Canada
6 14 Spain
7 16 Mexico
I want to select all users who have visited both Spain and France, excluding those who have visited England. How can I do that in MySQL?
One aggregation approach might be:
SELECT user_id
FROM yourTable
GROUP BY user_id
HAVING SUM(visited_country = 'Spain') > 0 AND
SUM(visited_country = 'France') > 0 AND
SUM(visited_country = 'England') = 0;

Why COUNT in the SQL HAVING condition doesn't calculate correct value?

When i solve this HackerRank problem
i found that my solution with condition
COUNT(total) = 1 doesn't work:
SELECT H.hacker_id AS hacker_id, H.name AS name, COUNT(C.challenge_id) AS total
FROM Hackers H
JOIN Challenges C ON H.hacker_id = C.hacker_id
GROUP BY H.hacker_id, H.name
HAVING
COUNT(total) = 1 OR -- HERE
OR
total = (
SELECT MAX(amount) FROM (
SELECT COUNT(challenge_id) AS amount FROM Challenges
GROUP BY hacker_id
) t
)
ORDER BY total DESC, hacker_id;
Output:
5120 Julia 50
18425 Anna 50
20023 Brian 50
33625 Jason 50
41805 Benjamin 50
52462 Nicholas 50
64036 Craig 50
69471 Michelle 50
77173 Mildred 50
94278 Dennis 50
96009 Russell 50
96716 Emily 50
2689 Joe 1
3432 Linda 1
5827 Kelly 1
6011 Robin 1
7537 David 1
7961 Michelle 1
9642 Joseph 1
9901 Lawrence 1
10975 Christine 1
11715 Louise 1
13075 John 1
13905 Evelyn 1
14307 David 1
14726 Emily 1
15109 Dorothy 1
17282 Norma 1
17311 Andrew 1
17663 Benjamin 1
17846 Alan 1
18709 James 1
19032 Andrew 1
19781 Jesse 1
19962 Patricia 1
20190 Aaron 1
21350 Bobby 1
21821 Carol 1
21916 Clarence 1
22396 Wayne 1
22455 Carolyn 1
23812 Jerry 1
24047 Elizabeth 1
25684 Alan 1
25990 Mildred 1
26802 Paul 1
27797 Helen 1
28766 Paul 1
29242 Jennifer 1
29841 Charles 1
30677 Keith 1
30778 Jose 1
30908 Helen 1
31770 Ashley 1
32364 Julia 1
32735 Cheryl 1
33273 Sara 1
33489 Denise 1
37092 Jennifer 1
37764 Jimmy 1
38540 Katherine 1
42467 Ernest 1
43240 Diana 1
43398 Steve 1
43595 Adam 1
45685 Bobby 1
46144 Sharon 1
46468 Timothy 1
46604 Christina 1
47156 Kelly 1
47921 Lillian 1
50560 Brian 1
52350 Teresa 1
53451 Jeffrey 1
53597 Rose 1
53768 Douglas 1
54015 Carolyn 1
54510 Paula 1
55415 Amy 1
56039 Teresa 1
56103 Kelly 1
56338 Jose 1
57195 Beverly 1
57873 Diana 1
58086 Debra 1
58167 David 1
58543 Rachel 1
59871 Martin 1
59895 Martha 1
60177 Brian 1
61093 Mark 1
61102 Kenneth 1
61206 Lillian 1
61769 Marie 1
63263 Dorothy 1
63684 Randy 1
63730 Sarah 1
63803 Carolyn 1
63961 Anna 1
64341 Virginia 1
64882 Roy 1
68178 Gloria 1
70499 Dennis 1
72321 Julie 1
72763 Julie 1
73267 Jeremy 1
73676 Linda 1
74320 Pamela 1
78615 Kathryn 1
79612 Tina 1
81652 Albert 1
83308 Roy 1
84739 Alan 1
84938 Judy 1
85094 Matthew 1
86142 Douglas 1
87040 Craig 1
87885 Gregory 1
88069 Jean 1
88083 Anna 1
88084 Alan 1
88858 Bruce 1
89514 Jeffrey 1
89903 Katherine 1
90276 Joyce 1
90369 Christina 1
91620 Debra 1
92239 Shirley 1
92920 Louis 1
94337 Lillian 1
94676 Patrick 1
94746 Adam 1
96521 Christine 1
96773 Angela 1
97338 Amy 1
98785 Rose 1
99101 Timothy 1
99165 Nancy 1
But this works:
SELECT H.hacker_id AS hacker_id, H.name AS name, COUNT(C.challenge_id) AS total
FROM Hackers H
JOIN Challenges C ON H.hacker_id = C.hacker_id
GROUP BY H.hacker_id, H.name
HAVING
--
total IN
(SELECT t0.total
FROM
(SELECT count(*) AS total
FROM challenges
GROUP BY hacker_id) t0
GROUP BY t0.total
HAVING count(t0.total) = 1)
-- instead of 'COUNT(total) = 1'
OR
total = (
SELECT MAX(amount) FROM (
SELECT COUNT(challenge_id) AS amount FROM Challenges
GROUP BY hacker_id
) t
)
ORDER BY total DESC, hacker_id;
Output:
5120 Julia 50
18425 Anna 50
20023 Brian 50
33625 Jason 50
41805 Benjamin 50
52462 Nicholas 50
64036 Craig 50
69471 Michelle 50
77173 Mildred 50
94278 Dennis 50
96009 Russell 50
96716 Emily 50
72866 Eugene 42
37068 Patrick 41
12766 Jacqueline 40
86280 Beverly 37
19835 Joyce 36
38316 Walter 35
29483 Jeffrey 34
23428 Arthur 33
95437 George 32
46963 Barbara 31
87524 Norma 30
84085 Johnny 29
39582 Maria 28
65843 Thomas 27
5443 Paul 26
52965 Bobby 25
77105 Diana 24
33787 Susan 23
45855 Clarence 22
33177 Jane 21
7302 Victor 20
54461 Janet 19
42277 Sara 18
99388 Mary 16
31426 Carlos 15
95010 Victor 14
27071 Gerald 10
90267 Edward 9
72609 Bobby 8
So, why i can't use just this
COUNT(total) = 1
instead of this large condition:
total IN
(SELECT t0.total
FROM
(SELECT count(*) AS total
FROM challenges
GROUP BY hacker_id) t0
GROUP BY t0.total
HAVING count(t0.total) = 1)
Because in your context total is just an alias to a aggregate result, not a value. In general you must repeat the COUNT(C.challenge_id) part but it will not help you here as COUNT(COUNT(C.challenge_id)) = 1 is clear wrong.
There are solutions to it like putting the aggregation dataset result in a temp table/table variable or using a CTE, that's is why a subquery works.
Disclaimer: don't chequed your query for correctness but that IN sounds ugly and using a CTE can possible be a better approach.
Below an example solution using MS SQL, sorry, don't get a MySql running right now nut don't used CTE =)
create table dbo.HACKER
(
hacker_id int,
name varchar(100)
)
GO
create table dbo.CHALLENGE
(
challenge_id int,
hacker_id int
)
GO
insert into dbo.HACKER
(hacker_id,name)
values
(5077,'Rose')
,(21283,'Angela')
,(62743,'Frank')
,(88255,'Patrick')
,(96196,'Lisa')
insert into dbo.CHALLENGE
(challenge_id, hacker_id)
values
(61654,5077)
,(58302,21283)
GO
--drop table #Temp
--drop table #totalsToExclude
select hk.hacker_id, hk.name, x.total
into #Temp
from dbo.HACKER hk
join (select ch.hacker_id, count(*) as total from dbo.CHALLENGE ch group by ch.hacker_id) as x
on x.hacker_id = hk.hacker_id
select * from #Temp
declare #maxTotal as int =(select max(total) from #temp)
select #maxTotal
select t.total, count(*) as [Count_total]
into #totalsToExclude
from #temp t
group by t.total
having(count(*) >1)
delete tx from #totalsToExclude tx where tx.total = #maxTotal
select * from #totalsToExclude
select * from #Temp t
where t.total not in (select t.total from #totalsToExclude)
order by t.total desc, t.hacker_id

How to count number of occurrence in a filed table

if I have a table with a field City like this:
City
------
London
Los Angeles
London
Athens
Rome
Paris
Paris
How could I get this result?
City | CityNumb
-------------------
London 2
Athens 1
Los Angeles 1
Paris 2
Rome 1
select City, count(*) as CityNumb
from your_table
group by City
Above image shows the SQL query and results for above task

SQL Inner Join statement not giving desired results

Hopefully someone can give me a hand here. I have the following two tables:
Table: locations
location_id user_id city state
1 1 Los Angeles CA
2 1 New York NY
3 1 Chicago IL
4 2 Dallas TX
5 3 Denver CO
6 4 Miami FL
7 5 Atlanta GA
Table: events
event_id user_id event_name event_date
1 1 My Event 1 2017-02-01
2 2 My Event 2 2017-03-01
3 3 My Event 3 2017-04-01
4 4 My Event 4 2017-05-01
5 5 My Event 5 2017-06-01
I am running the following query:
SELECT e.event_id, e.user_id, e.event_name, e.event_date,
l.user_id, l.city, l.state
FROM events e
INNER JOIN locations l
ON e.user_id = l.user_id
ORDER BY e.event_date ASC
I am trying just to get JUST the records in the events table, but also pull the corresponding city and state that match the user_id that both tables have in common. The output should be:
event_id user_id event_name event_date city state
1 1 My Event 1 2017-02-01 Los Angeles CA
2 2 My Event 2 2017-03-01 Dallas TX
3 3 My Event 3 2017-04-01 Denver CO
4 4 My Event 4 2017-05-01 Miami FL
5 5 My Event 5 2017-06-01 Atlanta GA
Can anyone point me to my error in the SQL statement?
You never gave us the logic for deciding which location to choose for a given user. One approach would be to take the minimum location_id associated with a given user:
SELECT t1.*,
COALESCE(t2.city, 'NA'),
COALESCE(t2.state, 'NA')
FROM events t1
LEFT JOIN locations t2
ON t1.user_id = t2.user_id
INNER JOIN
(
SELECT user_id, MIN(location_id) AS min_location_id
FROM locations
GROUP BY user_id
) t3
ON t2.user_id = t3.user_id AND
t2.location_id = t3.min_location_id
That's quite impossible, here the issue is for user with id 1 you have 3 tuples in the locations table , which city to select is the big question.
1 1 Los Angeles CA
2 1 New York NY
3 1 Chicago IL

How do I include a custom count in a MySQL SELECT result set?

I have the following two tables:
TABLE: area
*City_ID *Number Name
-------- ------- ----
SUR 1 Fleetwood
SUR 2 Whalley
SUR 3 Guildford
SUR 4 Newton
SUR 5 Cloverdale
SUR 6 South Surrey
ABB 1 Abbotsford East
ABB 2 Abbotsford West
ABB 3 Aberdeen
ABB 4 Bradner
ABB 5 Central Abbotsford
ABB 6 Matsqui
ABB 7 Poplar
ABB 8 Sumas Mountain
ABB 9 Sumas Prairie
TABLE: city
*ID Name
--- ----
SUR Surrey
ABB Abbotsford
LAN Langley
Using the following statement:
SELECT DISTINCT area.City_ID, city.Name
FROM area
INNER JOIN city
WHERE area.City_ID = city.ID
I get:
SELECT:
City_ID city.Name
------- ---------
SUR Surrey
ABB Abbotsford
But how do I SELECT the following:
SELECT:
City_ID city.Name area_COUNT
------- --------- ----------
SUR Surrey 6
ABB Abbotsford 9
where area_COUNT is the number of rows in area for each corresponding City_ID?
Use GROUP BY instead of DISTINCT:
SELECT city.City_ID, city.Name, COUNT(*)
FROM area
INNER JOIN city
WHERE area.City_ID = city.ID
GROUP BY city.ID
You should also add Name in Group by clause
SELECT area.City_ID, city.Name, count(*) as area_COUNT
FROM area
INNER JOIN city
WHERE area.City_ID = city.ID
GROUP BY area.City_ID,City.Name
add group by at the end
SELECT area.City_ID, city.Name, count(*) as area_COUNT
FROM area
INNER JOIN city
WHERE area.City_ID = city.ID
GROUP BY area.City_ID
Use GROUP BY instead of DISTINCT:
SELECT city.City_ID, city.Name, COUNT(*)
FROM area
INNER JOIN city
WHERE area.City_ID = city.ID
GROUP BY city.ID