A list showing each product requested on each client stock request. Show client name, product number and quantity requested. Sorted by client name and then product number.
I tried this:
SELECT STOCK_REQUEST.requestNum, STOCK_REQUEST.clientNum, CLIENT.clientName, REQUEST_LIST.productNum, REQUEST_LIST.qtyRequested
FROM STOCK_REQUEST, CLIENT, REQUEST_LIST
WHERE CLIENT.clientNum = STOCK_REQUEST.clientNum
AND REQUEST_LIST.requestNum = STOCK_REQUEST.requestNum
ORDER BY CLIENT.clientName AND REQUEST_LIST.productNum
I get sth like this:
requestNum clientNum clientName productNum qtyRequested
2 2 [->] David Liu 4 674
3 2 [->] David Liu 5 66
1 1 [->] Ian Peng 2 45
5 4 [->] James Cameron 3 809
4 3 [->] Mark Moris 1 164
But the requirement says we have to use sub-query, so I try to change it to:
SELECT CLIENT.clientName, REQUEST_LIST.productNum, REQUEST_LIST.qtyRequested
FROM CLIENT, REQUEST_LIST
WHERE CLIENT.clientNum IN (
SELECT clientNum
FROM STOCK_REQUEST)
AND REQUEST_LIST.requestNum IN (
SELECT requestNum
FROM STOCK_REQUEST)
ORDER BY CLIENT.clientName AND REQUEST_LIST.productNum
But the result is not what I expect:
clientName productNum qtyRequested
David Liu 4 674
James Cameron 3 809
Mark Moris 5 66
David Liu 2 45
James Cameron 1 164
Mark Moris 4 674
James Cameron 5 66
Mark Moris 2 45
Ian Peng 3 809
James Cameron 4 674
Ian Peng 1 164
James Cameron 2 45
David Liu 3 809
Ian Peng 5 66
David Liu 1 164
Ian Peng 4 674
Mark Moris 3 809
David Liu 5 66
Ian Peng 2 45
Mark Moris 1 164
Related
I have a table like this
create table users(
user_id int not null auto_increment,
user_age date,
user_address varchar(255),
primary key(user_id)
)
user_id
user_age
user_address
1
2010-01-05
87 Polk St. Suite 5
2
2010-01-06
Carrera 52 con Ave. Bolivar #65-98 Liano Largo
3
2010-01-07
Ave. 5 de Mayo Porlamar
4
2010-01-08
89 Chiaroscuro Rd.
5
2010-01-09
Via Ludovico il Moro 22
6
2010-01-10
Rue JosephBens 532
7
2011-01-05
43 rue St. Laurent
8
2011-01-06
Heerstr. 22
9
2011-01-07
South House 300 Queensbridge
10
2011-01-08
Ing. Gustavo Moncaa 8585 Piso 20-A
11
2011-01-09
Obere Str. 57
12
2011-01-10
Avda. de la Constitución 2222
13
2012-01-05
Mataderos 2312
14
2012-01-06
120 Hanover Sq.
15
2012-01-07
Berguvsvägen 8
16
2012-01-08
Forsterstr. 57
And I'd like my table to be like this:
user_id
user_age
user_address
1
2010-01-05
87 Polk St. Suite 5
2
2010-01-06
Carrera 52 con Ave. Bolivar #65-98 Liano Largo
7
2011-01-05
43 rue St. Laurent
8
2011-01-06
Heerstr. 22
13
2012-01-05
Mataderos 2312
14
2012-01-06
120 Hanover Sq.
How can I make this happen with group by statement?
If your MySQL supports windows function try:
with cte as
(
SELECT *, ROW_NUMBER() OVER(partition by year(user_age) ORDER BY user_age) row_num
FROM users
)
select user_id,user_age,user_address
from cte
where row_num<=2;
Demo
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.
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
I’ve been trying to wrangle some data from a group of MySQL tables into a Pandas DataFrame with a MultiIndex. The tables are roughly like this:
create table team (
teamID integer NOT NULL,
teamName varchar(64) NOT NULL,
primary key (teamID));
create table coach (
coachID integer NOT NULL,
teamID integer NOT NULL,
coachName varchar(64) NOT NULL,
primary key (coachID));
create table player (
playerID integer NOT NULL,
teamID integer NOT NULL,
playerName varchar(64) NOT NULL,
primary key (playerID));
Each team can have one or more coaches and one or more players.
Here’s the SELECT and MERGE:
import mysql.connector
connection = mysql.connector.connect(user='root', passwd='temp', database='mydb')
team = sql.read_frame('select * from team;', connection)
coach = sql.read_frame('select * from coach;', connection)
player = sql.read_frame('select * from player;', connection)
connection.close()
df = pd.merge(
pd.merge(team, coach, on='teamID'),
player, on='teamID')
The DataFrame now looks like this:
In [2]: df
Out[2]:
teamID teamName coachID coachName playerID playerName
0 1 Red 1 Rachel Evans 1 Carol Lee
1 1 Red 1 Rachel Evans 2 Abigail O'Neil
2 1 Red 1 Rachel Evans 3 Becky Hood
3 1 Red 1 Rachel Evans 4 Bridget Sawyer
4 1 Red 2 Gladys Nenn 1 Carol Lee
5 1 Red 2 Gladys Nenn 2 Abigail O'Neil
6 1 Red 2 Gladys Nenn 3 Becky Hood
7 1 Red 2 Gladys Nenn 4 Bridget Sawyer
8 2 Green 3 Reina Stevens 5 Amy Reid
9 2 Green 3 Reina Stevens 6 Angie Costa
10 2 Green 3 Reina Stevens 7 Annie Reese
11 2 Green 3 Reina Stevens 8 Barbara Lo
12 2 Green 4 Jill Hunt 5 Amy Reid
13 2 Green 4 Jill Hunt 6 Angie Costa
14 2 Green 4 Jill Hunt 7 Annie Reese
15 2 Green 4 Jill Hunt 8 Barbara Lo
16 3 Blue 5 Lynn Peters 9 Alicia Green
17 3 Blue 5 Lynn Peters 10 Beth Spire
18 3 Blue 5 Lynn Peters 11 Candace Pierce
19 3 Blue 5 Lynn Peters 12 Carmen Jones
20 3 Blue 6 Stephanie Lenter 9 Alicia Green
21 3 Blue 6 Stephanie Lenter 10 Beth Spire
22 3 Blue 6 Stephanie Lenter 11 Candace Pierce
23 3 Blue 6 Stephanie Lenter 12 Carmen Jones
And now I would like to create a MultiIndex that shapes this data so that it looks something like this:
In [2]: df
Out[2]:
teamID teamName coachID coachName playerID playerName
1 Red 1 Rachel Evans 1 Carol Lee
2 Gladys Nenn 2 Abigail O'Neil
3 Becky Hood
4 Bridget Sawyer
2 Green 3 Reina Stevens 5 Amy Reid
4 Jill Hunt 6 Angie Costa
7 Annie Reese
8 Barbara Lo
I’ve been able to do this with straight Python, but I want to be able to take advantage of Pandas powerful and concise indexing features.
Adding the following
df.set_index(['teamID', 'teamName', 'coachID', 'coachName', 'playerID'], inplace=True)
makes the first four columns hierarchical. But the last two columns are still duplicated:
playerName
teamID teamName coachID coachName playerID
1 Red 1 Rachel Evans 1 Carol Lee
2 Abigail O'Neil
3 Becky Hood
4 Bridget Sawyer
2 Gladys Nenn 1 Carol Lee
2 Abigail O'Neil
3 Becky Hood
4 Bridget Sawyer
2 Green 3 Reina Stevens 5 Amy Reid
6 Angie Costa
7 Annie Reese
8 Barbara Lo
4 Jill Hunt 5 Amy Reid
6 Angie Costa
7 Annie Reese
8 Barbara Lo
Can someone show me the easiest way to normalize this table? I'm having trouble figuring out how to group relationships.
Film# Title Dir# DirectorName Actor# ActorName Role Timeon
F1100 Happy Day D101 Jim Alan A1000 Judy Foster Amy Jones 50
A1001 Robert Dinero Harry 30
A1004 Harrison Ford Jack Actress 55
A1009 Eddy Murphy Excel Fuse 65
A1000 Judy Foster Amy Jones 15
F1101 AirForceOne D105 Woody Ellen A1000 Judy Foster Amy Jones 50
A1004 Harrison Ford Bill Ford 55
A1009 Eddy Murphy Police one 35
A1002 Mary Jackson Police two 45
F1102 RunAwayBride D101 Jim Alan A1003 Julia Roberts Amy Jones 50
A1007 David Lynch Sam Strickland 45
A1004 Harrison Ford Robber 35
A1005 John Wayne Cab Driver 25
A1000 Judy Foster News Reporter 65
You need two data tables, Film and Person, and two intersection tables, FilmDirector and FilmRole. An abbreviated example:
Film
filmID title
1100 Happy Day
1101 Airforce One
1102 Runaway Bride
Person
personID name
101 Jim Alan
105 Woody Ellen
1000 Judy Foster
1001 Robert Dinero
1002 Mary Jackson
1003 Julia Roberts
1004 Harrison Ford
1005 John Wayne
1007 David Lynch
1009 Eddy Murphy
FilmDirector
filmDirectorID filmID personID
1 1100 101
2 1101 105
3 1102 101
FilmRole
filmRoleID FilmID personID role timeOn
1 1100 1000 Amy Jones 50
2 1100 1001 Harry 30
3 1100 1004 Jack Actress 55
6 1101 1000 Amy Jones 50
7 1101 1004 Bill Ford 55
...