In SQL how can I repeat Row_Number value after a count of 3? - mysql

The table has 1000 rows. And I want to add a new column with the Row_number that repeats after a count of 3
INPUT TABLE
ID
Name
CountryCode
District
Population
1
Kabul
Afg
Kabol
178000
2
Qandahar
Afg
Qandahar
237500
3
Herat
Afg
Herat
186800
4
Mazar-e-Sharif
Afg
Balkh
127800
5
Amsterdam
Nld
Noord-Holland
731200
6
Rotterdam
Nld
Zuid-Holland
593321
7
Haag
Nld
Zuid-Holland
440900
8
Utrecht
Nld
Utrecht
234323
9
Eindhoven
Nld
Noord-Brabant
201843
10
Tilburg
Nld
Noord-Brabant
193238
11
Groningen
Nld
Groningen
172701
OUTPUT TABLE
ID
Name
CountryCode
District
Population
Row_Number
1
Kabul
Afg
Kabol
178000
1
2
Qandahar
Afg
Qandahar
237500
2
3
Herat
Afg
Herat
186800
3
4
Mazar-e-Sharif
Afg
Balkh
127800
1
5
Amsterdam
Nld
Noord-Holland
731200
2
6
Rotterdam
Nld
Zuid-Holland
593321
3
7
Haag
Nld
Zuid-Holland
440900
1
8
Utrecht
Nld
Utrecht
234323
2
9
Eindhoven
Nld
Noord-Brabant
201843
3
10
Tilburg
Nld
Noord-Brabant
193238
1
11
Groningen
Nld
Groningen
172701
2

You should use ROW_NUMBER() to get a row number, you can the use various ways to get it to be 1,2,3 in a sequence, for instance use modulus
case when MOD(ROW_NUMBER(),3) = 0 then 3 else MOD(ROW_NUMBER(),3) end as row_number.

Related

mysql: how to select group by first character and top 5 by counter

my table look like following
id person counter
1 Ona 4946
2 Mayra 15077
3 Claire 496
4 Rita 13929
5 Demond 579
6 Winnifred 13580
7 Green 1734
8 Jacquelyn 19092
9 Aisha 5572
10 Kian 8826
11 Alexandrea 7514
12 Dalton 14151
13 Rossie 18403
14 Carson 19537
15 Mason 2022
16 Emie 2394
17 Jonatan 6655
18 June 5037
19 Jazmyn 10856
20 Mittie 18928
here is the fiddle
i would like to select the top 5 by counter and group by first character, here is the sql that i tried:
SELECT SUBSTR(person,1,1) AS Alpha, person, counter
FROM myTable
GROUP BY SUBSTR(person,1,1)
ORDER BY SUBSTR(person,1,1) ASC, counter DESC;
how to select desired result as following:
alpha person counter
a Arvid 9236
a Aisha 5572
a Alf 4000
a Ahmad 3500
a Alvin 2100
b Brandon 13000
b Ben 8230
b Bonny 7131
b Bella 4120
b Bun 1200
c Connie 9320
c Calvin 8310
c Camalia 6123
c Cimon 3419
c Clay 2515
im using mysql 8.0
You can do:
select *
from (
select *, row_number() over(partition by substr(person, 1, 1)
order by counter desc) as rn
from myTable
) x
where rn <= 5
order by substr(person, 1, 1), rn
Result:
id person counter rn
---- ---------- -------- --
153 Alf 19758 1
283 Alycia 19706 2
260 Abe 19463 3
223 Assunta 18808 4
300 Ari 18031 5
210 Bennie 18309 1
159 Barry 18281 2
128 Beulah 18080 3
314 Benny 16795 4
474 Barry 15789 5
342 Casandra 19656 1
14 Carson 19537 2
67 Chaim 19429 3
280 Colin 18507 4
500 Corbin 18433 5
380 Daphney 19138 1
234 Dejah 18781 2
241 Derrick 18722 3
49 Dasia 18562 4
312 Darrel 17903 5
163 Evalyn 19847 1
79 Ernestine 19523 2
344 Emilie 19520 3
371 Eva 19119 4
469 Emma 18403 5
140 Fiona 19522 1
216 Flo 18314 2
356 Frieda 16082 3
254 Floy 15942 4
54 Florencio 12739 5
447 Geoffrey 19858 1
327 Geoffrey 19223 2
335 Grant 19100 3
454 Giuseppe 16175 4
83 Gardner 15235 5
373 Hilario 19507 1
35 Hanna 19276 2
200 Halle 18150 3
491 Hailee 17521 4
411 Hermann 17018 5
21 Idella 7440 1
177 Izabella 5536 2
115 Isai 4164 3
412 Izabella 2112 4
275 Imani 573 5
195 Joannie 19374 1
8 Jacquelyn 19092 2
48 Jalon 18861 3
251 Jamie 18768 4
367 Joanny 17600 5
282 Kendra 19278 1
421 Kendra 19213 2
363 Kaylin 18977 3
96 Kaylie 18423 4
310 Katrine 17754 5
146 Lonzo 19778 1
194 Leonora 18258 2
399 Laurine 16847 3
137 Leslie 16718 4
190 Luther 16318 5
87 Maegan 19112 1
20 Mittie 18928 2
271 Mariana 18149 3
317 Mary 18043 4
305 Maybelle 17666 5
281 Noelia 19203 1
176 Nickolas 19047 2
408 Nelson 15901 3
142 Nasir 13700 4
366 Nicole 10694 5
423 Ova 19759 1
487 Osborne 19539 2
438 Ozella 18911 3
375 Ora 18270 4
414 Onie 17358 5
52 Pascale 19658 1
39 Pearlie 17621 2
364 Price 14177 3
161 Precious 10337 4
294 Paula 9162 5
70 Quincy 18343 1
73 Quincy 16631 2
192 Quentin 13578 3
131 Rodger 19776 1
231 Royal 19033 2
313 Rocky 19008 3
13 Rossie 18403 4
45 Rosanna 15992 5
418 Sydnee 19810 1
470 Sadie 19189 2
123 Shanna 18862 3
485 Savanah 18664 4
302 Steve 16412 5
406 Toney 18283 1
28 Tremaine 16400 2
98 Taurean 15911 3
278 Tremaine 14391 4
311 Treva 14026 5
239 Ubaldo 11630 1
78 Valentina 17736 1
458 Vita 17527 2
170 Vergie 16971 3
158 Vance 15089 4
272 Veronica 12027 5
102 Willis 18155 1
329 Ward 14919 2
156 Westley 14867 3
136 Winnifred 14315 4
6 Winnifred 13580 5
323 Yolanda 17920 1
155 Yesenia 6164 2
402 Zachary 19129 1
37 Zaria 5398 2
See running example at DB Fiddle.

