how to normalize the table? - mysql

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
...

Related

query to overwrite the record in duplicate instances

|student_id |subject |marks|
|
|1001 english 75|
|1001 maths 80|
|1001 science 70|
|1001 english 90|
|1001 maths 95|
|1001 science 85|
required output:
student_id English Maths Science
1001 75 80 70
1002 90 95 85
so how to get the result as expected am new to sql. Thanks in advance and note that the records are having 1001 as ids intentionally

SQL Query to return count of Author, Customer for list of Books

trying to segregate the Authors and Customer count with respective to book
select Book, count(Author) as Author, count(Customer) as Customer
from Books
group by Book
order by Book Asc;
but, resulting same values for customer and Author
SNo
Book
Author
Customer
1
A Study in Scarlet
Arthur Conan Doyle
Perry mason
8
Careless Kitten
Erle Stanley Gardner
Agatha Christie
20
Careless Kitten
Erle Stanley Gardner
Sherlock Holms
16
Gitanjali
Rabindranath Tagore
Mark Twain
21
Gitanjali
Rabindranath Tagore
Perry Mason
22
Gitanjali
Rabindranath Tagore
Miss Marple
23
Gitanjali
Rabindranath Tagore
Perry Mason
3
Gora
Rabindranath Tagore
Miss Marple
10
Gora
Rabindranath Tagore
Miss Marple
4
Murder on the Orient Exp
Agatha Christie
Miss Marple
5
Murder on the Orient Exp
Agatha Christie
Mark Twain
13
Peril at End House
Agatha Christie
Arthur Conan Doyle
17
Peril at End House
Agatha Christie
Mark Twain
18
The Adventures of Huckle
Mark Twain
Arthur Conan Doyle
6
The Adventures of Tom
Mark Twain
Agatha Christie
14
The Adventures of Tom
Mark Twain
Agatha Christie
9
The Gilded Age
Mark Twain
Perry Mason
11
The Gilded Age
Mark Twain
Perry Mason
11
The Gilded Age
Mark Twain
Perry Mason
12
The Gilded Age
Mark Twain
Perry Mason
15
The Gilded Age
Mark Twain
Rabindranath Tagore
2
The Murder of Roger Ack
Agatha Christie
Erle Stanley Gardner
7
The Murder of Roger Ack
Agatha Christie
Arthur Conan Doyle
19
The Sign of Four
Arthur Conan Doyle
Harry Potter
The COUNT function in your query return quantity of not NULL values in columns Author and Customer. Because all records are not NULL you got equal values.
For calculate count of different values COUNT(DISTINCT <column>) should be used.
select
Book,
count(distinct Author) as AuthorsCnt,
count(distinct Customer) as CustomersCnt
from Books
group by Book
order by Book Asc;
Check SQL here

how to change this sql query to sub-query

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

How to create MultiIndex from joined MySQL tables?

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

Concatenating values in mysql where multiple rows in one table relate to one row in another table without displaying several rows

I've have gone through stackoverflow for days and googled a lot but can still find the solution.
I have 2 tables profile and qualification
in the table profile
pro_id surname firstname
-------- -------- ------------
1 John James
2 King Fred
3 Paul Smith
4 Cindy Hayes
Qua_id Degree School Year
------ ------ ------ -----
1 MBA Wharton university 2002
1 LLB Yale University 2001
2 BSc Covington University 1998
2 BEd Kellog University 1995
2 Msc MIT 2011
3 MBA Havard Business School 2002
3 MSc Yale University 2012
4 BSc University of Edinburgh 2010
4 BA University of Liverpool 2009
Now what i want to achieve is this
1 John James MBA Wharton university 2002, LLB Yale University 2001
2 King Fred BSc Covington University 1998, BEd Kellog University 1995, Msc MIT 2011
3 Paul Smith MBA Havard Business School 2002, MSc Yale University 2012
4 Cindy Hayes BSc University of Edinburgh 2010, BA University of Liverpool 2009
But what i currently have is :
1 John James MBA Wharton university 2002
1 John James LLB Yale University 2001
2 King Fred BSc Covington University 1998 BEd
2 King Fred Kellog University 1995
2 King Fred Msc MIT 2011
3 Paul Smith MBA Havard Business School 2002
3 Paul Smith MSc Yale University 2012
4 Cindy Hayes BSc University of Edinburgh 2010
4 Cindy Hayes BA University of Liverpool 2009
Here is my code
Select pro_id, surname, firstname concat(degree,school,year) as qual from profile,qualification Where profile.proid=qualification.qua_id
Select pro_id, surname, firstname, group_concat(degree,school,year) as qual
from profile
left join qualification
on qualification.qua_id = profile.pro_id
group by qua_id
your filed names look a little odd - qua_id should perhaps be profile_id (which I think shoudl really be user_id - ie they should both be a user id and foreign keys to a user table).