Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

bx-index.sql 1.1 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
  1. --- #dep bx-ratings
  2. --- #dep cluster
  3. --- #table bx.rating
  4. --- #table bx.add_action
  5. --- #step Index ratings
  6. CREATE INDEX IF NOT EXISTS bx_rating_user_idx ON bx.raw_ratings (user_id);
  7. CREATE INDEX IF NOT EXISTS bx_rating_isbn_idx ON bx.raw_ratings (isbn);
  8. ANALYZE bx.raw_ratings;
  9. --- #step Extract ISBNs
  10. INSERT INTO isbn_id (isbn)
  11. SELECT DISTINCT isbn
  12. FROM bx.raw_ratings WHERE isbn NOT IN (SELECT isbn FROM isbn_id);
  13. ANALYZE isbn_id;
  14. --- #step Set up rating views
  15. DROP VIEW IF EXISTS bx.rating;
  16. CREATE VIEW bx.rating
  17. AS SELECT user_id, COALESCE(cluster, bc_of_isbn(isbn_id)) AS book_id,
  18. MEDIAN(rating) AS rating, COUNT(rating) AS nratings
  19. FROM bx.raw_ratings
  20. JOIN isbn_id USING (isbn)
  21. LEFT JOIN isbn_cluster USING (isbn_id)
  22. WHERE rating > 0
  23. GROUP BY user_id, book_id;
  24. DROP VIEW IF EXISTS bx.add_action;
  25. CREATE VIEW bx.add_action
  26. AS SELECT user_id, COALESCE(cluster, bc_of_isbn(isbn_id)) AS book_id,
  27. COUNT(rating) AS nactions
  28. FROM bx.raw_ratings
  29. JOIN isbn_id USING (isbn)
  30. LEFT JOIN isbn_cluster USING (isbn_id)
  31. GROUP BY user_id, book_id;
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...