how can I make this sql join request

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);

Only show the highest amount on an INNER JOIN

We have an interactive Map of America which shows the state which equals your requirement.
We have an Problem with our Database, we have two tables staaten and economydata.
Our inner join
SELECT staaten.staat_id, staat_name, economydata.INDUSTRY_ID, economydata.INDUSTRIE_UMSATZ_ANTEIL
FROM staaten, economydata
WHERE staaten.staat_id = economydata.staat_i;`
gives us this output:
staat_id staat_name INDUSTRY_ID INDUSTRIE_UMSATZ_ANTEIL
1 Alabama 3 1
1 Alabama 6 1
1 Alabama 10 3
1 Alabama 11 4
1 Alabama 12 18
1 Alabama 13 11
1 Alabama 25 7
1 Alabama 34 6
1 Alabama 35 7
1 Alabama 36 3
1 Alabama 45 2
1 Alabama 51 5
1 Alabama 56 11
1 Alabama 60 6
1 Alabama 64 1
1 Alabama 65 3
1 Alabama 69 1
1 Alabama 70 7
1 Alabama 75 0
1 Alabama 78 3
1 Alabama 81 3
1 Alabama 82 17
2 Alaska 3 2
2 Alaska 6 16
2 Alaska 10 2
2 Alaska 11 4
2 Alaska 12 3
2 Alaska 13 0
2 Alaska 25 3
2 Alaska 34 2
2 Alaska 35 4
2 Alaska 36 12
2 Alaska 45 3
2 Alaska 51 3
2 Alaska 56 9
2 Alaska 60 4
2 Alaska 64 1
2 Alaska 65 2
2 Alaska 69 0
2 Alaska 70 7
2 Alaska 75 1
2 Alaska 78 3
2 Alaska 81 2
2 Alaska 82 21
But we only need one row per Staate which only contains the Highest economydata.INDUSTRIE_UMSATZ_ANTEIL with the INDUSTRY_ID.
The closest we got was with SELECT ... MAX(economydata.INDUSTRIE_UMSATZ_ANTEIL) FROM ... GROUP BY staaten.staat_name but we lost the connection between economydata.INDUSTRIE_UMSATZ_ANTEIL and INDUSTRY_ID, it showed us the Highest INDUSTRIE_UMSATZ_ANTEIL but not the right INDUSTRY_ID
You could use a tuple and a in clause on the result for max(INDUSTRIE_UMSATZ_ANTEIL) group by staat_id
SELECT
staaten.staat_id
, staat_name
, economydata.INDUSTRY_ID
, economydata.INDUSTRIE_UMSATZ_ANTEIL
FROM staaten
INNER JOIN economydata on staaten.staat_id = economydata.staat_i
where (staaten.staat_id, economydata.INDUSTRIE_UMSATZ_ANTEIL ) in (
SELECT
staaten.staat_id
, max(economydata.INDUSTRIE_UMSATZ_ANTEIL)
FROM staaten
INNER JOIN economydata on staaten.staat_id = economydata.staat_i
group by staaten.staat_id
)
and i suggest the use of explicit join instead that where clause ... the code is more clear

MySql Count the same value

I have this table. My task is this : Count the students having the same first name.
I want it to show me just the firstname and then how many times firstname is found:
like this: bob | 2
id firstN lastN group grade tax gender year university
9 Alyson Hanniga 112 1 0 female 1 UTC
5 Barney Stinson 111 4 0 male 1 UBB
8 Bob Saget 111 6 900 male 3 UBB
14 Bob Bob 112 10 250 male 1 UBB
11 Cobie Smulder 111 9 200 female 1 Dragos Voda
7 David Henrie 112 9 0 male 1 Dragos Voda
12 Jason Segel 112 10 150 male 1 UTC
13 Josh Radnor 112 7 0 male 2 Dragos Voda
4 Lily Aldrin 112 5 400 female 1 UBB
6 Lyndsy Fonseca 113 10 0 female 3 UTC
2 Marshal Eriksen 112 10 100 male 2 UTC
10 Neil Patrick 112 7 0 male 3 Dragos Voda
1 Robin Scherba 111 10 0 female 1 UBB
3 Ted Mosby 112 8 0 male 2 UBB
Here is query for
select FirstName,count(*) from tblStudent group by FirstName

Query to find the running sum of a column(qty) based on type(if 'a' add else subtract)

I have table with data as follows:
Store ItemNo Type Billno Qty
London 1 A 1 10
London 1 A 2 5
London 1 S 1 7
London 1 A 3 5
London 1 S 2 7
London 2 A 1 19
London 2 S 2 5
London 2 A 3 11
Paris 1 A 1 15
Paris 1 S 2 8
Paris 1 A 3 9
Paris 2 A 1 10
Paris 2 S 2 5
Now i want to calculate TotalQty, such that totalqty of qty of an itemno under particular store is calculated based on type. i.e. if type is A, qty should be added to total and if it is S , subtracted from total as shown below. In the example below for store london ,itemno 1 row 3 is the last entry,so the totalQty gives the current quantity availble for that item in that particular store.
Store ItemNo Type BillNo Qty TotalQty
London 1 A 1 10 10
London 1 A 2 5 15
London 1 S 1 7 8
London 1 A 3 5 13
London 1 S 2 7 6
London 2 A 1 19 19
London 2 A 2 5 24
London 2 S 3 11 13
Paris 1 A 1 15 15
Paris 1 S 2 8 7
Paris 1 A 3 9 16
Paris 2 A 1 10 10
Paris 2 S 2 5 5
Assuming I understand you right, you need to use the IIf statement:
SELECT Store, ItemNo, SUM(IIF(Type = 'A', Qty, 0 - Qty)) AS TotalQty
FROM MyTable
GROUP BY Store, ItemNo
should produce the following (untested):
Store ItemNo TotalQty
London 1 8
London 2 13
Paris 1 16
Paris 2 5
I think this is it (I was confused but then spotted your data is inconsistent between table and results):
SELECT S1.Store, S1.ItemNo, S1.Type, S1.Billno, S1.Qty,
(
SELECT SUM(SWITCH(
S2.Type = 'A', S2.Qty,
S2.Type = 'S', 0 - S2.Qty,
TRUE, NULL
))
FROM StoreStuff AS S2
WHERE S2.Store = S1.Store
AND S2.ItemNo = S1.ItemNo
AND S2.Billno <= S1.Billno
) AS TotalQty
FROM StoreStuff AS S1
ORDER
BY S1.Store, S1.ItemNo, S1.Billno;