How to select table column values ​​as sum value columns

I have the following table:

CREATE TABLE products
(
date DATE,
productname VARCHAR(80),
quantity INT(5)
);

INSERT INTO `products`(`date`, `productname`, `quantity`) VALUES ('2016-12-16','toy',5);
INSERT INTO `products`(`date`, `productname`, `quantity`) VALUES ('2016-12-18','santa',8);
INSERT INTO `products`(`date`, `productname`, `quantity`) VALUES ('2016-12-23','tree',15);
INSERT INTO `products`(`date`, `productname`, `quantity`) VALUES ('2016-11-16','toy',5);
INSERT INTO `products`(`date`, `productname`, `quantity`) VALUES ('2016-11-19','santa',2);
INSERT INTO `products`(`date`, `productname`, `quantity`) VALUES ('2016-11-24','tree',5);
INSERT INTO `products`(`date`, `productname`, `quantity`) VALUES ('2016-10-01','toy',10);
INSERT INTO `products`(`date`, `productname`, `quantity`) VALUES ('2016-10-04','santa',20);
INSERT INTO `products`(`date`, `productname`, `quantity`) VALUES ('2016-10-09','tree',30);
INSERT INTO `products`(`date`, `productname`, `quantity`) VALUES ('2016-10-01','toy',40);
INSERT INTO `products`(`date`, `productname`, `quantity`) VALUES ('2016-10-04','santa',30);
INSERT INTO `products`(`date`, `productname`, `quantity`) VALUES ('2016-10-09','tree',20)

I would like to see the sum of values ​​having dates per month, one per line, and product names as columns, so I created a query such as:

SELECT DATE_FORMAT(`date`, '%Y-%m') As Date, 
IF(`productname` = 'santa', SUM(`quantity`), 'none') As santa,
IF(`productname` = 'toy', SUM(`quantity`), 'none') As toy,
IF(`productname` = 'tree', SUM(`quantity`), 'none') As tree  

FROM `products`

GROUP BY DATE_FORMAT(`date`, '%Y-%m'),`productname`

Which gives me something like this:

+---------+-------+------+------+
| Date    | santa | toy  | tree |
+---------+-------+------+------+
| 2016-10 | 50    | none | none |
+---------+-------+------+------+
| 2016-10 | none  | 50   | none |
+---------+-------+------+------+
| 2016-10 | none  | none | 50   |
+---------+-------+------+------+
| 2016-11 | 2     | none | none |
+---------+-------+------+------+
| 2016-11 | none  | 5    | none |
+---------+-------+------+------+
| 2016-11 | none  | none | 5    |
+---------+-------+------+------+
| 2016-12 | 8     | none | none |
+---------+-------+------+------+
| 2016-12 | none  | 5    | none |
+---------+-------+------+------+
| 2016-12 | none  | none | 15   |
+---------+-------+------+------+

This is almost nice, but I would like it to be that way, so there is only one line for a specific month:

+---------+-------+------+------+
| Date    | santa | toy  | tree |
+---------+-------+------+------+
| 2016-10 | 50    | 50   | 50   |
+---------+-------+------+------+
| 2016-11 | 2     | 5    | 5    |
+---------+-------+------+------+
| 2016-12 | 8     | 5    | 15   |
+---------+-------+------+------+

Can I execute queries with queries?

+4
source share
2 answers

It should do

SELECT DATE_FORMAT(`date`, '%Y-%m') As Date,
  IFNULL(Sum(Case when `productname` = 'santa' then `quantity` end),0) As santa, 
  IFNULL(Sum(Case when `productname` = 'toy' then `quantity` end),0) As toy, 
  IFNULL(Sum(Case when `productname` = 'tree' then `quantity` end),0) As tree 
FROM `products`
GROUP BY DATE_FORMAT(`date`, '%Y-%m');
+2
source

It looks like you want to do a summary query for the product name. Try the following:

SELECT DATE_FORMAT(date, '%Y-%m') AS Date, 
       COALESCE(SUM(CASE WHEN productname = 'santa'
                         THEN quantity END), 'none') AS santa,
       COALESCE(SUM(CASE WHEN productname = 'toy'
                         THEN quantity END), 'none') AS toy,
       COALESCE(SUM(CASE WHEN productname = 'tree'
                         THEN quantity END), 'none') AS tree,
FROM products
GROUP BY DATE_FORMAT(date, '%Y-%m')
+1
source

Source: https://habr.com/ru/post/1664755/


All Articles