general-example-table.html 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <!--
  2. To render properly, keep the left-hand alignment of the table
  3. -->
  4. <table>
  5. <thead>
  6. <tr>
  7. <th></th>
  8. <th>Idiomatic Materialize SQL</th>
  9. </tr>
  10. </thead>
  11. <tbody>
  12. <tr>
  13. <td><code>ANY()</code> Equi-join condition</td>
  14. <td class="copyableCode">
  15. ***If no duplicates in the unnested field***
  16. ```mzsql
  17. -- sales_items.items contains no duplicates. --
  18. WITH individual_sales_items AS
  19. (SELECT unnest(items) as item, week_of FROM sales_items)
  20. SELECT s.week_of, o.order_id, o.item, o.quantity
  21. FROM orders o
  22. JOIN individual_sales_items s ON o.item = s.item
  23. WHERE date_trunc('week', o.order_date) = s.week_of;
  24. ```
  25. ***If duplicates exist in the unnested field***
  26. ```mzsql
  27. -- sales_items.items may contains duplicates --
  28. WITH individual_sales_items AS
  29. (SELECT DISTINCT unnest(items) as item, week_of FROM sales_items)
  30. SELECT s.week_of, o.order_id, o.item, o.quantity
  31. FROM orders o
  32. JOIN individual_sales_items s ON o.item = s.item
  33. WHERE date_trunc('week', o.order_date) = s.week_of
  34. ORDER BY s.week_of, o.order_id, o.item, o.quantity
  35. ;
  36. ```
  37. </td>
  38. </tr>
  39. <tr>
  40. <td><code>mz_now()</code> cannot be used with date/time operators</td>
  41. <td class="copyableCode">
  42. ```mzsql
  43. SELECT * from orders
  44. WHERE mz_now() > order_date + INTERVAL '5min'
  45. ;
  46. ```
  47. </td>
  48. </tr>
  49. <tr>
  50. <td><code>mz_now()</code> cannot be used with <code>OR</code>s in materialized/indexed view definitions and <code>SUBSCRIBE</code> statements</td>
  51. <td class="copyableCode">
  52. **Rewrite as `UNION ALL` with possible duplicates**
  53. ```mzsql
  54. CREATE MATERIALIZED VIEW forecast_completed_orders_duplicates_possible AS
  55. SELECT item, quantity, status from orders
  56. WHERE status = 'Shipped'
  57. UNION ALL
  58. SELECT item, quantity, status from orders
  59. WHERE order_date + interval '30' minutes >= mz_now()
  60. ;
  61. ```
  62. **Rewrite as UNION ALL that avoids duplicates across queries**
  63. ```mzsql
  64. CREATE MATERIALIZED VIEW forecast_completed_orders_deduplicated_union_all AS
  65. SELECT item, quantity, status from orders
  66. WHERE status = 'Shipped'
  67. UNION ALL
  68. SELECT item, quantity, status from orders
  69. WHERE order_date + interval '30' minutes >= mz_now()
  70. AND status != 'Shipped' -- Deduplicate by excluding those with status 'Shipped'
  71. ;
  72. ```
  73. **Rewrite as UNION to deduplicate any and all duplicated results**
  74. ```mzsql
  75. CREATE MATERIALIZED VIEW forecast_completed_orders_deduplicated_results AS
  76. SELECT item, quantity, status from orders
  77. WHERE status = 'Shipped'
  78. UNION
  79. SELECT item, quantity, status from orders
  80. WHERE order_date + interval '30' minutes >= mz_now()
  81. ;
  82. ```
  83. </td>
  84. </tr>
  85. </tbody>
  86. </table>
  87. <!--
  88. To render properly, keep the left-hand alignment of table
  89. -->