1

我有一个表格,它跟踪来自应用程序的返回码。我试图弄清楚如何格式化查询以将每天的返回码返回到单行中。这是示例表:

ID  COMPUTER    RETURNCODE  DATE
--  --------    ----------  ----
1   computer1   0           May, 15 2013 00:00:00+0000
2   computer1   1           May, 16 2013 00:00:00+0000
3   computer1   1           May, 17 2013 00:00:00+0000
4   computer2   0           May, 15 2013 00:00:00+0000
5   computer2   4           May, 16 2013 00:00:00+0000
6   computer2   4           May, 17 2013 00:00:00+0000
7   computer3   0           May, 15 2013 00:00:00+0000
8   computer3   8           May, 16 2013 00:00:00+0000
9   computer3   0           May, 17 2013 00:00:00+0000

这是我试图通过查询获得的输出:

COMPUTER    2013-05-15    2013-05-16    2013-05-17
--------    ----------    ----------    ----------
computer1       0             1             1
computer2       0             4             4
computer3       0             8             0

我在这里通过 sqlfiddle 创建了一个示例:SQL Fiddle 示例:http ://sqlfiddle.com/#!2/44687/1/0

任何帮助表示赞赏!

4

1 回答 1

0

这称为“透视”数据。这是在 MySQL 中执行此操作的一种方法:

SELECT computer,
       max(case when date = '2013-05-15' then returncode end),
       max(case when date = '2013-05-16' then returncode end),
       max(case when date = '2013-05-17' then returncode end)
FROM returncodes
group by computer
于 2013-05-16T01:31:06.437 回答