From adffde2ea9eee64a79a2782f39f379db4e3c9cf0 Mon Sep 17 00:00:00 2001 From: Johnny Ilmo Koo Date: Wed, 20 May 2020 12:47:39 +0900 Subject: [PATCH 1/4] Complete Assignment part I --- module1-introduction-to-sql/README.md | 104 +++++++++++++++++++++++++- 1 file changed, 100 insertions(+), 4 deletions(-) diff --git a/module1-introduction-to-sql/README.md b/module1-introduction-to-sql/README.md index 40497956..d3f100b0 100644 --- a/module1-introduction-to-sql/README.md +++ b/module1-introduction-to-sql/README.md @@ -51,14 +51,110 @@ randomized, the numeric and boolean fields were left as defaults. Use `sqlite3` to load and write queries to explore the data, and answer the following questions: -- How many total Characters are there? -- How many of each specific subclass? -- How many total Items? -- How many of the Items are weapons? How many are not? +- How many total Characters are there? 302 chracters +- How many of each specific subclass? not sure what the question exactly means. i'd say 5 because there are cleric, fighter, mage, necromancer and thief. +- How many total Items? 174 items +- How many of the Items are weapons? How many are not? 37 items are weapon items and 137 items are not weapons - How many Items does each character have? (Return first 20 rows) + +| character_name | charactoer id | num items | +| --- | --- | --- | +|Aliquid iste optio reiciendi |1|3| +|Optio dolorem ex a| 2| 3| +|Minus c |3| 2| +|Sit ut repr |4| 4| +|At id recusandae expl| 5| 4| +|Non nobis et of| 6| 1| +|Perferendis |7| 5| +|Accusantium amet quidem eve| 8| 3| +|Sed nostrum inventore error m |9| 4| +|Harum repellendus omnis od |10| 4| +|Itaque ut commodi, |11| 3| +|Molestiae quis |12| 3| +|Ali| 13 |4| +|Tempora quod optio possimus il |14| 4| +|Sed itaque beatae pari |15 |4| +|Quam dolor |16| 1| +|Molestias expedita |17| 5| +|Lauda |18| 5| +|Incidunt sint perferen |19| 3| +|Laboriosa |20| 1| + +```sql +select cc.name as character_name, cc.character_id, count(cci.id) as item_num from charactercreator_character cc +inner join charactercreator_character_inventory cci +on cci.character_id = cc.character_id +group by cc.character_id +LIMIT 20; +``` - How many Weapons does each character have? (Return first 20 rows) + +| character_name | character_id | weapon_num | +|--------------------------------|--------------|------------| +| At id recusandae expl | 5 | 2 | +| Perferendis | 7 | 1 | +| Itaque ut commodi, | 11 | 1 | +| Laboriosa | 20 | 1 | +| Dolorum nam reic | 22 | 1 | +| Repellat ad numquam volu | 23 | 1 | +| Doloribus neque | 26 | 1 | +| Ab voluptas se | 27 | 3 | +| In pariatur corpori | 29 | 2 | +| Possimus ad dignissimos vel, a | 30 | 1 | +| Ad necess | 32 | 1 | +| Voluptates sunt voluptas volu | 34 | 1 | +| Autem mollitia fuga lauda | 35 | 2 | +| Sint quibusdam ob | 36 | 3 | +| Rerum et o | 37 | 2 | +| Doloribus dolore r | 38 | 2 | +| Eaque su | 39 | 2 | +| Vel molestias numqua | 40 | 1 | +| Iste assumenda repellat q | 41 | 1 | +| Quod tempora | 47 | 1 | + +```sql +select cc.name as character_name, cc.character_id, count(cci.id) as weapon_num +from charactercreator_character cc +inner join charactercreator_character_inventory cci +on cci.character_id = cc.character_id +inner join armory_item ai +on ai.item_id = cci.item_id +inner join armory_weapon aw +on aw.item_ptr_id = ai.item_id +group by cc.character_id +LIMIT 20; +``` + - On average, how many Items does each Character have? + +```sql +select avg(item_num) from + ( + select cc.name as character_name, cc.character_id, count(cci.id) as item_num + from charactercreator_character cc + inner join charactercreator_character_inventory cci + on cci.character_id = cc.character_id + group by cc.character_id + LIMIT 20 + ); +``` +avg 3.3 items + - On average, how many Weapons does each character have? +avg 1.5 weapons + +```sql +select cc.name as character_name, cc.character_id, count(cci.id) as weapon_num +from charactercreator_character cc +inner join charactercreator_character_inventory cci +on cci.character_id = cc.character_id +inner join armory_item ai +on ai.item_id = cci.item_id +inner join armory_weapon aw +on aw.item_ptr_id = ai.item_id +group by cc.character_id +LIMIT 20 +``` You do not need all the tables - in particular, the `account_*`, `auth_*`, `django_*`, and `socialaccount_*` tables are for the application and do not have From 75401f29cb9a099ce9dc0a38f6ab58ff2298242b Mon Sep 17 00:00:00 2001 From: Johnny Ilmo Koo Date: Wed, 20 May 2020 12:49:22 +0900 Subject: [PATCH 2/4] Fix sql query at the end --- module1-introduction-to-sql/README.md | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/module1-introduction-to-sql/README.md b/module1-introduction-to-sql/README.md index d3f100b0..ead3e52e 100644 --- a/module1-introduction-to-sql/README.md +++ b/module1-introduction-to-sql/README.md @@ -141,20 +141,24 @@ select avg(item_num) from avg 3.3 items - On average, how many Weapons does each character have? -avg 1.5 weapons + ```sql -select cc.name as character_name, cc.character_id, count(cci.id) as weapon_num -from charactercreator_character cc -inner join charactercreator_character_inventory cci -on cci.character_id = cc.character_id -inner join armory_item ai -on ai.item_id = cci.item_id -inner join armory_weapon aw -on aw.item_ptr_id = ai.item_id -group by cc.character_id -LIMIT 20 +select avg(weapon_num) from + ( + select cc.name as character_name, cc.character_id, count(cci.id) as weapon_num + from charactercreator_character cc + inner join charactercreator_character_inventory cci + on cci.character_id = cc.character_id + inner join armory_item ai + on ai.item_id = cci.item_id + inner join armory_weapon aw + on aw.item_ptr_id = ai.item_id + group by cc.character_id + LIMIT 20 + ); ``` +avg 1.5 weapons You do not need all the tables - in particular, the `account_*`, `auth_*`, `django_*`, and `socialaccount_*` tables are for the application and do not have From 1c0a317954cf08239a9a28cdb44050bd4c40ee45 Mon Sep 17 00:00:00 2001 From: johnnykoo84 Date: Thu, 21 May 2020 11:38:22 +0900 Subject: [PATCH 3/4] Complete part 1 --- .../buddymove_holiday.py | 0 module1-introduction-to-sql/rpg_queries.py | 76 +++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 module1-introduction-to-sql/buddymove_holiday.py create mode 100644 module1-introduction-to-sql/rpg_queries.py diff --git a/module1-introduction-to-sql/buddymove_holiday.py b/module1-introduction-to-sql/buddymove_holiday.py new file mode 100644 index 00000000..e69de29b diff --git a/module1-introduction-to-sql/rpg_queries.py b/module1-introduction-to-sql/rpg_queries.py new file mode 100644 index 00000000..68df640a --- /dev/null +++ b/module1-introduction-to-sql/rpg_queries.py @@ -0,0 +1,76 @@ +import sqlite3 +conn = sqlite3.connect('rpg_db.sqlite3') +curs = conn.cursor() + +query = 'SELECT COUNT(*) FROM armory_item;' +curs.execute(query) +result = curs.execute(query).fetchall() +print(result) + + +# 1. How many total Chracters are there? +query_1 = 'SELECT COUNT(*) FROM charactercreator_character;' +ans_1 = curs.execute(query_1).fetchone() +print(f'How many total Characters are there? {ans_1}') + +# 2. How many of each specfic subclass? +query_21 = 'SELECT COUNT(*) FROM charactercreator_cleric;' +query_22 = 'SELECT COUNT(*) FROM charactercreator_fighter;' +query_23 = 'SELECT COUNT(*) FROM charactercreator_mage;' +query_24 = 'SELECT COUNT(*) FROM charactercreator_necromancer;' +query_25 = 'SELECT COUNT(*) FROM charactercreator_thief;' + +ans_21 = curs.execute(query_21).fetchone() +ans_22 = curs.execute(query_22).fetchone() +ans_23 = curs.execute(query_23).fetchone() +ans_24 = curs.execute(query_24).fetchone() +ans_25 = curs.execute(query_25).fetchone() + +print(f'How many of the subclass, cleric? {ans_21}') +print(f'How many of the subclass, fighter? {ans_22}') +print(f'How many of the subclass, mage? {ans_23}') +print(f'How many of the subclass, necromancer? {ans_24}') +print(f'How many of the subclass, thief? {ans_25}') + +# 3. How many total items? +query_3 = 'SELECT COUNT(*) FROM armory_item;' +ans_3 = curs.execute(query_3).fetchone() +print(f'How many total items? {ans_3}') + +# 4. How many of the items are weapons? How many are not? +query_41 = 'SELECT COUNT(*) FROM armory_item ai INNER JOIN armory_weapon aw on ai.item_id = aw.item_ptr_id;' +query_42 = 'SELECT COUNT(*) FROM armory_item ai LEFT JOIN armory_weapon aw on ai.item_id = aw.item_ptr_id WHERE aw.item_ptr_id IS NULL;' + +ans_41 = curs.execute(query_41).fetchone() +ans_42 = curs.execute(query_42).fetchone() + +print(f'How many of the items are weapons? {ans_41}') +print(f'How many of the items are not weapons? {ans_42}') + +# 5. How many items does each chracter have? (Return first 20 rows) +query_5 = 'select cc.name as character_name, cc.character_id, count(cci.id) as item_num from charactercreator_character cc inner join charactercreator_character_inventory cci on cci.character_id = cc.character_id group by cc.character_id LIMIT 20;' + +ans_5 = curs.execute(query_5).fetchall() +print('How many items does each character have? (return first 20 rows)') +print(ans_5) + +# 6. How many Weapons does each chracter have? (Return first 20 rows) +query_6 = 'select cc.name as character_name, cc.character_id, count(cci.id) as weapon_num from charactercreator_character cc inner join charactercreator_character_inventory cci on cci.character_id = cc.character_id inner join armory_item ai on ai.item_id = cci.item_id inner join armory_weapon aw on aw.item_ptr_id = ai.item_id group by cc.character_id LIMIT 20;' + +ans_6 = curs.execute(query_6).fetchall() +print('How many Weapons does each chracter have? (Return first 20 rows)') +print(ans_6) + +# 7. On average, how many items does each chracter have? +query_7 = 'select avg(item_num) from (select cc.name as character_name, cc.character_id, count(cci.id) as item_num from charactercreator_character cc inner join charactercreator_character_inventory cci on cci.character_id = cc.character_id group by cc.character_id LIMIT 20);' + +ans_7 = curs.execute(query_7).fetchone() +print('On average, how many items does each character have?') +print(ans_7) + +# 8. On average, how many Weapons does each chracter have? +query_8 = 'select avg(weapon_num) from (select cc.name as character_name, cc.character_id, count(cci.id) as weapon_num from charactercreator_character cc inner join charactercreator_character_inventory cci on cci.character_id = cc.character_id inner join armory_item ai on ai.item_id = cci.item_id inner join armory_weapon aw on aw.item_ptr_id = ai.item_id group by cc.character_id LIMIT 20);' + +ans_8 = curs.execute(query_8).fetchone() +print('On average, how many Weapons does each character have?') +print(ans_8) From 3e70eb90391b2c2b9671584442d8db14cfaa6cb0 Mon Sep 17 00:00:00 2001 From: johnnykoo84 Date: Thu, 21 May 2020 13:28:15 +0900 Subject: [PATCH 4/4] Complete part 2 --- Pipfile | 14 ++++++++ buddymove_holidayiq.sqlite3 | Bin 0 -> 16384 bytes .../buddymove_holiday.py | 31 ++++++++++++++++++ .../buddymove_holidayiq.sqlite3 | Bin 0 -> 8192 bytes 4 files changed, 45 insertions(+) create mode 100644 Pipfile create mode 100644 buddymove_holidayiq.sqlite3 create mode 100644 module1-introduction-to-sql/buddymove_holidayiq.sqlite3 diff --git a/Pipfile b/Pipfile new file mode 100644 index 00000000..65ea7f56 --- /dev/null +++ b/Pipfile @@ -0,0 +1,14 @@ +[[source]] +name = "pypi" +url = "https://pypi.org/simple" +verify_ssl = true + +[dev-packages] +autopep8 = "*" + +[packages] +sqlite3 = "*" +pandas = "*" + +[requires] +python_version = "3.7" diff --git a/buddymove_holidayiq.sqlite3 b/buddymove_holidayiq.sqlite3 new file mode 100644 index 0000000000000000000000000000000000000000..a7a3ffe3de8f5e18824fb8fd564405c608761a0e GIT binary patch literal 16384 zcmeHMZERcTbw2#Myd*D4N8!EXm6}FTn#76Il=t#GiX+-F{Xxs7CDF1do3tfb5*3S< zNjsz$F)?2!bP&mfjE*iKXO)*~EsO$618ta-&FJR+aMAfsP}mrj4of#|*gBxWvUW?+ zUpw!;B&EwA{U3l-1Ub*S=Q-zn&UxR!a|F3Hr{=nZ1yfE;OcDz96><9d>^XVwiQJ|whM}dw49R)fHbQI_)&{3eH zKu3X&0$()+$Znn4-P5DHeEh=1A2S;-A=g!TZozY%;WHx(tHrv9i&zewmo%8opd!zd2t5$|iz8wWR3Un0c zD9}-$qd-T2jshJ8Itp|Y=qS)pprgPaRKP&G>AU$Gx`W{SFXsd2Pn@@$-*Hx))6OH# z7oAT!ImiDx{@L+U$A^v;2XVaW7;(Jhc*^0he`)`v{b%+K`wjbL`x$%EzSI7+-O2rq z`xox-xj*OLCJ#{%qsvY)UYu{YUw*m?FCJHT#d zo#rpipP7GZ{=ocQ^Ck0)IbjZ&A2*wrUo#Jwzhb`6yvv+p#+U)-X~u5)t?3u0zcu~P zbk$TejhS|u`b@0xbK?W!$Htq+n(xK!#Uc(N+E)uhS_b9x34Ga&Q?o zN+&!-Y%}>BNmz&F*jW$pw3%cy-Gcg&f`_=NvP>+>8ghvx5 zp>L$&k8rR!T7=bUH*rxfYE(1yhpmNqSV>NJ$fF7$+8UM+mx9$4{(4AP8(+`?%adhT zn-$0-3LjE7yaHT$VWD!`L$x86zAU*iIrNYL~7c; zum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DH}*g8vPG~X8qor7!% zDu)HQkB`Hj!Fo5zqD|pd*CL06i@1_iWUh&1QZ+W~CrY+h?;V!s`BL|#& z_K*~{ZLUHJ2`I}t=CTggMKY$4w)#h+;@pF^BwQ8Au{PF#3&+FWv6~!J<2AdiE-#CG`= z?a_E!L>VKl(6=L%9&$j5*lI(sB%;x^DS}N?!#+e>RBn$#+pM7!31TDu2B$Z; z5nrKxwB3|!qzJYQs(k}n>b6LBD}K#tND+Tm774Mxzqar(M)aJQJHoKeb2C2w%z zR(hM>GRknRn+&uC#FO~@8Eo)-@J{LRwc*7A)2m3b**F5AU z#nxJ4IE+iYpT_~eNc!8{LP8m%UB^xp$%|;)+=o&)h|Q6qeY)-;kv7TzhNJf{Y@2YK zo1flJ=xL1MLW-?AXaY^_EO`CJ#_ zZVtV1$33t5wUU*)X7Qe|jI=cm*`Yc%i#+OBxE+9rtd5V(`i!Mz^xjFvTsTdIob+{Rx#sh)0Q#g{L)Vo7Ah zRG&hd5>gdC7@)7#axtiT$T!+dete^}kxJ0-Ay2k3`tVH;zOq39x=A0k(S6!rlxZ2) zlp|UsPqaBo93Hm#N(EnskzN`PKTSt3S19Jo_{NDm-tbaSZz+K9C+X|DTSX7~I(4Fw zsnG&qe0#K!$5$&J@|fz?GSPQ(Wqj$h?j~Prcr^)wTpFsSGQN)Op%$8dgD+ssF5r<^ Go%w&u5&dKU literal 0 HcmV?d00001 diff --git a/module1-introduction-to-sql/buddymove_holiday.py b/module1-introduction-to-sql/buddymove_holiday.py index e69de29b..aaa611ca 100644 --- a/module1-introduction-to-sql/buddymove_holiday.py +++ b/module1-introduction-to-sql/buddymove_holiday.py @@ -0,0 +1,31 @@ +import pandas as pd +import sqlite3 +import os + +conn = sqlite3.connect('buddymove_holidayiq.sqlite3') +cur = conn.cursor() +CSV_FILEPATH = os.path.join(os.path.dirname( + __file__), 'buddymove_holidayiq.csv') +df = pd.read_csv(CSV_FILEPATH) +# print(df.head()) +print(df.shape) + +cur.execute('DROP TABLE IF EXISTS Reviews;') +cur.execute( + 'CREATE TABLE Reviews(user_id, sports, religious, nature, theatre, shopping, picnic);') + +df.to_sql('Reviews', conn, if_exists='replace', index=False) +print('Count how many rows you have') +query_1 = 'SELECT COUNT(*) FROM Reviews' +result_1 = cur.execute(query_1).fetchone() +print(result_1) + +print('How many users who reviewed at least 100 Nature in the category also reviewed at least 100 in the Shopping category?') +query_2 = 'SELECT COUNT(*) FROM Reviews r where r.nature >= 100 AND r.shopping >= 100' +result_2 = cur.execute(query_2).fetchone() +print(result_2) + +print('(Stretch) What are the average number of reviews for each category?') +query_3 = 'SELECT AVG(sports) , AVG(religious), AVG(nature), AVG(theatre), AVG(shopping)FROM Reviews' +result_3 = cur.execute(query_3).fetchone() +[print("{:.2f}".format(int(i))) for i in result_3] diff --git a/module1-introduction-to-sql/buddymove_holidayiq.sqlite3 b/module1-introduction-to-sql/buddymove_holidayiq.sqlite3 new file mode 100644 index 0000000000000000000000000000000000000000..503ac2451a469df381613d1a1e18db537bebb9cc GIT binary patch literal 8192 zcmeI#u?~VT5C-6um^c~T99VADgt+hkiiV9vWH*Qvnjnz2=6l0(=+>%xZYX36#yQ&&F~+#Pp1ot-JQ)XD-TW(@ZML78{l({RW)A`Z2tWV= z5P$##AOHafKmY;|fPfWf_;l&}{1uw~Qj1hPq~sFOt_i?!R$?b44x00Izz00bZa0SG_< N0uX=z1R(Io0$&^|F5>_I literal 0 HcmV?d00001