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