Skip to content

Commit

Permalink
sql: Minor cleanup/formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
reedy committed Jul 9, 2023
1 parent b90755c commit 8289e1c
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 25 deletions.
2 changes: 1 addition & 1 deletion sql/clean-duplicate-airlines.sql
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ alter table airlines add constraint no_duplicates UNIQUE (name);
alter table airlines add column iata text;
alter table airlines add column icao text;
alter table airlines add column frequency int default 0;
update airlines p, (select alid, count(*) cnt from flights group by alid) as f set p.frequency=f.cnt where p.alid=f.alid;
update airlines p, (select alid, count(*) cnt from flights group by alid) as f set p.frequency = f.cnt where p.alid = f.alid;
6 changes: 3 additions & 3 deletions sql/clean-duplicate-planes.sql
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
-- Remove surrounding whitespace & all tabs
update planes
set name=TRIM(REPLACE(name, '\t', ''));
set name = TRIM(REPLACE(name, '\t', ''));

-- Show dupes
select name, min(plid), max(plid), count(*) as dupes from planes GROUP BY name having count(*) > 1 order by dupes;

-- Find exact dupes
drop table if exists tmp_planes_duplicates;
create table tmp_planes_duplicates (plid int, duplicate_of int)
create table tmp_planes_duplicates (plid int, duplicate_of int);
insert into tmp_planes_duplicates
select p1.plid, min(p2.plid) as duplicate_of
from planes p1, planes p2
Expand All @@ -33,7 +33,7 @@ alter table planes add constraint no_duplicates UNIQUE (name);
alter table planes add column iata text;
alter table planes add column icao text;
alter table planes add column frequency int default 0;
update planes p, (select plid, count(*) cnt from flights group by plid) as f set p.frequency=f.cnt where p.plid=f.plid;
update planes p, (select plid, count(*) cnt from flights group by plid) as f set p.frequency = f.cnt where p.plid = f.plid;

-- Set some basic types manually
update planes set iata='146' where name='BAe 146';
Expand Down
14 changes: 7 additions & 7 deletions sql/elite-levels.sql
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
-- Remove elite level from users who have expired
update users set elite="" where validity < now();
update users set elite = "" where validity < now();

-- Remove warning flag from users who have made their profiles public and have under 100 flights
-- (LEFT JOIN ensures that users with 0 flights are included)
update users LEFT JOIN (
select uid from flights group by uid having count(*) < 100
select uid from flights group by uid having count(*) < 100
) as nice_users
on (users.uid = nice_users.uid)
set elite="" where elite="X" and public != 'N' and users.uid!= 1;
set elite = "" where elite="X" and public != 'N' and users.uid ! = 1;

-- Set warning flag for non-elite users with >=100 flights
update users JOIN (
select uid from flights group by uid having count(*) >= 100
select uid from flights group by uid having count(*) >= 100
) as naughty_users
on (users.uid = naughty_users.uid)
set elite="X" where elite="" and users.uid!= 1;
set elite = "X" where elite = "" and users.uid != 1;

-- Set warning flag for non-elite users with hidden profiles
update users set elite="X" where elite="" and uid!= 1 and public="N";
update users set elite = "X" where elite = "" and uid != 1 and public = "N";

-- Summarize
select elite,public,count(*) from users group by elite,public;
select elite, public, count(*) from users group by elite, public;

6 changes: 4 additions & 2 deletions sql/find-duplicate-airports.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
SELECT apid, name, iata, airports.icao FROM airports
INNER JOIN (SELECT icao FROM airports
GROUP BY icao HAVING count(icao) > 1) dup ON airports.icao = dup.icao
INNER JOIN (
SELECT icao FROM airports
GROUP BY icao HAVING count(icao) > 1
) dup ON airports.icao = dup.icao
WHERE airports.icao != ""
ORDER BY icao;

59 changes: 49 additions & 10 deletions sql/top10.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,62 @@ select count(*) as '<br><b>Registered users</b>' from users where uid != 1;
select count(*) as '<b>Total flights</b>' from flights where uid != 1;
select count(*) as '<b>Flights added yesterday</b>' from flights where uid != 1 and date_sub(now(), interval 1 day) < upd_time;

