Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fadhilmch #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions politicians.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name,party,location,grade_current
Aaron Schock,R,IL,11.5362423
Adam Kinzinger,R,IL,8.995621754
Adam Schiff,D,LA,13.0347375
Adam Smith,D,WA,10.23695519
Anna Eshoo,D,WA,10.65351543
Bill Cassidy,R,LA,9.285165569
Candice Miller,R,WA,11.70865922
Carolyn Maloney,D,NY,13.03522892
Dana Rohrabacher,R,NY,10.64634133
Duncan Hunter,R,IL,13.66509733
Erik Paulsen,R,LA,11.69134788
Frank Guinta,R,HI,10.94131093
Frank LoBiondo,R,HI,13.08452404
John Kerry,D,LA,11.01029616
Joseph Lieberman,I,NY,7.95536381
Michael Bennet,D,IL,7.98091322
Olympia Snowe,R,WA,13.80125838
Richard Shelby,R,HI,11.31326157
Yvette Clarke,D,NY,12.45211362
Zoe Lofgren,D,HI,12.76643582
Empty file added poll-db.txt
Empty file.
Binary file added poll.db
Binary file not shown.
23 changes: 23 additions & 0 deletions poll.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!-- Release 1 -->

<!-- 1. Hitung jumlah vote untuk Sen. Olympia Snowe yang memiliki id 524. -->

<!-- 2. Sekarang lakukan JOIN tanpa menggunakan id `524`. Query kedua tabel votes dan congress_members. -->

<!-- 3. Sekarang gimana dengan representative Erik Paulsen? Berapa banyak vote yang dia dapatkan? -->

<!-- 4. Buatlah daftar peserta Congress yang mendapatkan vote terbanyak. Jangan sertakan field `created_at` dan `updated_at`. -->

<!-- 5. Sekarang buatlah sebuah daftar semua anggota Congress yang setidaknya mendapatkan beberapa vote dalam urutan dari yang paling sedikit. Dan juga jangan sertakan field-field yang memiliki tipe date. -->

<!-- Release 2 -->

<!-- 1. Siapa anggota Congress yang mendapatkan vote terbanyak? List nama mereka dan jumlah vote-nya. Siapa saja yang memilih politisi tersebut? List nama mereka, dan jenis kelamin mereka. -->

<!-- 2. Berapa banyak vote yang diterima anggota Congress yang memiliki grade di bawah 9 (gunakan field `grade_current`)? Ambil nama, lokasi, grade_current dan jumlah vote. -->

<!-- 3. Apa saja 10 negara bagian yang memiliki voters terbanyak? List semua orang yang melakukan vote di negara bagian yang paling populer. (Akan menjadi daftar yang panjang, kamu bisa gunakan hasil dari query pertama untuk menyederhanakan query berikut ini.) -->

<!-- 4. List orang-orang yang vote lebih dari dua kali. Harusnya mereka hanya bisa vote untuk posisi Senator dan satu lagi untuk wakil. Wow, kita dapat si tukang curang! Segera laporkan ke KPK!! -->

<!-- 5. Apakah ada orang yang melakukan vote kepada politisi yang sama dua kali? Siapa namanya dan siapa nama politisinya? -->
46 changes: 46 additions & 0 deletions query.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const sqlite3 = require('sqlite3').verbose();
const db = new sqlite3.Database('./poll.db');


function release_0_1() {
db.serialize(function(){
db.all("SELECT name,location,grade_current,(SELECT COUNT(*) FROM votes WHERE politicians_id = politicians.politicians_id) AS totalValue FROM politicians WHERE grade_current < 9 ORDER BY grade_current ASC", (err,rows)=>{
console.log("\nRelease 0 1:");
console.log(rows);
});
});
}



function release_0_2() {
db.serialize(function(){
db.all(`SELECT (SELECT COUNT(*) FROM votes WHERE politicians_id = politicians.politicians_id) AS totalVote, politicians.name AS politicianName,(first_name||' '||last_name) AS voterName,gender FROM voters
LEFT JOIN votes ON politicians.politicians_id = votes.politicians_id
LEFT JOIN politicians ON voters.voters_id = votes.voters_id WHERE totalVote IN (SELECT (SELECT COUNT(*) FROM votes WHERE politicians_id = politicians.politicians_id) AS totalVote FROM politicians ORDER BY totalVote DESC LIMIT 3)
ORDER BY totalVote DESC`,(err,rows)=>{
if(err)
console.log(err);
console.log("\nRelease 0 2:");
console.log(rows);
})
})
}

