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

az-index.sql 1.3 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
35
36
37
38
  1. --- #dep az-ratings
  2. --- #dep cluster
  3. --- #table az.user_ids
  4. --- #table az.rating
  5. --- #step Index ratings
  6. CREATE INDEX IF NOT EXISTS az_rating_user_idx ON az.raw_ratings (user_key);
  7. CREATE INDEX IF NOT EXISTS az_rating_asin_idx ON az.raw_ratings (asin);
  8. ANALYZE az.raw_ratings;
  9. --- #step Extract user IDs
  10. DROP TABLE IF EXISTS az.user_ids CASCADE;
  11. CREATE TABLE az.user_ids (
  12. user_id SERIAL PRIMARY KEY,
  13. user_key VARCHAR NOT NULL,
  14. UNIQUE (user_key)
  15. );
  16. INSERT INTO az.user_ids (user_key) SELECT DISTINCT user_key FROM az.raw_ratings;
  17. ANALYZE az.user_ids;
  18. --- #step Extract ISBNs
  19. INSERT INTO isbn_id (isbn)
  20. SELECT DISTINCT asin
  21. FROM az.raw_ratings WHERE asin NOT IN (SELECT isbn FROM isbn_id);
  22. ANALYZE isbn_id;
  23. --- #step Set up rating view
  24. DROP VIEW IF EXISTS az.rating;
  25. CREATE VIEW az.rating
  26. AS SELECT user_id, COALESCE(cluster, bc_of_isbn(isbn_id)) AS book_id,
  27. MEDIAN(rating) AS rating,
  28. (array_agg(rating ORDER BY rating_time DESC))[1] AS last_rating,
  29. MEDIAN(rating_time) AS timestamp,
  30. COUNT(rating) AS nratings
  31. FROM az.raw_ratings
  32. JOIN az.user_ids USING (user_key)
  33. JOIN isbn_id ON (isbn = asin)
  34. LEFT JOIN isbn_cluster USING (isbn_id)
  35. GROUP BY user_id, COALESCE(cluster, bc_of_isbn(isbn_id));
Tip!

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

Comments

Loading...