Changes between Version 2 and Version 3 of Complex DB Reports


Ignore:
Timestamp:
02/06/26 18:49:34 (43 hours ago)
Author:
231035
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Complex DB Reports

    v2 v3  
    106106ORDER BY activity_rank;
    107107}}}
    108 === Relation
    109 {{{
    110 Relation
    111 }}}
     108=== Relation Algebra
     109{{{
     110Period <-
     111{ (start_ts, end_ts) }
     112
     113ListingsByUser <-
     114γ
     115  user_id := l.owner_id;
     116  listings_created := COUNT(*)
     117(
     118  listings l ⨝
     119  (l.created_at ≥ p.start_ts ∧ l.created_at < p.end_ts)
     120  Period p
     121)
     122ReviewsByUser <-
     123γ
     124  user_id := r.reviewer_id;
     125  reviews_left := COUNT(*);
     126  avg_rating_left := AVG(r.rating)
     127(
     128  reviews r ⨝
     129  (r.created_at ≥ p.start_ts ∧ r.created_at < p.end_ts)
     130  Period p
     131)
     132AppointmentsByUser <-
     133γ
     134  user_id := a.responsible_owner_id;
     135  appointments_total := COUNT(*);
     136  appointments_done := COUNT(a.status = 'DONE');
     137  appointments_no_show := COUNT(a.status = 'NO_SHOW');
     138  appointments_cancelled := COUNT(a.status = 'CANCELLED')
     139(
     140  appointments a ⨝
     141  (a.date_time ≥ p.start_ts ∧ a.date_time < p.end_ts)
     142  Period p
     143)
     144
     145FavoritesByUser <-
     146γ
     147  user_id := f.client_id;
     148  favorites_saved_all_time := COUNT(*)
     149(
     150  favorite_listings f
     151)
     152
     153UserActivity <-
     154((((
     155     users u
     156     ⟕ (u.user_id = l.user_id) ListingsByUser l
     157    )
     158   ⟕ (u.user_id = r.user_id) ReviewsByUser r
     159   )
     160  ⟕ (u.user_id = a.user_id) AppointmentsByUser a
     161 )
     162 ⟕ (u.user_id = f.user_id) FavoritesByUser f
     163)
     164ActivityWithDefaults <-
     165π
     166  u.user_id,
     167  u.username,
     168  u.email,
     169  u.name,
     170  u.surname,
     171  listings_created := COALESCE(l.listings_created, 0),
     172  reviews_left := COALESCE(r.reviews_left, 0),
     173  avg_rating_left := COALESCE(r.avg_rating_left, 0),
     174  appointments_total := COALESCE(a.appointments_total, 0),
     175  appointments_done := COALESCE(a.appointments_done, 0),
     176  appointments_no_show := COALESCE(a.appointments_no_show, 0),
     177  appointments_cancelled := COALESCE(a.appointments_cancelled, 0),
     178  favorites_saved_all_time := COALESCE(f.favorites_saved_all_time, 0)
     179(
     180  UserActivity
     181)
     182
     183ActiveUsers <-
     184σ
     185  listings_created +
     186  reviews_left +
     187  appointments_total +
     188  favorites_saved_all_time > 0
     189(
     190  ActivityWithDefaults
     191)
     192ScoredUsers <-
     193π
     194  *,
     195  activity_score :=
     196    listings_created * 5 +
     197    reviews_left * 3 +
     198    appointments_done * 2 +
     199    favorites_saved_all_time * 1 -
     200    appointments_no_show * 2
     201(
     202  ActiveUsers
     203)
     204
     205RankedUsers <-
     206rank_dense
     207  activity_rank :=
     208    ORDER BY
     209      activity_score DESC,
     210      listings_created DESC,
     211      reviews_left DESC
     212(
     213  ScoredUsers
     214)
     215Result <-
     216τ activity_rank ASC
     217(
     218  π
     219    user_id,
     220    username,
     221    email,
     222    name,
     223    surname,
     224    listings_created,
     225    reviews_left,
     226    avg_rating_left,
     227    appointments_total,
     228    appointments_done,
     229    appointments_no_show,
     230    appointments_cancelled,
     231    favorites_saved_all_time,
     232    activity_score,
     233    activity_rank
     234  (
     235    RankedUsers
     236  )
     237)
     238
     239}}}
     240
     241== Recommending listings to a user by similar users and liked listings
     242This SQL query wants to find the recommended listings for a user based on similar users and his liked listings.
     243This query:
     244*
     245=== SQL
     246{{{
     247}}}
     248=== Relation Algebra
     249{{{
     250
     251}}}