Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/26.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 Server中带where子句的透视_Sql_Sql Server_Sql Server 2008_Pivot - Fatal编程技术网

SQL Server中带where子句的透视

SQL Server中带where子句的透视,sql,sql-server,sql-server-2008,pivot,Sql,Sql Server,Sql Server 2008,Pivot,我需要在SQL中使用Pivot来转换列中的行,但我的Pivot查询无法这样做 create table TestTable ( id int, colVal int ) insert into TestTable values(1,1) insert into TestTable values(1,2) insert into TestTable values(1,4) insert into TestTable values(2,1) inse

我需要在SQL中使用Pivot来转换列中的行,但我的Pivot查询无法这样做

    create table TestTable
    (
      id int,
      colVal int
    )

insert into TestTable values(1,1)
insert into TestTable values(1,2)
insert into TestTable values(1,4)
insert into TestTable values(2,1)
insert into TestTable values(2,2)
insert into TestTable values(2,6)
我试图根据下面的查询where子句获取列中colVal的值

select * from
(
    Select ID,colVal
    from TestTable
    where ID=1
) as PV
pivot
(max(id) for colVal in([1], [2], [3])) piv
对于每个ID,只能有3个colvalue,因此我在pivot中指定了[1]、[2]、[3]

I am looking for output like
ID  c1 c2 c3
1   1  2  4
有人能帮我吗。

只需使用Row\u Number()创建列序列

select id,[1] as c1,[2] as c2,[3] as c3 from
(
    Select ID
          ,col    = row_number() over (Partition By ID Order by colVal)
          ,colVal
    from TestTable
    where ID=1
) as PV
pivot
(max(colVal) for col in([1], [2], [3])) piv
返回

ID  c1  c2  c3
1   1   2   4
编辑-动态版本

为2008年编辑


这真是太棒了!!!如果您能在下面的查询中解释我,我将非常高兴:
col=row_number()(按ID划分,按colVal排序)
还有一件事,如果“每个ID只能有3个colValues”发生更改怎么办。那么,我应该在透视列列表中进行更改吗?@Abhishek_Chourasia窗口函数非常有用,值得您花时间熟悉它们,如果您运行子查询(带或不带WHERE),您将看到列增量为1。现在,它将成为pivot@Abhishek_Chourasia如果列数增加。。。两种选择。1) 只需在轴上添加更多的列,或者2)您将需要动态。。稍后,我将发布一个动态版本“CONCAT”,在查询中给出一个错误,表示它不是内置函数名。我正在使用SQL2008R0。
Declare @SQL varchar(max) = Stuff((Select Distinct ',' + QuoteName(concat('C',row_number() over (Partition By ID Order by colVal))) 
                                    From  TestTable  
                                    Order By 1 
                                    For XML Path('')
                                  ),1,1,'') 
Select  @SQL = '
Select [id],' + @SQL + '
From (
        Select ID
              ,col    = concat(''C'',row_number() over (Partition By ID Order by colVal))
              ,colVal
        from TestTable
        where ID=1
     ) A
 Pivot (max(colVal) For [col] in (' + @SQL + ') ) p'
Exec(@SQL);
Declare @SQL varchar(max) = Stuff((Select Distinct ',' + QuoteName('C'+cast(row_number() over (Partition By ID Order by colVal) as varchar(10))) 
                                    From  TestTable  
                                    Order By 1 
                                    For XML Path('')
                                  ),1,1,'') 
Select  @SQL = '
Select [id],' + @SQL + '
From (
        Select ID
              ,col    = ''C''+cast(row_number() over (Partition By ID Order by colVal) as varchar(10))
              ,colVal
        from TestTable
        where ID=1
     ) A
 Pivot (max(colVal) For [col] in (' + @SQL + ') ) p'
Exec(@SQL);