Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将行值转换为列值SQL PIVOT_Sql_Sql Server_Pivot - Fatal编程技术网

将行值转换为列值SQL PIVOT

将行值转换为列值SQL PIVOT,sql,sql-server,pivot,Sql,Sql Server,Pivot,SQL从表中返回以下结果 +-----+----+-----+----+----+ | PID | PY | QTY | TS | TM | +-----+----+-----+----+----+ | 99 | CT | 1 | 1 | 6 | | 99 | E | 3 | 1 | 5 | | 99 | GR | 4 | 1 | 6 | +-----+----+-----+----+----+ 我想使用PIVOT(如果可能的话)使结果如下: +-----+--

SQL从表中返回以下结果

+-----+----+-----+----+----+
| PID | PY | QTY | TS | TM |
+-----+----+-----+----+----+
|  99 | CT |   1 |  1 |  6 |
|  99 | E  |   3 |  1 |  5 |
|  99 | GR |   4 |  1 |  6 |
+-----+----+-----+----+----+
我想使用PIVOT(如果可能的话)使结果如下:

+-----+----+-----+----+----+----+------+------+------+------+------+------+------+
| PID | PY | QTY | TS | TM | PY | QTY  |  TS  |  TM  |  PY  | QTY  |  TS  |  TM  |
+-----+----+-----+----+----+----+------+------+------+------+------+------+------+
|  99 | CT |   1 |  1 |  6 | E    | 3    | 1    | 5    | GR   | 4    | 1    | 6    |
| 100 | V  |   6 |  6 |  2 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
+-----+----+-----+----+----+----+------+------+------+------+------+------+------+

如果值的数量有限,那么获得结果的最简单方法是将聚合函数与一些CASE表达式一起使用

select 
  pid,
  PY1 = max(case when py = 'CT' then PY end),
  QTY1 = max(case when py = 'CT' then QTY else 0 end),
  TS1 = max(case when py = 'CT' then TS else 0 end),
  TM1 = max(case when py = 'CT' then TM else 0 end),
  PY2 = max(case when py = 'E' then PY end),
  QTY2 = max(case when py = 'E' then QTY else 0 end),
  TS2 = max(case when py = 'E' then TS else 0 end),
  TM2 = max(case when py = 'E' then TM else 0 end),
  PY3 = max(case when py = 'GR' then PY end),
  QTY3 = max(case when py = 'GR' then QTY else 0 end),
  TS3 = max(case when py = 'GR' then TS else 0 end),
  TM3 = max(case when py = 'GR' then TM else 0 end),
  PY4 = max(case when py = 'V' then PY end),
  QTY4 = max(case when py = 'V' then QTY else 0 end),
  TS4 = max(case when py = 'V' then TS else 0 end),
  TM4 = max(case when py = 'V' then TM else 0 end)
from yourtable
group by pid;
看。您可以使用PIVOT来获得结果,但结果会更混乱,因为您需要先取消对多个列的PIVOT,然后再对它们进行PIVOT。如果要使用pivot函数,则必须执行以下操作

首先,使用
row_number()
为每个
pid
组合指定一个唯一的值:

select pid, py, qty, ts, tm,
  rn = row_number() over(partition by pid order by pid)
from yourtable

然后取消打印多列
py
qty
ts
tm

select 
  pid, 
  new_col = col + cast(rn as varchar(10)),  
  val
from
(
  select pid, py, qty, ts, tm,
    rn = row_number() over(partition by pid order by pid)
  from yourtable
) d
cross apply
(
  select 'py', py union all
  select 'qty', cast(qty as varchar(10)) union all
  select 'ts', cast(ts as varchar(10)) union all
  select 'tm', cast(tm as varchar(10))
) c (col, val)

我使用
交叉应用
将多列转换为多行。最后,您将透视
新列及其相应的值:

select pid,
    py1, qty1, ts1, tm1, py2, qty2, ts2, tm2,
    py3, qty3, ts3, tm3, py4, qty4, ts4, tm4
from
(
  select 
    pid, 
    new_col = col + cast(rn as varchar(10)),  
    val
  from
  (
    select pid, py, qty, ts, tm,
      rn = row_number() over(partition by pid order by pid)
    from yourtable
  ) d
  cross apply
  (
    select 'py', py union all
    select 'qty', cast(qty as varchar(10)) union all
    select 'ts', cast(ts as varchar(10)) union all
    select 'tm', cast(tm as varchar(10))
  ) c (col, val)
) src
pivot
(
    max(val)
    for new_col in (py1, qty1, ts1, tm1, py2, qty2, ts2, tm2,
                    py3, qty3, ts3, tm3, py4, qty4, ts4, tm4)
) piv;

两个版本将给出相同的最终结果

| pid | py1 | qty1 | ts1 | tm1 | py2 | qty2 | ts2 | tm2 | py3 | qty3 | ts3 | tm3 |    py4 |   qty4 |    ts4 |    tm4 |
|-----|-----|------|-----|-----|-----|------|-----|-----|-----|------|-----|-----|--------|--------|--------|--------|
|  99 |  CT |    1 |   1 |   6 |   E |    3 |   1 |   5 |  GR |    4 |   1 |   6 | (null) | (null) | (null) | (null) |

@ughai非常感谢您的任何建议和建议,因为我是一个新手,非常感谢您的朋友,这正是我所寻求的。