首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何选择表行值作为值和的列

如何选择表行值作为值和的列
EN

Stack Overflow用户
提问于 2016-12-24 10:19:37
回答 2查看 63关注 0票数 4

我有下表:

代码语言:javascript
复制
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)

我希望看到每个月的日期为一行,产品名称为列的数量之和,因此我创建了如下查询:

代码语言:javascript
复制
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`

这给了我这样的感觉:

代码语言:javascript
复制
+---------+-------+------+------+
| 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   |
+---------+-------+------+------+

这几乎很好,但我希望它是这样的,所以在一个特定的月份只有一排:

代码语言:javascript
复制
+---------+-------+------+------+
| Date    | santa | toy  | tree |
+---------+-------+------+------+
| 2016-10 | 50    | 50   | 50   |
+---------+-------+------+------+
| 2016-11 | 2     | 5    | 5    |
+---------+-------+------+------+
| 2016-12 | 8     | 5    | 15   |
+---------+-------+------+------+

是否可以通过查询来实现?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-12-24 10:24:21

这应该可以

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2016-12-24 10:29:42

看起来您想要对产品名称执行枢轴查询。试试这个:

代码语言:javascript
复制
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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41312388

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档