Select rows from mysql table with limit by group - mysql

I have the table
id code name
=====================
1 30100 John
2 30100 Andrew
3 30100 Sandy
4 29145 Mike
5 29145 Tony
6 29145 Laura
7 29145 Henry
8 00124 Michael
9 00124 Teddy
10 13405 Andy
11 09325 Patrick
I want to select only 2 names grouped by code and get this result.
id code name
=====================
1 30100 John
2 30100 Andrew
4 29145 Mike
5 29145 Tony
8 00124 Michael
9 00124 Teddy
10 13405 Andy
11 09325 Patrick
Can somebody help me to make such query/queries?
Thanks

SELECT id, code, name
FROM TableName a
WHERE
(
SELECT count(*)
FROM TableName as f
WHERE f.code = a.code and a.id >= f.id
) <= 2
ORDER BY id, code, name
SQLFiddle Demo

Related

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

I wanna hide id if NOT like but don't work SELECT * FROM character, character_actor WHERE character.id NOT LIKE character_actor.character_id;

I wanna hide id if NOT like but don't work
SELECT *
FROM character, character_actor WHERE character.id NOT LIKE character_actor.character_id;
id name id character_id actor_name
1 Doogie Howser 1 4 Alyson Hannigan
1 Doogie Howser 2 3 Alyson Hannigan
1 Doogie Howser 3 2 Neil Patrick Harris
2 Barney Stinson 1 4 Alyson Hannigan
2 Barney Stinson 2 3 Alyson Hannigan
2 Barney Stinson 4 1 Neil Patrick Harris
3 Lily Aldrin 1 4 Alyson Hannigan
3 Lily Aldrin 3 2 Neil Patrick Harris
3 Lily Aldrin 4 1 Neil Patrick Harris
4 Willow Rosenberg 2 3 Alyson Hannigan
4 Willow Rosenberg 3 2 Neil Patrick Harris
4 Willow Rosenberg 4 1 Neil Patrick Harris
You want a NOT IN query:
SELECT *
FROM character WHERE character.id NOT IN (SELECT character_actor.character_id from character_actor)
It can also be accomplished with a NOT EXISTS. Depending on the DBMS and structure, this might lead to better performance.
SELECT *
FROM character c WHERE NOT EXISTS(SELECT NULL from character_actor ca WHERE ca.character_id=c.id)
Try this query
SELECT *
FROM character as c JOIN character_actor as ca
ON c.character_id != ca.character_id
for example I have two tables , I wanna get two_table.name without one_table.name
one_table
id name
1 Dave
2 Mary
3 Fry male
4 Leela
5 Odie
two_table
id name
1 Dave
2 Mary
3 Fry male
4 Leela
5 Odie
6 Jumpy
7 Sneakers
8 Jack
9 Malia
10 Lee

In SQL select and count all entries where with same value in column

I have a table like in an mysql-database.
id section number name
1 A 1234 fred
2 B 5678 mo
3 B 1234 fred
4 C 8901 lou
5 A 8901 lou
6 A 2345 lee
7 B 2345 lee
8 C 2345 lee
9 A 6789 paul
10 B 1234 fred
This is my table and i need to get all numbers is in more then one section with the name of the section. like this
id section number name
1 A 1234 fred
3 B 1234 fred
Fred is in section A and B.
In my solution i get this
id section number count name
1 A 1234 1 fred
3 B 1234 2 fred
6 A 2345 1 lee
7 B 2345 1 lee
8 C 2345 1 lee
2 B 5678 1 mo
9 A 6789 1 paul
4 C 8901 1 lou
5 A 8901 1 lou
in my way i try is sql select
SELECT id, section, number, count(number) AS count, name
FROM table
GROUP BY section, number
ORDER BY number
Is is the right way? How can i improve that for mor number columns, if have 3 numbercolumns
id section number1 number2 number3 name
Now i do this with 3 sql-statements

ROW COUNT in MYSQL

In mysql, I got table "a" as follow
ID USERNAME SUBJECT_ID
1 ALAN 1
2 ALAN 2
3 ALAN 3
4 ALAN 4
5 ALAN 5
6 JOHN 6
7 ALAN 7
8 JOHN 8
9 ALAN 9
I just want to know username = ALAN takes how many subject and how generate a new column to C become
ID USERNAME SUBJECT_ID C
1 ALAN 1 1
2 ALAN 2 2
3 ALAN 3 3
4 ALAN 4 4
5 ALAN 5 5
7 ALAN 7 6
9 ALAN 9 7
The column is group by USERNAME and generate the row count.How should I query and generate C field?
The query I have tried, but I don't know how to generate "C"
SELECT ID,USERNAME,SUBJECT_ID,C FROM a WHERE USERNAME = "ALAN" GROUP BY SUBJECT_ID
Please help, thanks.
You can use Row number in mysql, this way:
SELECT T1.*,#rownum := #rownum+1 as C
FROM TableName T1,(SELECT #rownum := 0) r
WHERE T1.Username LIKE '%ALAN%'
Result:
ID USERNAME SUBJECT_ID C
-----------------------------
1 ALAN 1 1
2 ALAN 2 2
3 ALAN 3 3
4 ALAN 4 4
5 ALAN 5 5
7 ALAN 7 6
9 ALAN 9 7
Example in SQL Fiddle.

mysql join get info from 2 rows

i have 2 tables:
players
id | dni | name
-----------------
1 222 mike
2 333 gerard
3 444 mark
4 555 alfred
5 666 thomas
5 777 nicolas
teams
id | dni1 | dni2 | cat
------------------------
1 222 333 1
2 444 555 1
3 666 333 2
4 777 222 2
what i want is to make a select statement depending on the cat. so for
cat=1 should be:
mike gerard
mark alfred
and for cat=2 should be:
thomas gerard
nicolas mike
i tryed many join statements but i cant figure it out, can you help me please?
EDIT to make it more difficult, the players table have a new colum= points
players
id | dni | name | points
--------------------------
1 222 mike 1
2 333 gerard 2
3 444 mark 3
4 555 alfred 4
5 666 thomas 5
5 777 nicolas 6
now the result give us the sum of the points colums from both players and order by biggest sum number.
cat=1 should be:
mark alfred 7
mike gerard 3
select t.cat, p1.name name1, p2.name name2, p1.points + p2.points points
from teams AS t
join players AS p1 ON p1.dni = t.dni1
join players AS p2 on p2.dni = t.dni2
where cat = 1
order by points desc
DEMO