如何在有条件的情况下透视表中的行? [英] How to pivot rows in a table with condition?

查看:82
本文介绍了如何在有条件的情况下透视表中的行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用SQL脚本来透视数据库中的数据.这就是表格中的样子.

I want to pivot data in my database with an SQL script. This is how it looks like in the table.

id  name        state   cpu_usage   memory_usage    datetime
1   machine-2   Running 50.6693     5060099372      2013-11-18 17:00:22.550
2   machine-3   Running 19.8329     14951967776     2013-11-18 17:00:22.550
3   machine-4   Running 24.2103     3309923047      2013-11-18 17:00:22.550
4   machine-2   Running 97.2774     2867303718      2013-11-18 17:01:22.550
5   machine-3   Running 66.2991     11048043786     2013-11-18 17:01:22.550
6   machine-5   Running 26.6446     13362336682     2013-11-18 17:01:22.550
7   machine-2   Running 39.5881     6969468931      2013-11-18 17:02:22.550
8   machine-3   Running 99.0000     9340168327      2013-11-18 17:02:22.550
9   machine-4   Running 85.0968     16154409604     2013-11-18 17:02:22.550

这就是我希望结果显示为CPU使用率的方式.

This is how I want the result to look like for CPU usage.

Date                        machine-2   machine-3   machine-4   machine-5
2013-11-18 17:00:22.550     50.6693     19.8329     24.2103     null
2013-11-18 17:01:22.550     97.2774     66.2991     null        26.6446
2013-11-18 17:02:22.550     39.5881     99.0000     85.0968     null

如何使用SQL做到这一点?

How to do this with SQL?

推荐答案

有几种不同的方法可以得到结果.

There are a few different ways that you can get the result.

您可以使用条件逻辑将具有CASE表达式的聚合函数应用于创建新列:

You can apply an aggregate function with a CASE expression using conditional logic to create your new columns:

select datetime,
  max(case when name = 'machine-2' then cpu_usage end) machine2,
  max(case when name = 'machine-3' then cpu_usage end) machine3,
  max(case when name = 'machine-4' then cpu_usage end) machine4,
  max(case when name = 'machine-5' then cpu_usage end) machine5
from yourtable
group by datetime;

请参见带演示的SQL提琴

或者您可以应用PIVOT函数,该函数会将您的name值转换为列:

Or you can apply the PIVOT function which will convert your name values into columns:

select datetime,
  [machine-2], [machine-3], 
  [machine-4], [machine-5]
from
(
  select datetime, name, cpu_usage
  from yourtable
) d
pivot
(
  max(cpu_usage)
  for name in ([machine-2], [machine-3], [machine-4], [machine-5])
) piv;

请参见带演示的SQL小提琴.

如果name列的值数量有限,则上述两个版本将为您工作,但是如果没有,那么您将需要使用动态sql:

The above two versions will work for you if you have a limited number of values for the name columns, but if not then you will need to look at using dynamic sql:

DECLARE @cols AS NVARCHAR(MAX),
    @query  AS NVARCHAR(MAX)

select @cols = STUFF((SELECT distinct ',' + QUOTENAME(name) 
                    from yourtable
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

set @query = 'SELECT datetime,' + @cols + ' 
            from 
            (
              select datetime, name, cpu_usage
              from yourtable
            ) x
            pivot 
            (
                max(cpu_usage)
                for name in (' + @cols + ')
            ) p '

execute sp_executesql @query;

请参见带演示的SQL提琴.所有版本都给出结果:

See SQL Fiddle with Demo. All versions give a result:

|                        DATETIME | MACHINE-2 | MACHINE-3 | MACHINE-4 | MACHINE-5 |
|---------------------------------|-----------|-----------|-----------|-----------|
| November, 18 2013 17:00:22+0000 |   50.6693 |   19.8329 |   24.2103 |    (null) |
| November, 18 2013 17:01:22+0000 |   97.2774 |   66.2991 |    (null) |   26.6446 |
| November, 18 2013 17:02:22+0000 |   39.5881 |        99 |   85.0968 |    (null) |

这篇关于如何在有条件的情况下透视表中的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