function release_0_3() {
db.serialize(function() {
db.all("SELECT (SELECT COUNT(*) FROM votes WHERE voters_id = voters.voters_id) AS totalVote,(first_name|| ' '||last_name) AS name,gender,age FROM voters WHERE totalVote > 1 ORDER BY totalVote DESC",
(err, rows) => {
if(err)
console.log(err);
console.log("\nRelease 0 3:");
console.log(rows);
});
});
}



// release_0_1();
// release_0_2();
release_0_3();
48 changes: 48 additions & 0 deletions seed-data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//your code here
const sqlite3 = require('sqlite3').verbose();
const db = new sqlite3.Database('./poll.db');

const fs = require('fs');
const voters = fs.readFileSync('./voters.csv','utf8').split('\n');
const votes = fs.readFileSync('./votes.csv', 'utf8').split('\n');
const politicians = fs.readFileSync('./politicians.csv','utf8').split("\n");

write_database(politicians,'politicians');
write_database(voters,'voters');
write_database(votes,'votes');

db.close();


function write_database(arr,table){
db.serialize(function(){
for(let i = 1; i < arr.length-1; i++){
db.run(`INSERT INTO ${table} VALUES (null,${convert_array_value(arr[i])})`);
}
console.log(`Succeed to insert ${table} data to database`);
});
}

function convert_array_value(arr){
let tempArr = arr.split(',');
for(let i = 0;i<tempArr.length;i++){
if(typeof tempArr[i] == 'string'){
tempArr[i] = `"${tempArr[i]}"`
}
}
return tempArr.join(',');
}

function delete_data(table,id){
db.serialize(function(){
db.run(`DELETE FROM ${table} WHERE id = ${id}`);
console.log(`Deleted data ${id}`);
});
}

function update_data(table,id,parameter,data){
db.serialize(function(){
db.run(`UPDATE ${table} SET ${parameter} = ${data} WHERE id = ${id}`);
console.log(`Update ${parameter} to ${data} in ${id}`);
});
}
42 changes: 42 additions & 0 deletions setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//your code here
const sqlite3 = require('sqlite3').verbose();
const db = new sqlite3.Database('./poll.db');

const fs = require('fs');
const voters = fs.readFileSync('./voters.csv','utf8').split('\n');
const votes = fs.readFileSync('./votes.csv', 'utf8').split('\n');
const politicians = fs.readFileSync('./politicians.csv','utf8').split("\n");

create_database_politicians();
create_database_voters();
create_database_votes();

db.close();


function create_database_politicians(){
let tempArr = politicians[0].split(',');
db.serialize(function(){
db.run(`DROP TABLE politicians`);
db.run(`CREATE TABLE IF NOT EXISTS politicians (politicians_id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, ${tempArr[0]} VARCHAR(50) NOT NULL, ${tempArr[1]} VARCHAR(2) NOT NULL, ${tempArr[2]} VARCHAR(2) NOT NULL, ${tempArr[3]} REAL NOT NULL);`);
console.log(`Succeed to create politicians table!`);
});
}

function create_database_voters(){
let tempArr = voters[0].split(',');
db.serialize(function(){
db.run(`DROP TABLE voters`);
db.run(`CREATE TABLE IF NOT EXISTS voters (voters_id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, ${tempArr[0]} VARCHAR(20) NOT NULL,${tempArr[1]} VARCHAR(20) NOT NULL,${tempArr[2]} VARCHAR(6) NOT NULL,${tempArr[3]} INTEGER NOT NULL);`);
console.log(`Succeed to create voters table!`);
});
}