select lpad(count(*),8," ") as '<table width=100%><tr><td style="vertical-align: top;"><pre><b>Top 10 users by flights<br>All time</b>', IF(u.public='N',u.name,concat('<a href="https://openflights.org/user/',u.name,'">',u.name,'</a>')) as '' from users as u,flights as f where u.uid != 1 and u.uid=f.uid group by f.uid order by count(*) desc limit 10;
select lpad(count(*),8," ") as '<table width=100%><tr><td style="vertical-align: top;"><pre><b>Top 10 users by flights<br>All time</b>', IF(u.public='N',u.name,concat('<a href="https://openflights.org/user/',u.name,'">',u.name,'</a>')) as ''
from users as u,flights as f where u.uid != 1 and u.uid = f.uid
group by f.uid order by count(*) desc limit 10;

select lpad(count(*),8," ") as '</pre></td><td style="vertical-align: top;"><pre><b><br>Last week</b>', IF(u.public='N',u.name,concat('<a href="https://openflights.org/user/',u.name,'">',u.name,'</a>')) as '' from users as u,flights as f where u.uid != 1 and u.uid=f.uid and date_sub(now(), interval 7 day) < upd_time group by f.uid order by count(*) desc limit 10;
select lpad(count(*),8," ") as '</pre></td><td style="vertical-align: top;"><pre><b><br>Last week</b>', IF(u.public='N',u.name,concat('<a href="https://openflights.org/user/',u.name,'">',u.name,'</a>')) as ''
from users as u,flights as f
where u.uid != 1 and u.uid = f.uid and date_sub(now(), interval 7 day) < upd_time
group by f.uid order by count(*) desc
limit 10;

select lpad(count(*),8," ") as '</pre></td><td style="vertical-align: top;"><pre><b><br>Yesterday</b>', IF(u.public='N',u.name,concat('<a href="https://openflights.org/user/',u.name,'">',u.name,'</a>')) as '' from users as u,flights as f where u.uid != 1 and u.uid=f.uid and date_sub(now(), interval 1 day) < upd_time group by f.uid order by count(*) desc limit 10;
select lpad(count(*),8," ") as '</pre></td><td style="vertical-align: top;"><pre><b><br>Yesterday</b>', IF(u.public='N',u.name,concat('<a href="https://openflights.org/user/',u.name,'">',u.name,'</a>')) as ''
from users as u,flights as f
where u.uid != 1 and u.uid = f.uid and date_sub(now(), interval 1 day) < upd_time
group by f.uid order by count(*) desc
limit 10;

select lpad(sum(distance),8," ") as '</pre></td></tr><tr><td style="vertical-align: top;"><pre><b>Top 10 users by miles<br>All time</b>', IF(u.public='N',u.name,concat('<a href="https://openflights.org/user/',u.name,'">',u.name,'</a>')) as '' from users as u,flights as f where u.uid != 1 and u.uid=f.uid group by f.uid order by sum(distance) desc limit 10;
select lpad(sum(distance),8," ") as '</pre></td></tr><tr><td style="vertical-align: top;"><pre><b>Top 10 users by miles<br>All time</b>', IF(u.public='N',u.name,concat('<a href="https://openflights.org/user/',u.name,'">',u.name,'</a>')) as ''
from users as u,flights as f
where u.uid != 1 and u.uid = f.uid
group by f.uid
order by sum(distance) desc
limit 10;

select lpad(sum(distance),8," ") as '</pre></td><td style="vertical-align: top;"><pre><b><br>Last week</b>', IF(u.public='N',u.name,concat('<a href="https://openflights.org/user/',u.name,'">',u.name,'</a>')) as '' from users as u,flights as f where u.uid != 1 and u.uid=f.uid and date_sub(now(), interval 7 day) < upd_time group by f.uid order by sum(distance) desc limit 10;
select lpad(sum(distance),8," ") as '</pre></td><td style="vertical-align: top;"><pre><b><br>Last week</b>', IF(u.public='N',u.name,concat('<a href="https://openflights.org/user/',u.name,'">',u.name,'</a>')) as ''
from users as u,flights as f
where u.uid != 1 and u.uid = f.uid and date_sub(now(), interval 7 day) < upd_time
group by f.uid
order by sum(distance) desc
limit 10;

select lpad(sum(distance),8," ") as '</pre></td><td style="vertical-align: top;"><pre><b><br>Yesterday</b>', IF(u.public='N',u.name,concat('<a href="https://openflights.org/user/',u.name,'">',u.name,'</a>')) as '' from users as u,flights as f where u.uid != 1 and u.uid=f.uid and date_sub(now(), interval 1 day) < upd_time group by f.uid order by sum(distance) desc limit 10;
select lpad(sum(distance),8," ") as '</pre></td><td style="vertical-align: top;"><pre><b><br>Yesterday</b>', IF(u.public='N',u.name,concat('<a href="https://openflights.org/user/',u.name,'">',u.name,'</a>')) as ''
from users as u,flights as f
where u.uid != 1 and u.uid = f.uid and date_sub(now(), interval 1 day) < upd_time
group by f.uid
order by sum(distance) desc
limit 10;

