0

我有这样的查询结果:

Date       User1 User2 User3 ....
----------------------------------
1/1/2000   55     78    98   ...
1/1/2001   26     33    56   ...
1/1/2002   88     67    12   ...

列数未知,因为它是数据透视查询的结果。

我想将列的名称更改为如下所示:

Date    User1 (blue)  User2 (green)  User3(brown)

颜色是我从另一个表中检索到的信息。

我怎样才能做到这一点?

谢谢

编辑:这是查询。

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

select @cols = STUFF((SELECT distinct ',' + QUOTENAME(C.Name)  
                    from [History]



            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')
set @query = 'SELECT [Date],' + @cols +' 
             from 
             (
                select [Date], Name, Value
                from [History]


             ) x
            pivot 
            (
                max(value)
                for Name in (' + @cols + ')
            ) p '

execute(@query)
4

3 回答 3

1

SQL小提琴

架构设置

create table history (date datetime, name varchar(10), value int);
insert history values
 ('20130101', 'user1', 123),
 ('20130101', 'user2', 124),
 ('20130101', 'user3', 125),
 ('20130102', 'user1', 223),
 ('20130102', 'user3', 223),
 ('20130103', 'user2', 323);

create table colours (name varchar(10), colour_name varchar(10));
insert colours values
 ('user1', 'blue'),
 ('user2', 'green'),
 ('user3', 'brown');

查询

DECLARE @scols nvarchar(max),
        @cols AS NVARCHAR(MAX),
        @query AS NVARCHAR(MAX);

select @cols = STUFF((
  SELECT ',' + QUOTENAME(C.Name)  
  from (select distinct name from [History]) C
  ORDER BY C.Name
  FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'),1,1,'');

select @scols = STUFF((
  SELECT ',' + QUOTENAME(Name) + ' AS ' + QUOTENAME(colour_Name)  
  from (select distinct c.name, x.colour_name
        from [History] C
        JOIN colours x on x.name = c.name) y
  ORDER BY Name
  FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'),1,1,'');

set @query = '
    SELECT [Date],' + @scols +' 
    from (
        select [Date], Name, Value
        from [History]
         ) x
    pivot 
         (
        max(value)
        for Name in (' + @cols + ')
         ) p ';

-- print @query --<< uncomment this line to see the query that gets generated
exec (@query);

结果

|                           DATE |   BLUE |  GREEN |  BROWN |
-------------------------------------------------------------
| January, 01 2013 00:00:00+0000 |    123 |    124 |    125 |
| January, 02 2013 00:00:00+0000 |    223 | (null) |    223 |
| January, 03 2013 00:00:00+0000 | (null) |    323 | (null) |
于 2013-04-24T09:48:25.283 回答
0
select Date, User1 as blue,User2 as green,User3 as brown from tableName

使用这样的查询。

使用 ' as' 关键字更改列名。

于 2013-04-24T07:05:20.833 回答
0

获取映射您可以使用旧列名的查找表到新列名,例如

CREATE TABLE colname(
  oldname varchar(20),
  newname varchar(20)
)

insert into colname values ( 'user1','user1 (blue)');
insert into colname values ( 'user2','user2 (green)');

然后您可以构建一个使用此映射的 sql 语句

declare @sq varchar(2000)
set @sq ='select date'
select @sq = @sq +  ',' + oldname + ' as [' + newname +']' from colname
set @sq = @sq + 'from ( existing query goes here ) ' 
select @sq

当@sq 中的 sql 看起来不错时,您可以将最后一个选择替换为

exec ( @sq ) 

运行查询

于 2013-04-24T07:45:55.003 回答