SQL选择并将数据作为列名称

时间:2016-10-11 10:46:49

标签: mysql sql pivot

我有一个存储以下记录的数据库:

+----+---------------------+-------------+-----------------+
| id | user_name           |       status|            date |
+----+---------------------+-------------+-----------------+
|  1 | A                   |         Paid|       2016-10-11|
|  2 | B                   |     Not Paid|       2016-10-12|
|  3 | C                   |         Paid|       2016-10-12|
|  4 | A                   |     Not Paid|       2016-10-13|
+----+---------------------+-------------+-----------------+

我希望获得如下结果:

+----+---------------------+-------------+-----------------+-----------------+
| id | user_name           |   2016-10-11|      2016-10-12 |      2016-10-13 |
+----+---------------------+-------------+-----------------+-----------------+
|  1 | A                   |         Paid|               NA|         Not Paid|
|  2 | B                   |           NA|         Not Paid|               NA|
|  3 | C                   |           NA|             Paid|               Na|
+----+---------------------+-------------+-----------------+-----------------+

如何查询它以获得这样的结果?
PS:英语不好

仅供参考:我正在使用mySQL和DBMS,这是创建脚本:

CREATE TABLE `moneygame`.`pay_table`(  
  `id` INT(11) NOT NULL AUTO_INCREMENT,
  `user_name` VARCHAR(50),
  `status` VARCHAR(50),
  `p_date` DATE,
  PRIMARY KEY (`id`)
);

2 个答案:

答案 0 :(得分:0)

您可以像这样查询

select user_name, max([2016-10-11]) as [2016-10-11], max([2016-10-12]) [2016-10-12],max([2016-10-13]) [2016-10-13] from #yourtable 
pivot 
(max(status) for date in ([2016-10-11],[2016-10-12],[2016-10-13])) p
group by user_name

答案 1 :(得分:0)

如果没有固定数量的日期,那么我不建议您做您想做的事情。无论如何,这是解决问题的方法。

create table p as select * from
(select 1 id, 'A' user_name, 'Paid' status, '2016-10-11' date union 
select 2 id, 'B' user_name, 'Not Paid' status, '2016-10-12' date union 
select 3 id, 'C' user_name, 'Paid' status, '2016-10-12' date union 
select 4 id, 'A' user_name, 'Not Paid' status, '2016-10-13' date) t;
  

日期表示为列

select user_name, ifnull(max(a),'NA') `2016-10-11`, 
ifnull(max(b),'NA') `2016-10-12`, ifnull(max(c),'NA') `2016-10-13`
from (select user_name, 
case when date = '2016-10-11' then status else null end a,
case when date = '2016-10-12' then status else null end b,
case when date = '2016-10-13' then status else null end c
from p group by user_name, date) s group by user_name;

enter image description here

  

当用户名表示为列

如果您拥有固定数量的用户和可移动的日期范围,那么这样做应该是最佳的。

select date, ifnull(max(a),'NA') A, 
ifnull(max(b),'NA') B, ifnull(max(c),'NA') C from
(select date,
case when user_name = 'A' then status else null end a,
case when user_name = 'B' then status else null end b,
case when user_name = 'C' then status else null end c
from p group by date, user_name) x group by date;

enter image description here

顺便说一下,如果你仍然需要做动态列,你应该阅读: