table structure is like :
student area yearlevel code year sem result
123010 INFO 9 0002 2015 1 77
123011 INFO 9 0002 2015 1 70
123012 INFO 9 0002 2015 1 55
123037 INFO 9 0002 2016 2 49
123037 INFO 9 0002 2017 1 NULL
123010 COMP 9 0007 2016 1 82
123010 ISYS 9 0026 2015 2 82
123011 ISYS 9 0026 2015 2 88
123012 ISYS 9 0026 2015 2 66
123010 COMP 9 0038 2016 2 77
123010 COMP 9 0041 2016 1 45
123010 COMP 9 0041 2017 1 NULL
123010 ISYS 9 0049 2016 1 88
So student 101 has a repeated subject 000002
Use GROUP BY and HAVING function :
SELECT student,subject
FROM your_table
GROUP BY student,subject
HAVING COUNT (*) > 1
This should work:
select Student,subject from table group by student, subject having count(*)>1
Related
I have 3 tables, and a query:
SELECT
DISTINCT assistent.id as id,
name,
events.client as client,
assistentprice.id as priceid,
value
FROM
`assistents`
LEFT JOIN `events` ON assistents.id = events.assistent
LEFT JOIN `assistentprice` ON assistents.id = assistentprice.id_assistente
ORDER BY
name
I got a result like:
id
name
client
priceid
value
88
MARK
44
12
7.00
88
MARK
27
14
8.00
88
MARK
44
15
11.00
88
MARK
27
11
10.00
88
MARK
44
10
9.00
16
OSCAR
49
21
8.00
16
OSCAR
14
23
9.00
16
OSCAR
14
22
7.00
16
OSCAR
49
19
9.00
So, table is ordered by name, but i want to see also ordered/grouped client for every assistent. For exampe, for Mark it have to be:
id
name
client
priceid
value
88
MARK
27
12
7.00
88
MARK
27
14
8.00
88
MARK
44
15
11.00
88
MARK
44
11
10.00
How can i do this?
How can get this : the last name (nom), first name (prenom) and age of competitors that participated at all competitons. I have difficulties with count and join.
my user table :
id
nom
prenom
login
age
1
Wehner
Einar
kleinviola
79
2
Beer
Cierra
earnestinelebsa
71
3
Gina
Lucien
cassindagmar
97
4
Maybelle
Delphine
haleypredovic
91
5
Upton
Elwyn
sstreich
63
6
Irwin
Prof.
christopframi
25
7
Ernser
Clint
cesar65
83
8
Bechtelar
Sheila
sofiasawayn
77
9
Simonis
Remington
christafahey
35
10
Parisian
Octavia
swiftsage
89
11
Predovic
Rory
bartolettisabri
78
12
Will
Sven
price66
20
13
O'Hara
Zoey
tiffanywillms
96
14
McGlynn
Julie
gkoss
74
15
Walter
Maximus
amandajenkins
63
16
Hahn
Andrew
drutherford
77
17
Kunze
Elinore
ziemanntheron
95
18
Ursula
Evelyne
collierodessa
64
19
Klein
Kirsten
darrellrunolfss
96
20
Chester
Lucien
jamey55
24
21
Darron
Antoine
justina27
60
22
Boyer
Harvey
hesseljameson
45
23
Jade
Lucien
kpagac
29
24
Eliane
Delphine
delphahessel
75
25
Lang
Shanna
sophia73
23
26
Wilderman
Fredrick
shaina75
34
27
Daniel
Emie
alene73
86
28
Daniel
Rhoda
foster22
63
29
Trantow
Tommie
boconner
40
30
Kerluke
Adolf
vstanton
74
31
Sehoubo
David
davidshbo
20
32
dfglskdsklj
dfvdvf
dfgdfg
0
my competitors table :
id_competitor
id_concours
1
1
2
1
3
1
4
1
5
1
6
1
7
1
8
1
31
1
9
2
10
2
11
2
12
2
13
2
14
2
15
2
16
2
17
2
18
2
31
2
1
3
2
3
3
3
4
3
5
3
19
3
20
3
31
3
2
4
4
4
6
4
8
4
10
4
12
4
14
4
16
4
18
4
20
4
1
5
3
5
5
5
7
5
9
5
11
5
13
5
15
5
17
5
19
5
my competitons table:
id
date_debut
date_fin
descriptif
theme
etat
1
2019-01-01 00:00:00
2019-03-01 00:00:00
Le premier concours de la plateforme
Les zinzins de l'espace
4
2
2018-01-01 00:00:00
2018-02-01 00:00:00
Le deuxième concours de la plateforme
Outils
4
3
2020-04-01 00:00:00
2020-05-01 00:00:00
Le troisième concours de la plateforme
Voiture sur autoroute
2
4
2018-07-01 00:00:00
2018-08-11 00:00:00
Le quatrième concours de la plateforme
Naruto Uzumaki
3
5
2018-10-01 00:00:00
2018-11-01 00:00:00
Le cinquième concours de la plateforme
Le grand peuple au dessus de la mer
4
This should return the name, first name and age of all users that participated in ALL competitions:
SELECT nom, prenom, age
FROM user
WHERE (SELECT count(DISTINCT id_concours) FROM competitors WHERE id_competitor = user.id) = (SELECT count(*) FROM competitons);
let's say we have records in a table and we would like to be able to order (and reorder them).
the table could look like something like this, see below
id Pos Level parentId
Europe 18 1 0 null
Germany 9 2 1 18
Berlin 2 3 2 9
Frankfurt 20 4 2 9
Stuttgart 23 5 2 9
France 29 6 1 18
Paris 26 7 2 29
Lyon 13 8 2 29
Americas 11 9 0 null
USA 27 10 1 11
New York 22 11 2 27
Manhattan 19 12 3 22
Brooklyn 7 13 3 22
Los Angeles 25 14 2 27
Mexico 6 15 1 11
Canada 4 16 1 11
Montreal 21 17 2 4
Vancouver 3 18 2 4
Asia 8 19 0 null
China 14 20 1 8
Beijing 17 21 2 14
Shenzhen 30 22 2 14
Shanghai 28 23 2 14
Japan 16 24 1 8
Tokyo 1 25 2 16
Shinjuku 15 26 3 1
Oceania 24 27 0 null
Autralia 5 28 1 24
Sydney 10 29 2 5
Africa
12 30 0 null
where id is a unique id (can be anything), position the position of the element in the list, level depth level and parentId parent id (if exists)
Typically I would want the following method:
/**
#param sourceId: id of the element to be moved
#params targetId: id of the element which position needs to be overtaken
#param aboveOrBelow: defines whether the old element (target) will be placed above or below the source element
#return if successful, new position of the source element, if unsuccessful: message explaining why unsuccessful
*/
def move(sourceId: Long, targetId: Long, aboveOrBelow: Boolean = true):Either[Long, String]
what's the most efficient way to implement this or am I missing something? Is there already a built-in mechanism for such operations in (My)SQL?
constraints:
- end user who might be allowed to reorder do not necessarily see all records (e.g. only asian records)
- records can be added and deleted
=== edit ===
i rewrote the structure taking in account suggestions in the comments:
id pos parentId
Europe 18 1 null
Germany 9 1 18
Berlin 2 1 9
Frankfurt 20 2 9
Stuttgart 23 3 9
France 29 2 18
Paris 26 1 29
Lyon 13 2 29
Americas 11 2 null
USA 27 1 11
New York 22 1 27
Manhattan 19 1 22
Brooklyn 7 2 22
Los Angeles 25 2 27
Mexico 6 3 11
Canada 4 4 11
Montreal 21 1 4
Vancouver 3 2 4
Asia 8 3 null
China 14 1 8
Beijing 17 1 14
Shenzhen 30 2 14
Shanghai 28 3 14
Japan 16 2 8
Tokyo 1 1 16
Shinjuku 15 1 1
Oceania 24 4 null
Autralia 5 1 24
Sydney 10 1 5
Africa 12 5 null
And for constructing the tree structure i would use recursive cte as follows. and construct it as a view
with recursive cte(place_name,id,parent_id,level)
as (select place_name,id,parent_id,1 as level
from countries_hierarchy
where parent_id is null
union all
select concat(lpad(' ',a.level+1,' ')
,b.place_name
)
,b.id
,b.parent_id
,a.level+1
from cte a
join countries_hierarchy b
on a.id=b.parent_id
)
select * from cte
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=334820e4e01cf8749c5abcaa447963a0
So, this is the challenge:
I have two tables:
Etalon:
+-----+-----+-----+-----+----+
| e1 | e2 | e3 | e4 | e5 |
+-----+-----+-----+-----+----+
| 01 | 02 | 03 | 04 | 05 |
+-----+-----+-----+-----+----+
And Candidates:
+-----+----+-----+-----+-----+----+----+
| ID | c1 | c2 | c3 | c4 | c5 | nn |
+-----+----+-----+-----+-----+----+----+
| 00 | 03 | 08 | 02 | 01 | 06 | ** |
+-----+----+-----+-----+-----+----+----+
| 01 | 05 | 04 | 03 | 02 | 01 | ** |
+-----+----+-----+-----+-----+----+----+
| 02 | 06 | 07 | 08 | 09 | 10 | ** |
+-----+----+-----+-----+-----+----+----+
| 03 | 08 | 06 | 09 | 02 | 07 | ** |
+-----+----+-----+-----+-----+----+----+
What request should I use, to find and save (in nn column) the number of matches between two rows (e1, e2, e3, e4, e5 and c1, c2, c3, c4, c5) for each row in table candidate?
Should be the next result:
Candidates:
|-----|----|-----|-----|-----|-----|----|
| ID | c1 | c2 | c3 | c4 | c5 | nn |
|-----|----|-----|-----|-----|-----|----|
| 00 | 03 | 08 | 02 | 01 | 06 | 03 |
|-----|----|-----|-----|-----|-----|----|
| 01 | 05 | 04 | 03 | 02 | 01 | 05 |
|-----|----|-----|-----|-----|-----|----|
| 02 | 06 | 07 | 08 | 09 | 10 | 00 |
|-----|----|-----|-----|-----|-----|----|
| 03 | 08 | 06 | 09 | 02 | 07 | 01 |
|-----|----|-----|-----|-----|-----|----|
The result for nn is:
0 - no matches
1,2,3,4,5 - numbers of matches
How can I achieve that?
The objective is to establish a maximal partial matching between the master row and each row of the client table without regard to the respective column identities.
The idea is to abstract away from the column ids by representing the column contents in another way. As you indicated that the value domain is {1, ..., 10}, one may choose the first 10 prime numbers {p_1, ...,p_10} = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 }, mapping i to p_i. The comparisons will be based on the product of the mapped column values. This approach exploits the uniqueness of prime factorization, ie. every positive integer factorizes into a unique multi-set of prime numbers.
A one-pass standalone sql update statement is rather cumbersome to write down, therefore we create a temporary table that contains the products of the mapped values:
CREATE TEMPORARY TABLE t_pp (
id NUMBER
, mp_candidates NUMBER
, mp_etalon NUMBER
, nn NUMBER
);
INSERT INTO t_pp ( id, mp_candidates, mp_etalon )
SELECT id
, CASE c1
WHEN 1 THEN 2
WHEN 2 THEN 3
WHEN 3 THEN 5
WHEN 4 THEN 7
WHEN 5 THEN 11
WHEN 6 THEN 13
WHEN 7 THEN 17
WHEN 8 THEN 19
WHEN 9 THEN 23
WHEN 10 THEN 29
ELSE 31
END
* CASE c2 WHEN 2 THEN 3 WHEN 3 THEN 5 WHEN 4 THEN 7 WHEN 5 THEN 11 WHEN 6 THEN 13 WHEN 7 THEN 17 WHEN 8 THEN 19 WHEN 9 THEN 23 WHEN 10 THEN 29 ELSE 31 END
* CASE c3 WHEN 2 THEN 3 WHEN 3 THEN 5 WHEN 4 THEN 7 WHEN 5 THEN 11 WHEN 6 THEN 13 WHEN 7 THEN 17 WHEN 8 THEN 19 WHEN 9 THEN 23 WHEN 10 THEN 29 ELSE 31 END
* CASE c4 WHEN 2 THEN 3 WHEN 3 THEN 5 WHEN 4 THEN 7 WHEN 5 THEN 11 WHEN 6 THEN 13 WHEN 7 THEN 17 WHEN 8 THEN 19 WHEN 9 THEN 23 WHEN 10 THEN 29 ELSE 31 END
* CASE c5 WHEN 2 THEN 3 WHEN 3 THEN 5 WHEN 4 THEN 7 WHEN 5 THEN 11 WHEN 6 THEN 13 WHEN 7 THEN 17 WHEN 8 THEN 19 WHEN 9 THEN 23 WHEN 10 THEN 29 ELSE 31 END
mp_candidates
, CASE e1
WHEN 1 THEN 2
WHEN 2 THEN 3
WHEN 3 THEN 5
WHEN 4 THEN 7
WHEN 5 THEN 11
WHEN 6 THEN 13
WHEN 7 THEN 17
WHEN 8 THEN 19
WHEN 9 THEN 23
WHEN 10 THEN 29
ELSE 31
END
* CASE e2 WHEN 2 THEN 3 WHEN 3 THEN 5 WHEN 4 THEN 7 WHEN 5 THEN 11 WHEN 6 THEN 13 WHEN 7 THEN 17 WHEN 8 THEN 19 WHEN 9 THEN 23 WHEN 10 THEN 29 ELSE 31 END
* CASE e3 WHEN 2 THEN 3 WHEN 3 THEN 5 WHEN 4 THEN 7 WHEN 5 THEN 11 WHEN 6 THEN 13 WHEN 7 THEN 17 WHEN 8 THEN 19 WHEN 9 THEN 23 WHEN 10 THEN 29 ELSE 31 END
* CASE e4 WHEN 2 THEN 3 WHEN 3 THEN 5 WHEN 4 THEN 7 WHEN 5 THEN 11 WHEN 6 THEN 13 WHEN 7 THEN 17 WHEN 8 THEN 19 WHEN 9 THEN 23 WHEN 10 THEN 29 ELSE 31 END
* CASE e5 WHEN 2 THEN 3 WHEN 3 THEN 5 WHEN 4 THEN 7 WHEN 5 THEN 11 WHEN 6 THEN 13 WHEN 7 THEN 17 WHEN 8 THEN 19 WHEN 9 THEN 23 WHEN 10 THEN 29 ELSE 31 END
mp_etalon
, 0 nn
FROM candidates
CROSS JOIN etalon
;
Now for pass #2 - counting matches:
UPDATE t_pp
SET nn =
CASE WHEN mp_candidates MOD 2 = 0 AND mp_etalon MOD 2 = 0 THEN 1 ELSE 0 END
+ CASE WHEN mp_candidates MOD 3 = 0 AND mp_etalon MOD 3 = 0 THEN 1 ELSE 0 END
+ CASE WHEN mp_candidates MOD 5 = 0 AND mp_etalon MOD 5 = 0 THEN 1 ELSE 0 END
+ CASE WHEN mp_candidates MOD 7 = 0 AND mp_etalon MOD 7 = 0 THEN 1 ELSE 0 END
+ CASE WHEN mp_candidates MOD 11 = 0 AND mp_etalon MOD 11 = 0 THEN 1 ELSE 0 END
+ CASE WHEN mp_candidates MOD 13 = 0 AND mp_etalon MOD 13 = 0 THEN 1 ELSE 0 END
+ CASE WHEN mp_candidates MOD 17 = 0 AND mp_etalon MOD 17 = 0 THEN 1 ELSE 0 END
+ CASE WHEN mp_candidates MOD 19 = 0 AND mp_etalon MOD 19 = 0 THEN 1 ELSE 0 END
+ CASE WHEN mp_candidates MOD 23 = 0 AND mp_etalon MOD 23 = 0 THEN 1 ELSE 0 END
+ CASE WHEN mp_candidates MOD 29 = 0 AND mp_etalon MOD 29 = 0 THEN 1 ELSE 0 END
;
Finally, transferring the results to the original table and cleaning up:
UPDATE candidates c
set nn = ( SELECT p.nn FROM t_pp p WHERE p.id = c.id )
;
DELETE TEMPORARY TABLE t_pp;
Some more notes:
The scheme as shown assumes that cell values are unique within each row. However, it can easily be extended to allow formultiple occurrences of values.
In principle, this can be wrapped in a single sql statement - for obvious reasons this is not recommended.
Rdbms other than mysql follow the sql standard and provide the WITH clause that obviates the need for a temporaray table.
The value 31 in the ELSE branch of the above CASE expressions is a dummy value.
I have a data set:
Player Year Points
John 2010 60
Jane 2010 83
Stan 2010 42
Brian 2010 51
Phil 2010 65
John 2009 1
Jane 2009 44
Stan 2009 89
Brian 2009 4
Phil 2009 82
John 2008 93
Jane 2008 12
Stan 2008 31
Brian 2008 41
Phil 2008 63
John 2007 47
Jane 2007 73
Stan 2007 72
Brian 2007 17
Phil 2007 81
I am looking to get the points and years ranked by PERSON, not overall. In other words:
Player Year Points Personal Rank
John 2010 60 2
Jane 2010 83 1
Stan 2010 42 3
Brian 2010 51 1
Phil 2010 65 3
John 2009 1 4
Jane 2009 44 3
Stan 2009 89 1
Brian 2009 4 4
Phil 2009 82 1
John 2008 93 1
Jane 2008 12 4
Stan 2008 31 4
Brian 2008 41 2
Phil 2008 63 4
John 2007 47 3
Jane 2007 73 2
Stan 2007 72 2
Brian 2007 17 3
Phil 2007 81 2
Is this possible to do in Excel?
I also have this data in MySql if anyone knows (or can point me in the right direction) a SELECT for it.
Thanks in advance.
set #player = '',#num=0;
select id,player,year,points,rank from
(select *,
#num := if(#player = player, #num + 1, 1) as rank,
#player := player as p
from results order by player,points desc) as t
order by id
In mysql you could also use something like this:
SET #rank=0;
SELECT #rank:=#rank+1 AS rank, person, year, points FROM yourtable ORDER BY id person;