|
| 1 | +/* |
| 2 | +Create a running total of standard_amt_usd (in the orders table) over order time with |
| 3 | +no date truncation. Your final table should have two columns: one with the amount |
| 4 | +being added for each new row, and a second with the running total. |
| 5 | +*/ |
| 6 | + |
| 7 | +SELECT standard_amt_usd, |
| 8 | + SUM(standard_amt_usd) OVER (ORDER BY occurred_at) AS running_total |
| 9 | +FROM orders; |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | + |
| 14 | + |
| 15 | +/* |
| 16 | +Create a running total of standard_amt_usd (in the orders table) over order time, but |
| 17 | +this time, date truncate occurred_at by year and partition by that same year-truncated |
| 18 | +occurred_at variable. Your final table should have three columns: One with the amount |
| 19 | +ing added for each row, one for the truncated date, and a final columns with the running |
| 20 | +total within each year. |
| 21 | +*/ |
| 22 | + |
| 23 | +SELECT standard_amt_usd, |
| 24 | + DATE_TRUNC('year', occurred_at) year, |
| 25 | + SUM(standard_amt_usd) OVER ( |
| 26 | + PARTITION BY DATE_TRUNC('year', occurred_at) |
| 27 | + ORDER BY occurred_at |
| 28 | + ) AS running_total |
| 29 | +FROM orders; |
| 30 | + |
| 31 | + |
| 32 | + |
| 33 | + |
| 34 | + |
| 35 | +/* |
| 36 | +Select the id, account_id, and total variable from the orders table, then create a |
| 37 | +column called total_rank that ranks this total amount of paper ordered (from |
| 38 | +highest to lowest) for each account using a partition. Your final table should |
| 39 | +have these four columns. |
| 40 | +*/ |
| 41 | + |
| 42 | +SELECT id, |
| 43 | + account_id, |
| 44 | + total, |
| 45 | + RANK() OVER ( |
| 46 | + PARTITION BY account_id |
| 47 | + ORDER BY total DESC |
| 48 | + ) total_rank |
| 49 | +FROM orders; |
| 50 | + |
| 51 | + |
| 52 | + |
| 53 | + |
| 54 | + |
| 55 | +/* |
| 56 | +Determine how the current order's total revenue (i.e. from sales of all types |
| 57 | +of paper) revenue compares to the next order's total revenue. You'll need to |
| 58 | +use occurred_at and total_amt_usd in the orders table along with LEAD to do |
| 59 | +so. In your query results, there should be four columns: occurred_at, |
| 60 | +total_amt_usd, lead, and lead_difference. |
| 61 | +*/ |
| 62 | + |
| 63 | +SELECT occurred_at, |
| 64 | + total_amt_usd, |
| 65 | + LEAD(total_amt_usd) OVER (ORDER BY occurred_at) AS lead, |
| 66 | + LEAD(total_amt_usd) OVER (ORDER BY occurred_at) - total_amt_usd AS lead_difference |
| 67 | +FROM ( |
| 68 | +SELECT occurred_at, |
| 69 | + SUM(total_amt_usd) AS total_amt_usd |
| 70 | + FROM orders |
| 71 | + GROUP BY 1 |
| 72 | + ) sub |
| 73 | + |
| 74 | + |
| 75 | + |
| 76 | + |
| 77 | + |
| 78 | +/* |
| 79 | +Use the NTILE functionality to divide the accounts into 4 levels in terms of the |
| 80 | +amount of standard_qty for their orders. Your resulting table should have the |
| 81 | +account_id, the occurred_at time for each order, the total amount of standard_qty |
| 82 | +paper purchased, and one of four levels in a standard_quartile column. |
| 83 | +*/ |
| 84 | + |
| 85 | +SELECT account_id, |
| 86 | + occurred_at, |
| 87 | + standard_qty, |
| 88 | + NTILE(4) OVER (PARTITION BY account_id ORDER BY standard_qty) |
| 89 | +FROM orders |
| 90 | +ORDER BY 1; |
| 91 | + |
| 92 | + |
| 93 | + |
| 94 | + |
| 95 | + |
| 96 | +/* |
| 97 | +Use the NTILE functionality to divide the accounts into two levels in terms of |
| 98 | +the amount of gloss_qty for their orders. Your resulting table should have the |
| 99 | +account_id, the occurred_at time for each order, the total amount of gloss_qty |
| 100 | +paper purchased, and one of two levels in a gloss_half column. |
| 101 | +*/ |
| 102 | + |
| 103 | +SELECT account_id, |
| 104 | + occurred_at, |
| 105 | + gloss_qty, |
| 106 | + NTILE(2) OVER (PARTITION BY account_id ORDER BY gloss_qty) |
| 107 | +FROM orders |
| 108 | +ORDER BY 1; |
| 109 | + |
| 110 | + |
| 111 | + |
| 112 | + |
| 113 | + |
| 114 | +/* |
| 115 | +Use the NTILE functionality to divide the orders for each account into 100 |
| 116 | +levels in terms of the amount of total_amt_usd for their orders. Your |
| 117 | +resulting table should have the account_id, the occurred_at time for each |
| 118 | +order, the total amount of total_amt_usd paper purchased, and one of 100 |
| 119 | +levels in a total_percentile column. |
| 120 | +*/ |
| 121 | + |
| 122 | +SELECT account_id, |
| 123 | + occurred_at, |
| 124 | + total_amt_usd, |
| 125 | + NTILE(100) OVER (PARTITION BY account_id ORDER BY total_amt_usd) |
| 126 | +FROM orders |
| 127 | +ORDER BY 1; |
| 128 | + |
| 129 | + |
0 commit comments