Advertisement
Guest User

Untitled

a guest
May 6th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. +------+------------+-------+--------+----------+
  2. | ID | Orderdate | apple | banana | quantity |
  3. +------+------------+-------+--------+----------+
  4. | 1005 | 2015-05-05 | 2 | 0 | 2 |
  5. | 1005 | 2015-05-05 | 0 | 2 | 2 |
  6. | 1005 | 2015-05-06 | 0 | 1 | 1 |
  7. | 1006 | 2011-05-06 | 0 | 3 | 3 |
  8. | 1006 | 2011-10-06 | 1 | 0 | 1 |
  9. +------+------------+-------+--------+----------+
  10.  
  11. +------+------------+-------+--------+----------+
  12. | ID | Orderdate | apple | banana | quantity |
  13. +------+------------+-------+--------+----------+
  14. | 1005 | 2015-05-05 | 2 | 2 | 4 |
  15. | 1005 | 2015-05-06 | 2 | 3 | 5 |
  16. | 1006 | 2011-05-06 | 0 | 3 | 3 |
  17. | 1006 | 2011-10-06 | 1 | 3 | 4 |
  18. +------+------------+-------+--------+----------+
  19.  
  20. insert into testfinal1 (ID, Orderdate, apple, banana, quantity)
  21. select ID, Orderdate,
  22. (select sum(apple)from testfinal where date_format(OrderDate, '%Y-%m-%d') <= date_format(OrderDate, '%Y-%m-%d' )) as apple,
  23. (select sum(banana)from testfinal where date_format(OrderDate, '%Y-%m-%d') <= date_format(OrderDate, '%Y-%m-%d' )) as banana,
  24. (select sum(quantity)from testfinal where date_format(OrderDate, '%Y-%m-%d') <= date_format(OrderDate, '%Y-%m-%d' )) as quantity
  25. from testfinal group by ID, Orderdate;
  26.  
  27. SELECT
  28. ID,
  29. Orderdate,
  30. SUM(apple) AS 'apple',
  31. SUM(banana) AS 'banana',
  32. SUM(quantity) AS 'quantity'
  33. FROM testfinal
  34. GROUP BY Orderdate;
  35.  
  36. DROP TABLE IF EXISTS testfinal1;
  37.  
  38. CREATE TABLE testfinal1 LIKE testfinal;
  39.  
  40. INSERT INTO testfinal1(ID,Orderdate,apple,banana,quantity)
  41.  
  42. SELECT
  43. ID,
  44. Orderdate,
  45. SUM(apple) AS 'apple',
  46. SUM(banana) AS 'banana',
  47. SUM(quantity) AS 'quantity'
  48. FROM testfinal
  49. GROUP BY Orderdate;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement