|
| 1 | +SHOW server_version \gset |
| 2 | +SELECT substring(:'server_version', '\d+')::int = 14; |
| 3 | + ?column? |
| 4 | +---------- |
| 5 | + f |
| 6 | +(1 row) |
| 7 | + |
| 8 | +SELECT substring(:'server_version', '\d+')::int = 13; |
| 9 | + ?column? |
| 10 | +---------- |
| 11 | + f |
| 12 | +(1 row) |
| 13 | + |
| 14 | +-- Create table to insert summaries. |
| 15 | +create table popular_products |
| 16 | +( |
| 17 | + review_summary jsonb, |
| 18 | + year double precision, |
| 19 | + month double precision |
| 20 | +); |
| 21 | +set topn.number_of_counters to 1000; |
| 22 | +-- Create different summaries by grouping the reviews according to their year and month. |
| 23 | +insert into |
| 24 | + popular_products(review_summary, year, month ) |
| 25 | +select |
| 26 | + topn_add_agg(product_id), |
| 27 | + extract(year from review_date) as year, |
| 28 | + extract(month from review_date) as month |
| 29 | +from |
| 30 | + customer_reviews |
| 31 | +group by |
| 32 | + year, |
| 33 | + month; |
| 34 | +-- Create another table for the merged results. |
| 35 | +create table |
| 36 | + overall_result(merged_summary jsonb); |
| 37 | +-- Let's merge the summaries for the overall result and check top-20 items. |
| 38 | +insert into |
| 39 | + overall_result(merged_summary) |
| 40 | +select |
| 41 | + topn_union_agg(review_summary) |
| 42 | +from |
| 43 | + popular_products; |
| 44 | +select |
| 45 | + (topn(merged_summary, 20)).* |
| 46 | +from |
| 47 | + overall_result |
| 48 | +order by 2 DESC, 1; |
| 49 | + item | frequency |
| 50 | +------------+----------- |
| 51 | + 0671003755 | 576 |
| 52 | + 0613033205 | 575 |
| 53 | + 0671014730 | 572 |
| 54 | + 0743527550 | 572 |
| 55 | + 0375700757 | 555 |
| 56 | + 0871136791 | 555 |
| 57 | + 0679460691 | 551 |
| 58 | + 0375402926 | 550 |
| 59 | + 0613221583 | 536 |
| 60 | + 0812550293 | 536 |
| 61 | + 1575110458 | 536 |
| 62 | + 1590073983 | 536 |
| 63 | + 1590073991 | 536 |
| 64 | + 0375402675 | 524 |
| 65 | + 0375403477 | 524 |
| 66 | + 0375703241 | 524 |
| 67 | + 0399143904 | 524 |
| 68 | + 0425170349 | 524 |
| 69 | + 0613222407 | 524 |
| 70 | + 0765342294 | 510 |
| 71 | +(20 rows) |
| 72 | + |
| 73 | +-- Test window functions |
| 74 | +CREATE TABLE daily_populars |
| 75 | +( |
| 76 | + date DATE, |
| 77 | + agg_data JSONB |
| 78 | +); |
| 79 | +INSERT INTO daily_populars |
| 80 | + SELECT |
| 81 | + date_trunc('day', review_date), |
| 82 | + topn_add_agg(product_id) |
| 83 | + FROM |
| 84 | + customer_reviews |
| 85 | + GROUP BY |
| 86 | + 1; |
| 87 | +SELECT |
| 88 | + date, |
| 89 | + topn_union_agg(agg_data) OVER seven_days |
| 90 | +FROM |
| 91 | + daily_populars |
| 92 | +WINDOW |
| 93 | + seven_days AS (ORDER BY date ASC ROWS 6 PRECEDING) |
| 94 | +ORDER BY |
| 95 | + 1 |
| 96 | +LIMIT 5; |
| 97 | + date | topn_union_agg |
| 98 | +------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| 99 | + 12-30-1970 | {"1551802538": 1, "1551803542": 1} |
| 100 | + 06-19-1995 | {"0898624932": 1, "1551802538": 1, "1551803542": 1} |
| 101 | + 06-23-1995 | {"0521469112": 1, "0898624932": 1, "1551802538": 1, "1551803542": 1} |
| 102 | + 07-14-1995 | {"0521469112": 1, "0679722955": 1, "0898624932": 1, "1551802538": 1, "1551803542": 1} |
| 103 | + 07-18-1995 | {"0195069056": 1, "0471114251": 1, "0517887290": 1, "0521469112": 1, "0679722955": 1, "0898624932": 1, "0962344788": 1, "1551802538": 1, "1551803542": 1, "1574531093": 1} |
| 104 | +(5 rows) |
| 105 | + |
0 commit comments