code_junkie

COALESCE SUM GROUP?

Nov 12th, 2011
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.21 KB | None | 0 0
  1. SELECT
  2. SUM(`order_items`.`quantity`) as `count`,
  3. `menu_items`.`name`
  4. FROM
  5. `orders`,
  6. `menu_items`,
  7. `order_items`
  8. WHERE
  9. `orders`.`id` = `order_items`.`order_id` AND
  10. `menu_items`.`id` = `order_items`.`menu_item_id` AND
  11. `orders`.`date` >= '2008-11-01' AND
  12. `orders`.`date` <= '2008-11-30'
  13. GROUP BY
  14. `menu_items`.`id`
  15.  
  16. SELECT m.name, COALESCE(SUM(oi.quantity), 0) AS count
  17. FROM menu_items AS m
  18. LEFT OUTER JOIN (
  19. order_items AS oi JOIN orders AS o
  20. ON (o.id = oi.order_id)
  21. ) ON (m.id = oi.menu_item_id
  22. AND o.`date` BETWEEN '2008-11-01' AND '2008-11-30')
  23. GROUP BY m.id;
  24.  
  25. +--------+-------+
  26. | name | count |
  27. +--------+-------+
  28. | bread | 2 |
  29. | milk | 1 |
  30. | honey | 2 |
  31. | cheese | 0 |
  32. +--------+-------+
  33.  
  34. DROP TABLE IF EXISTS menu_items;
  35. CREATE TABLE menu_items (
  36. id INT PRIMARY KEY,
  37. name VARCHAR(10)
  38. ) TYPE=InnoDB;
  39.  
  40. DROP TABLE IF EXISTS orders;
  41. CREATE TABLE orders (
  42. id INT PRIMARY KEY,
  43. `date` DATE
  44. ) TYPE=InnoDB;
  45.  
  46. DROP TABLE IF EXISTS order_items;
  47. CREATE TABLE order_items (
  48. order_id INT,
  49. menu_item_id INT,
  50. quantity INT,
  51. PRIMARY KEY (order_id, menu_item_id),
  52. FOREIGN KEY (order_id) REFERENCES orders(id),
  53. FOREIGN KEY (menu_item_id) REFERENCES menu_items(id)
  54. ) TYPE=InnoDB;
  55.  
  56. INSERT INTO menu_items VALUES
  57. (1, 'bread'),
  58. (2, 'milk'),
  59. (3, 'honey'),
  60. (4, 'cheese');
  61.  
  62. INSERT INTO orders VALUES
  63. (1, '2008-11-02'),
  64. (2, '2008-11-03'),
  65. (3, '2008-10-29');
  66.  
  67. INSERT INTO order_items VALUES
  68. (1, 1, 1),
  69. (1, 3, 1),
  70. (2, 1, 1),
  71. (2, 2, 1),
  72. (2, 3, 1),
  73. (3, 4, 10);
  74.  
  75. SELECT
  76. name
  77. , nvl(count, 0) as count
  78. FROM
  79. menu_items items
  80. LEFT JOIN (
  81. SELECT
  82. menu_items.id
  83. , SUM(order_items.quantity) as count
  84. FROM
  85. menu_items
  86. LEFT JOIN order_items ON menu_items.id = order_items.menu_item_id
  87. LEFT JOIN orders ON orders.id = order_items.order_id
  88. WHERE
  89. "date" between to_date('2008-11-01','YYYY-MM-DD') and to_date('2008-11-30','YYYY-MM-DD')
  90. GROUP BY
  91. menu_items.id
  92. ) counts on items.id = counts.id;
  93.  
  94. SQL> create table menu_items ( id number, name varchar2(10));
  95. create table order_items (order_id number, menu_item_id number, quantity number);
  96. create table orders (id number, "date" date);
  97.  
  98. Table created.
  99.  
  100. SQL>
  101. Table created.
  102.  
  103. SQL>
  104. Table created.
  105.  
  106. SQL>
  107. insert into menu_items values (1, 'bread');
  108. insert into menu_items values (2, 'milk');
  109. insert into menu_items values (3, 'honey');
  110. insert into menu_items values (4, 'cheese');
  111. SQL>
  112. 1 row created.
  113.  
  114. SQL>
  115. 1 row created.
  116.  
  117. SQL>
  118. 1 row created.
  119.  
  120. SQL>
  121. 1 row created.
  122.  
  123. SQL>
  124. insert into orders values (1, to_date('2008-11-02', 'YYYY-MM-DD'));
  125. insert into orders values (2, to_date('2008-11-03', 'YYYY-MM-DD'));
  126. insert into orders values (3, to_date('2008-10-29', 'YYYY-MM-DD'));SQL>
  127. 1 row created.
  128.  
  129. SQL>
  130. 1 row created.
  131.  
  132. SQL>
  133. insert into order_items values (1, 1, 1);
  134. insert into order_items values (1, 3, 1);
  135. 1 row created.
  136.  
  137. SQL>
  138. 1 row created.
  139.  
  140. SQL>
  141. insert into order_items values (2, 1, 1);
  142. insert into order_items values (2, 2, 1);
  143. insert into order_items values (2, 3, 1);
  144.  
  145. insert into order_items values (3, 4, 10);
  146. 1 row created.
  147.  
  148. SQL>
  149. 1 row created.
  150.  
  151. SQL>
  152. 1 row created.
  153.  
  154. SQL>
  155. 1 row created.
  156.  
  157. SQL> SQL>
  158.  
  159. 1 row created.
  160.  
  161. SQL>
  162. SELECT
  163. name
  164. , nvl(count, 0) as count
  165. FROM
  166. menu_items items
  167. LEFT JOIN (
  168. SELECT
  169. menu_items.id
  170. , SUM(order_items.quantity) as count
  171. FROM
  172. menu_items
  173. LEFT JOIN order_items ON menu_items.id = order_items.menu_item_id
  174. LEFT JOIN orders ON orders.id = order_items.order_id
  175. WHERE
  176. "date" between to_date('2008-11-01','YYYY-MM-DD') and to_date('2008-11-30','YYYY-MM-DD')
  177. GROUP BY
  178. menu_iteSQL> 2 3 4 5 6 7 ms.id
  179. ) counts on items.id = counts.id; 8 9 10 11 12 13 14 15 16 17 18
  180.  
  181. NAME COUNT
  182. ---------- ----------
  183. bread 2
  184. milk 1
  185. honey 2
  186. cheese 0
  187.  
  188. SQL>
  189. drop table menu_items;
  190. drop table order_items;
  191. drop table orders;SQL>
  192. Table dropped.
  193.  
  194. SQL>
  195. Table dropped.
  196.  
  197. SQL>
  198.  
  199. Table dropped.
  200.  
  201. SQL>
Add Comment
Please, Sign In to add comment