select lpad(count,8," ") as '</pre></td></tr><tr><td style="vertical-align: top;"><pre><b>Top 10 users by popularity<br>All time</b>', IF(u.public='N',u.name,concat('<a href="https://openflights.org/user/',u.name,'">',u.name,'</a>')) as '' from users as u where u.uid != 1 order by count desc limit 10;
select lpad(count,8," ") as '</pre></td></tr><tr><td style="vertical-align: top;"><pre><b>Top 10 users by popularity<br>All time</b>', IF(u.public='N',u.name,concat('<a href="https://openflights.org/user/',u.name,'">',u.name,'</a>')) as ''
from users as u
where u.uid != 1
order by count desc
limit 10;

select distinct CONCAT(s.iata,' &harr;') as '</pre></td></tr><tr><td style="vertical-align: top;"><pre><b>Top 10 routes</b>',d.iata as '',lpad(count(fid),6," ") as '' from airports as s,airports as d,flights as f where f.uid != 1 and s.apid=f.src_apid and d.apid=f.dst_apid group by s.apid,d.apid order by count(fid) desc limit 10;
select distinct CONCAT(s.iata,' &harr;') as '</pre></td></tr><tr><td style="vertical-align: top;"><pre><b>Top 10 routes</b>',d.iata as '',lpad(count(fid),6," ") as ''
from airports as s,airports as d,flights as f
where f.uid != 1 and s.apid = f.src_apid and d.apid = f.dst_apid
group by s.apid,d.apid
order by count(fid) desc
limit 10;

select rpad(a.name,20,' ') as '</pre></td><td style="vertical-align: top;"><pre><b>Top 10 airlines</b>', lpad(count(*),6," ") as '' from airlines as a,flights as f where f.uid != 1 and a.alid > 1 and a.alid=f.alid group by f.alid order by count(*) desc limit 10;
select rpad(a.name,20,' ') as '</pre></td><td style="vertical-align: top;"><pre><b>Top 10 airlines</b>', lpad(count(*),6," ") as ''
from airlines as a,flights as f
where f.uid != 1 and a.alid > 1 and a.alid = f.alid
group by f.alid
order by count(*) desc
limit 10;

select rpad(a.name,20,' ') as '</pre></td><td style="vertical-align: top;"><pre><b>Top 10 airports</b>',a.iata as '', lpad(sum(x.ct),6," ") as '' from
( select src_apid as apid, count(*) as ct
Expand All @@ -31,7 +70,7 @@ select rpad(a.name,20,' ') as '</pre></td><td style="vertical-align: top;"><pre>
from flights
GROUP BY dst_apid
) x, airports as a
where a.apid=x.apid
where a.apid = x.apid
group by x.apid
order by sum(x.ct) desc
limit 10;
Expand Down
11 changes: 9 additions & 2 deletions sql/update-demo.sql
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
--
-- Refresh the "demo" flights shown when logging in
--
delete from flights where uid=1;
delete from flights where uid = 1;

-- Select every other flight
-- Note: uid forced to 0 (demo), trid forced to NULL (not in trip)
insert into flights(uid,src_apid,src_date,src_time,dst_apid,distance,code,seat,seat_type,class,reason,plid,alid,trid,duration,registration,note,upd_time,opp,mode) select 1,src_apid,src_date,src_time,dst_apid,distance,code,seat,seat_type,class,reason,plid,alid,null,duration,registration,note,upd_time,opp,mode from flights where fid % 2 = 0 and date_sub(now(), interval 3 day) < upd_time order by upd_time desc limit 200;
insert into flights(uid,src_apid,src_date,src_time,dst_apid,distance,code,seat,seat_type,class,reason,plid,alid,trid,duration,registration,note,upd_time,opp,mode)
select 1,src_apid,src_date,src_time,dst_apid,distance,code,seat,seat_type,class,reason,plid,alid,null,duration,registration,note,upd_time,opp,mode
from flights
where fid % 2 = 0 and date_sub(now(), interval 3 day) < upd_time
order by upd_time desc
limit 200;

select now(), count(*) from flights where uid=1;

0 comments on commit 8289e1c

Please sign in to comment.