function create_database_votes(){
let tempArr = votes[0].split(',');
db.serialize(function(){
db.run(`DROP TABLE votes`);
db.run(`CREATE TABLE IF NOT EXISTS votes (votes_id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,${tempArr[0]} INTEGER NOT NULL,${tempArr[1]} INTEGER NOT NULL, FOREIGN KEY (${tempArr[1]}) REFERENCES politicians(${tempArr[1]}),FOREIGN KEY (${tempArr[0]}) REFERENCES voters(${tempArr[0]}));`);
console.log(`Succeed to create votes table!`);
});
}
151 changes: 151 additions & 0 deletions voters.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
first_name,last_name,gender,age
Aaliyah,Langworth,male,61
Aaliyah,VonRueden,male,73
Aaliyah,Konopelski,female,39
Abdullah,Block,male,66
Amaya,Satterfield,male,23
Barbara,Ortiz,female,76
Barney,Lubowitz,female,76
Berniece,Sanford,female,23
Betsy,Blick,male,23
Buster,Hodkiewicz,male,56
Cade,Batz,female,61
Cameron,Walsh,female,30
Cameron,Dickinson,female,36
Camilla,Feil,male,23
Candice,Torp,female,23
Courtney,Spinka,female,42
Devon,Kutch,female,26
Devon,Mayer,female,71
Devon,Waters,female,70
Devonte,Erdman,female,27
Devonte,Pfeffer,male,29
Dora,Schinner,male,78
Dora,Cole,female,18
Duncan,Dach,male,70
Duncan,O'Reilly,male,75
Durward,Stroman,male,37
Elias,Watsica,female,50
Eliezer,Predovic,female,30
Elinore,Collier,female,75
Emmett,Corkery,female,88
Emmie,Swaniawski,female,76
Fabian,Murphy,female,85
Fabian,Keeling,male,76
Fabian,Nolan,male,70
Fabiola,Klocko,female,45
Furman,Crist,male,23
Furman,Waelchi,female,26
Gabriel,Ledner,male,62
Gabriel,Dicki,female,87
Gerald,Bernier,male,23
Graciela,Berge,female,21
Grady,Grady,female,34
Graham,Hoppe,female,19
Graham,Padberg,male,58
Harley,Murphy,male,72
Harley,Runolfsdottir,female,37
Hudson,Streich,male,25
Hudson,Donnelly,male,20
Hugh,Feil,male,33
Hunter,Erdman,male,68
Hyman,Grant,male,74
Ian,Dibbert,male,40
Isaac,Ratke,male,81
Isabell,Price,male,73
Iva,Roob,female,44
Ivah,Raynor,male,71
Ivory,Gorczany,female,61
Ivory,Wolf,female,38
Ivy,Stehr,female,89
Janice,Zboncak,male,48
Janice,Erdman,male,57
Jason,Erdman,male,37
Jason,Lang,female,56
Jenifer,Lind,male,47
Jennie,Mante,female,21
Margarett,Wilkinson,male,73
Margarett,Schultz,female,36
Margaretta,Koepp,male,72
Neoma,Gottlieb,male,38
Nona,Murazik,female,88
Norbert,Kozey,male,41
Ottilie,Lakin,female,88
Prudence,Bergstrom,female,33
Prudence,Brakus,female,67
Prudence,Goldner,male,37
Prudence,Kilback,female,88
Queen,White,female,64
Ricardo,Koss,female,65
Richie,Eichmann,male,85
Richie,Sanford,female,70
Santino,Labadie,male,18
Santino,Nienow,female,62
Santos,Hintz,female,20
Santos,Jaskolski,female,79
Sarah,Feeney,female,64
Sarah,Hyatt,female,26
Theodore,Ferry,female,89
Theodore,Stamm,male,56
Theresa,Price,female,83
Theresia,Fay,male,64
Theresia,Rohan,female,20
Tobin,Streich,male,77
Toby,Terry,female,19
Toby,Wehner,male,18
Tom,Huel,female,45
Ursula,Bednar,male,65
Ursula,Mayer,male,63
Ursula,Hahn,female,87
Vanessa,O'Hara,male,28
Vanessa,Rodriguez,female,40
Walker,Sauer,male,19
Walker,Reichert,male,21
Walker,Stoltenberg,female,59
Walker,Stiedemann,female,32
Wallace,Klein,female,41
Walter,Franecki,female,68
Walter,Kuvalis,male,22
Walton,Jerde,female,73
Xavier,Harber,female,38
Xavier,Braun,female,41
Xavier,Streich,female,28
Yadira,Krajcik,female,81
Yasmeen,Lubowitz,male,41
Yasmeen,Thompson,female,80
Yasmine,Keeling,male,61
Yasmine,Rippin,female,77
Yesenia,Schaden,female,55
Yesenia,Watsica,female,26
Yessenia,Hudson,female,42
Yolanda,Jones,male,24
Yolanda,Reynolds,female,26
Yolanda,Abernathy,male,90
Yoshiko,Zulauf,male,42
Yoshiko,Klein,male,37
Yvette,Schuster,male,76
Yvette,Russel,male,88
Yvonne,Wolff,male,31
Zachariah,Dach,male,18
Zachary,Hackett,female,45
Zachary,Durgan,male,69
Zachery,Metz,female,52
Zachery,Lehner,female,25
Zackary,Funk,female,77
Zackery,Schuster,female,53
Zakary,Schiller,male,78
Zakary,Lehner,female,19
Zakary,Hodkiewicz,female,62
Zakary,Padberg,male,30
Zander,Runte,male,50
Zane,Little,female,59
Zaria,Reichert,male,55
Zaria,Towne,male,85
Zechariah,Wolff,female,60
Zechariah,Johnson,female,61
Zelma,Langworth,male,87
Zoe,Bernier,female,55
Zoe,Crona,male,32
Zoie,Waters,female,21
Zoila,Leffler,male,72
Zoila,Cummings,male,52
Loading