Skip to content

Instantly share code, notes, and snippets.

@Sstobo
Created January 30, 2018 18:32
Show Gist options
  • Save Sstobo/795eae0134035e3577a61fb6c6234ce3 to your computer and use it in GitHub Desktop.
Save Sstobo/795eae0134035e3577a61fb6c6234ce3 to your computer and use it in GitHub Desktop.
postgre commands
#### start times for bookings by members named 'David Farrell'
select bks.starttime
from
cd.bookings bks
inner join cd.members mems
on mems.memid = bks.memid
where
mems.firstname='David'
and mems.surname='Farrell';
#### list of the start times for bookings for tennis courts, for the date '2012-09-21'? Return a
list of start time and facility name pairings, ordered by the time.
select bks.starttime as start, facs.name as name
from
cd.facilities facs
inner join cd.bookings bks
on facs.facid = bks.facid
where
facs.facid in (0,1) and
bks.starttime >= '2012-09-21' and
bks.starttime < '2012-09-22'
order by bks.starttime;
#### list of all members who have recommended another member? Ensure that there are no duplicates in the
list, and that results are ordered by (surname, firstname).
select distinct recs.firstname as firstname, recs.surname as surname
from
cd.members mems
inner join cd.members recs
on recs.memid = mems.recommendedby
order by surname, firstname;
#### list of all members, including the individual who recommended them (if any)? Ensure that results are
ordered by (surname, firstname).
select mems.firstname as memfname, mems.surname as memsname, recs.firstname as recfname, recs.surname as recsname
from
cd.members mems
left outer join cd.members recs
on recs.memid = mems.recommendedby
order by memsname, memfname;
#### members who have used a tennis court? Include in your output the name of the court, and the name of the member formatted
as a single column. Ensure no duplicate data, and order by the member name.
select distinct mems.firstname || ' ' || mems.surname as member, facs.name as facility
from
cd.members mems
inner join cd.bookings bks
on mems.memid = bks.memid
inner join cd.facilities facs
on bks.facid = facs.facid
where
bks.facid in (0,1)
order by member
#### bookings on the day of 2012-09-14 which will cost the member (or guest) more than $30? Remember that guests
have different costs to members (the listed costs are per half-hour 'slot'), and the guest user is always ID 0. Include
in your output the name of the facility, the name of the member formatted as a single column, and the cost. Order by
descending cost, and do not use any subqueries.
select mems.firstname || ' ' || mems.surname as member,
facs.name as facility,
case
when mems.memid = 0 then
bks.slots*facs.guestcost
else
bks.slots*facs.membercost
end as cost
from
cd.members mems
inner join cd.bookings bks
on mems.memid = bks.memid
inner join cd.facilities facs
on bks.facid = facs.facid
where
bks.starttime >= '2012-09-14' and
bks.starttime < '2012-09-15' and (
(mems.memid = 0 and bks.slots*facs.guestcost > 30) or
(mems.memid != 0 and bks.slots*facs.membercost > 30)
)
order by cost desc;
#### output a list of all members, including the individual who recommended them (if any), without using any
joins? Ensure that there are no duplicates in the list, and that each firstname + surname pairing is formatted
as a column and ordered.
select distinct mems.firstname || ' ' || mems.surname as member,
(select recs.firstname || ' ' || recs.surname as recommender
from cd.members recs
where recs.memid = mems.recommendedby
)
from
cd.members mems
order by member;
------ OR
select member, facility, cost from (
select
mems.firstname || ' ' || mems.surname as member,
facs.name as facility,
case
when mems.memid = 0 then
bks.slots*facs.guestcost
else
bks.slots*facs.membercost
end as cost
from
cd.members mems
inner join cd.bookings bks
on mems.memid = bks.memid
inner join cd.facilities facs
on bks.facid = facs.facid
where
bks.starttime >= '2012-09-14' and
bks.starttime < '2012-09-15'
) as bookings
where cost > 30
order by cost desc;
####
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment