Guest User

Untitled

a guest
Jul 22nd, 2018
357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. Sale | Store | Product | Discount
  2. ---------------------------------
  3. 1 | 1 | B | Yes
  4. 2 | 1 | B | Yes
  5. 3 | 1 | B | No
  6. 4 | 1 | D | Yes
  7. 5 | 1 | A | No
  8. 6 | 2 | A | No
  9. 7 | 3 | B | No
  10. 8 | 3 | B | No
  11. 9 | 1 | D | Yes
  12. 10 | 2 | A | No
  13.  
  14. Store | Product | Sales | Discounts
  15. -----------------------------------
  16. 1 | A | 1 | 0
  17. 1 | B | 3 | 2
  18. 1 | C | 0 | 0
  19. 2 | A | 2 | 0
  20. 2 | B | 0 | 0
  21. 2 | C | 0 | 0
  22. 3 | A | 0 | 0
  23. 3 | B | 2 | 0
  24. 3 | C | 0 | 0
  25.  
  26. Store | Product types sold | Discounts given on product types:
  27. -------------------------------------------------------------
  28. 1 | 2 | 1
  29. 2 | 1 | 0
  30. 3 | 1 | 0
  31.  
  32. mysql> SELECT Store, Product,
  33. COUNT(1) AS Sales,
  34. SUM(IF(Discount='Yes', 1, 0)) AS Discounts
  35. FROM input
  36. WHERE Product IN ('A', 'B', 'C', 'D')
  37. GROUP BY Store, Product
  38. +-------+---------+-------+-----------+
  39. | Store | Product | Sales | Discounts |
  40. +-------+---------+-------+-----------+
  41. | 1 | A | 1 | 0 |
  42. | 1 | B | 3 | 2 |
  43. | 1 | D | 2 | 2 |
  44. | 2 | A | 2 | 0 |
  45. | 3 | B | 2 | 0 |
  46. +-------+---------+-------+-----------+
  47. 5 rows in set (0.00 sec)
  48.  
  49. mysql> SELECT Store,
  50. COUNT(1) AS `Product types sold`,
  51. SUM(Discounts) AS `Discounts given on product types`
  52. FROM (
  53. SELECT Store, Product,
  54. COUNT(1) AS Sales,
  55. SUM(IF(Discount='Yes', 1, 0)) AS Discounts
  56. FROM input
  57. WHERE Product IN ('A', 'B', 'C', 'D')
  58. GROUP BY Store, Product
  59. ) AS intermediate
  60. GROUP BY Store
  61. +-------+--------------------+----------------------------------+
  62. | Store | Product types sold | Discounts given on product types |
  63. +-------+--------------------+----------------------------------+
  64. | 1 | 3 | 4 |
  65. | 2 | 1 | 0 |
  66. | 3 | 1 | 0 |
  67. +-------+--------------------+----------------------------------+
  68. 3 rows in set (0.00 sec)
  69.  
  70. select Store,
  71. (Select count( distinct Product) from products as p2 where p2.Store = p1.Store),
  72. (Select count(distinct Product) from products as p3 where p3.Store = p1.Store and p3.Discount = "Yes")
  73. from products p1
  74. group by Store
Add Comment
Please, Sign In to add comment