|
| 1 | + |
| 2 | + |
| 3 | + |
| 4 | +select count(*) from cd.facilities |
| 5 | + |
| 6 | +select count(*) from cd.facilities where guestcost >=10 |
| 7 | + |
| 8 | +select recommendedby, count(recommendedby) as count |
| 9 | +from cd.members |
| 10 | +where recommendedby is not null |
| 11 | +group by recommendedby |
| 12 | +order by recommendedby |
| 13 | + |
| 14 | +select f.facid , sum(slots) as total_slots from cd.bookings b inner join cd.facilities f on f.facid = b.facid |
| 15 | +group by f.facid |
| 16 | +order by f.facid |
| 17 | + |
| 18 | + |
| 19 | +select f.facid , sum(slots) as total_slots |
| 20 | +from cd.bookings b inner join cd.facilities f on f.facid = b.facid |
| 21 | +where starttime::date >= '2012-09-01' and starttime::date < '2012-10-01' |
| 22 | +group by f.facid |
| 23 | +order by total_slots |
| 24 | + |
| 25 | +select f.facid , extract(month from date(starttime)) as month, sum(slots) as total_slots |
| 26 | +from cd.bookings b inner join cd.facilities f on f.facid = b.facid |
| 27 | +where starttime::date >= '2012-01-01' and starttime::date < '2013-01-01' |
| 28 | +group by f.facid, extract(month from date(starttime)) |
| 29 | +order by f.facid, month |
| 30 | + |
| 31 | + |
| 32 | + |
| 33 | +select count(distinct cd.members.memid) as count |
| 34 | +from cd.members inner join cd.bookings |
| 35 | +on cd.members.memid = cd.bookings.memid |
| 36 | +-- group by cd.members.memid |
| 37 | +having count(cd.bookings.facid) > 1 |
| 38 | + |
| 39 | + |
| 40 | +select facid, sum(slots) as total_slots from cd.bookings |
| 41 | +group by facid |
| 42 | +having sum(slots)> 1000 |
| 43 | +order by facid |
| 44 | + |
| 45 | + |
| 46 | +select f.name , |
| 47 | + |
| 48 | +sum( slots * case when memid = 0 then f.guestcost else f.membercost end) as revenue |
| 49 | +from cd.bookings b inner join cd.facilities f on b.facid = f.facid |
| 50 | +group by f.name |
| 51 | +order by revenue |
| 52 | + |
| 53 | + |
| 54 | + |
| 55 | +select f.name, |
| 56 | +sum( slots * case when memid = 0 then f.guestcost else f.membercost end) as revenue |
| 57 | +from cd.bookings b inner join cd.facilities f on b.facid = f.facid |
| 58 | +group by f.name |
| 59 | +having sum( slots * case when memid = 0 then f.guestcost else f.membercost end) < 1000 |
| 60 | +order by revenue |
| 61 | + |
| 62 | + |
| 63 | +select facid, sum(slots) as total_slots from cd.bookings |
| 64 | +group by facid |
| 65 | +order by total_slots desc limit 1 |
| 66 | + |
| 67 | +--alternative messy limitless version: |
| 68 | +select facid, sum(slots) as total_slots from cd.bookings |
| 69 | +group by facid |
| 70 | +having sum(slots) = (select max(k) from (select facid, |
| 71 | + sum(slots) as k from cd.bookings group by facid)o) |
| 72 | + |
| 73 | + |
| 74 | +--alternative messy limitless version 2: |
| 75 | + |
| 76 | +select facid, max(totalslots) from ( |
| 77 | + select facid, sum(slots) as totalslots |
| 78 | + from cd.bookings |
| 79 | + group by facid |
| 80 | + ) as sub group by facid |
| 81 | + |
| 82 | + |
| 83 | + |
| 84 | + |
| 85 | +select facid, |
| 86 | +extract(month from date(starttime)) as month, |
| 87 | +sum(slots) as slots from cd.bookings where extract(year from date(starttime)) = '2012' |
| 88 | + |
| 89 | +group by rollup(facid, extract(month from date(starttime))) |
| 90 | +order by facid, month |
| 91 | + |
| 92 | + |
| 93 | +select f.facid, f.name , |
| 94 | + |
| 95 | +trim(to_char(sum(b.slots)/2.0,'9999999999999999D99')) as total_hours |
| 96 | + |
| 97 | +from cd.bookings b inner join cd.facilities f on b.facid=f.facid |
| 98 | +group by f.facid , f.name |
| 99 | +order by f.facid |
| 100 | + |
| 101 | + |
| 102 | +-- логика - выбрать первый букинг - это все даты больше 2012 сент и первый из них - это минимальный!!! |
| 103 | +select surname, firstname, cd.members.memid, min(starttime) as starttime |
| 104 | +from cd.members |
| 105 | +inner join cd.bookings |
| 106 | +on cd.members.memid = cd.bookings.memid |
| 107 | +where starttime::date >= '2012-09-01' |
| 108 | +group by surname, firstname, cd.members.memid |
| 109 | +order by cd.members.memid |
| 110 | + |
| 111 | + |
| 112 | + |
| 113 | +select (select count(*) as k from cd.members) , |
| 114 | + firstname,surname |
| 115 | +from cd.members m |
| 116 | +order by m.joindate |
| 117 | + |
| 118 | + |
| 119 | +select row_number() over( order by joindate) , |
| 120 | +firstname,surname |
| 121 | +from cd.members |
| 122 | + |
| 123 | + |
| 124 | +select facid, total from ( |
| 125 | + |
| 126 | +select facid, sum(slots) as total, |
| 127 | +rank() over (order by sum(slots) desc) as rank |
| 128 | +from cd.bookings group by facid) as ranked |
| 129 | +where rank =1 |
| 130 | + |
| 131 | + |
| 132 | +-- my solution |
| 133 | +select rank2.name, |
| 134 | +rank() over (order by rank2.revenue desc) as ranky from |
| 135 | +(select f.name, |
| 136 | +sum( slots * case when memid = 0 |
| 137 | +then f.guestcost else f.membercost end) as revenue |
| 138 | +from cd.bookings b inner join cd.facilities f on b.facid = f.facid |
| 139 | +group by f.name) rank2 |
| 140 | + order by ranky limit 3 |
| 141 | + |
| 142 | + |
| 143 | +select name, rank from ( |
| 144 | + select facs.name as name, rank() over (order by sum(case |
| 145 | + when memid = 0 then slots * facs.guestcost |
| 146 | + else slots * membercost |
| 147 | + end) desc) as rank |
| 148 | + from cd.bookings bks |
| 149 | + inner join cd.facilities facs |
| 150 | + on bks.facid = facs.facid |
| 151 | + group by facs.name |
| 152 | + ) as subq |
| 153 | + where rank <= 3 |
| 154 | +order by rank; |
| 155 | + |
| 156 | + |
| 157 | + |
| 158 | + |
| 159 | + |
| 160 | +select |
| 161 | +firstname,surname,hours, |
| 162 | +rank() over( order by hours desc) as rank |
| 163 | + |
| 164 | + from (select m.firstname, m.surname, ((sum(b.slots)+10)/20)*10 as hours |
| 165 | + |
| 166 | + from |
| 167 | + cd.bookings b |
| 168 | + inner join cd.facilities f on b.facid = f.facid |
| 169 | + inner join cd.members m on m.memid = b.memid |
| 170 | + group by m.firstname, m.surname)k |
| 171 | + order by rank, surname, firstname |
| 172 | + |
| 173 | + |
| 174 | + |
| 175 | + select name, case when class = 1 then 'high' when class = 2 then 'average' else 'low' end as revenue from |
| 176 | +(select f.name as name, |
| 177 | + |
| 178 | +ntile(3) over (order by |
| 179 | + |
| 180 | + sum( |
| 181 | + case when memid = 0 then slots * f.guestcost |
| 182 | + else slots * membercost end) desc ) as class |
| 183 | + from cd.bookings b inner join cd.facilities f on f.facid = b.facid |
| 184 | + group by f.name) as subq |
| 185 | + order by class,name |
| 186 | + |
| 187 | + |
| 188 | + |
| 189 | + select facs.name as name, |
| 190 | + facs.initialoutlay/((sum(case |
| 191 | + when memid = 0 then slots * facs.guestcost |
| 192 | + else slots * membercost |
| 193 | + end)/3) - facs.monthlymaintenance) as months |
| 194 | + from cd.bookings bks |
| 195 | + inner join cd.facilities facs |
| 196 | + on bks.facid = facs.facid |
| 197 | + group by facs.facid |
| 198 | +order by name; |
| 199 | + |
| 200 | + |
| 201 | +select dategen.date, |
| 202 | + ( |
| 203 | + -- correlated subquery that, for each day fed into it, |
| 204 | + -- finds the average revenue for the last 15 days |
| 205 | + select sum(case |
| 206 | + when memid = 0 then slots * facs.guestcost |
| 207 | + else slots * membercost |
| 208 | + end) as rev |
| 209 | + |
| 210 | + from cd.bookings bks |
| 211 | + inner join cd.facilities facs |
| 212 | + on bks.facid = facs.facid |
| 213 | + where bks.starttime > dategen.date - interval '14 days' |
| 214 | + and bks.starttime < dategen.date + interval '1 day' |
| 215 | + )/15 as revenue |
| 216 | + from |
| 217 | + ( |
| 218 | + -- generates a list of days in august |
| 219 | + select cast(generate_series(timestamp '2012-08-01', |
| 220 | + '2012-08-31','1 day') as date) as date |
| 221 | + ) as dategen |
| 222 | +order by dategen.date; |
| 223 | + |
| 224 | + |
0 commit comments