一个项目需要一个变态(对我来说)的多表查询,首先先建了个视图名叫SelectProduct,总算把几个表连起来了,查询结果:
ID ProductName AttributeName AttributeValue
1 钻戒1 颜色 无色
1 钻戒1 净度 完全洁净
2 钻戒2 颜色 完全无色
2 钻戒2 净度 内部洁净但我想要的结果是这种:产品名称           颜色    净度
钻戒1   无色 完全洁净
钻戒2   完全无色          内部洁净然后再论坛里搜帖子,模仿写了如下代码:declare @sql varchar(8000)
set @sql = 'select ID,ProductName 产品名称'
select @sql = @sql + ' , (case AttributeName when ''' + AttributeName+ ''' then AttributeValue  end) [' + AttributeName + ']'
 from (select distinct AttributeName from SelectProduct) as a
set @sql = @sql + ' from a group by ProductName,ID'
exec(@sql) 但是执行的时候提示错误:对象名  'a' 无效。
有哪位高手可以帮我看看怎么回事儿吗?谢谢了

解决方案 »

  1.   

    ' from a group by ProductName,ID'
    把这段改成:
    ' from 表名 group by ProductName,ID'
      

  2.   

    if object_id('[SelectProduct]') is not null drop table [SelectProduct]
    go
    create table [SelectProduct]([ID] int,[ProductName] varchar(5),[AttributeName] varchar(4),[AttributeValue] varchar(8))
    insert [SelectProduct]
    select 1,'钻戒1','颜色','无色' union all
    select 1,'钻戒1','净度','完全洁净' union all
    select 2,'钻戒2','颜色','完全无色' union all
    select 2,'钻戒2','净度','内部洁净'declare @sql varchar(8000)
    set @sql = 'select ID,ProductName 产品名称'
    select @sql = @sql + ' , max(case AttributeName when ''' + AttributeName+ ''' then AttributeValue  end) [' + AttributeName + ']'
     from (select distinct AttributeName from SelectProduct) as a
    set @sql = @sql + ' from SelectProduct group by ProductName,ID'
    exec(@sql) /**
    ID          产品名称  净度       颜色
    ----------- ----- -------- --------
    1           钻戒1   完全洁净     无色
    2           钻戒2   内部洁净     完全无色
    **/
      

  3.   

    ----------------------------------------------------------------------------------
    -- Author : htl258(Tony)
    -- Date   : 2010-05-19 21:13:23
    -- Version: Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86) 
    --          Jul  9 2008 14:43:34 
    --          Copyright (c) 1988-2008 Microsoft Corporation
    --          Developer Edition on Windows NT 5.1 <X86> (Build 2600: Service Pack 2)
    -- Blog   : http://blog.csdn.net/htl258
    ------------------------------------------------------------------------------------> 生成测试数据表: [SelectProduct]
    IF OBJECT_ID('[SelectProduct]') IS NOT NULL
    DROP TABLE [SelectProduct]
    GO
    CREATE TABLE [SelectProduct] ([ID] [int],[ProductName] [nvarchar](10),[AttributeName] [nvarchar](10),[AttributeValue] [nvarchar](10))
    INSERT INTO [SelectProduct]
    SELECT '1','钻戒1','颜色','无色' UNION ALL
    SELECT '1','钻戒1','净度','完全洁净' UNION ALL
    SELECT '2','钻戒2','颜色','完全无色' UNION ALL
    SELECT '2','钻戒2','净度','内部洁净'--SELECT * FROM [SelectProduct]-->SQL查询如下:
    declare @sql varchar(8000)
    set @sql = 'select ID,ProductName 产品名称'
    select @sql = @sql + ' ,max(case [AttributeName] when ''' + AttributeName+ ''' then AttributeValue  end) [' + AttributeName + ']'
     from (select distinct AttributeName from SelectProduct) as a
    set @sql = @sql + ' from [SelectProduct] group by ProductName,ID'
    EXEC(@sql) 
    /*
    ID          产品名称       净度         颜色
    ----------- ---------- ---------- ----------
    1           钻戒1        完全洁净       无色
    2           钻戒2        内部洁净       完全无色(2 行受影响)
    */
      

  4.   

    谢谢2楼的,不过照你说的改成了视图名,变成这样了啊ID ProductName AttributeName AttributeValue
    1 钻戒1 完全洁净          NULL
    1 钻戒1 NULL 无色
    2 钻戒2 内部洁净          NULL
    2 钻戒2 NULL 完全无色
      

  5.   

    晕,可以了,就是不知道为啥要加个max呢??费解…………
      

  6.   

    没聚合GORUP BY 出来就会像你5楼的效果,慢慢就会理解的。
      

  7.   

    select  id,productname,
    [颜色]=(select top 1 attributeValue from selectproduct where attributename='颜色'),
    [净度]=(select top 1 attributeValue from selectproduct where attributename='净度')
    from 
    (select distinct id,productname from SelectProduct) as a
      

  8.   

    你print(@sql)看静态语句就明白了.
      

  9.   

    一、行转列
    if not object_id('class') is null
     drop table class
    go
    create table class(sname varchar(10),cname varchar(10),score int)Insert class
    select N'张三',N'语文',78 union all
    select N'张三',N'数学',87 union all
    select N'张三',N'英语',82 union all
    select N'张三',N'物理',90 union all
    select N'李四',N'语文',65 union all
    select N'李四',N'数学',77 union all
    select N'李四',N'英语',65 union all
    select N'李四',N'物理',85 --1、静态方法
    select 
        sname,
        [数学]=max(case when cname='数学' then score else 0 end),
        [物理]=max(case when cname='物理' then score else 0 end),
        [英语]=max(case when cname='英语' then score else 0 end),
        [语文]=max(case when cname='语文' then score else 0 end) 
    from 
        Class 
    group by sname
    --2、利用函数
    create function GetScore(@sname varchar(10),@cname varchar(10))
    returns int
    as
    begin
    declare @score int
    set @score=0
    select @score=score from class where sname=@sname and cname=@cname
    return @score
    endselect sname,语文=dbo.GetScore(sname,'语文'),数学=dbo.GetScore(sname,'数学'),
    物理=dbo.GetScore(sname,'物理'),英语=dbo.GetScore(sname,'英语')
    from class
    group by sname
    --3、动态
    declare @s nvarchar(4000)
    set @s=''
    Select     @s=@s+','+quotename(cname)+'=max(case when cname='+quotename(cname,'''')+' then score else 0 end)'
    from Class group by cname
    select @s
    exec('select sname'+@s+' from Class group by sname')
    --quotename使函数中的输入成为一个有效的标示符
    --4、利用pivot对表进行透视
    select * 
    from 
        Class 
    pivot 
        (max([Score]) for cname in([数学],[物理],[英语],[语文]))b
        
    --5、加上总成绩和平均成绩(静态方法)
    select *,[数学]+[物理]+[英语]+[语文] 总成绩,([数学]+[物理]+[英语]+[语文])/4 平均成绩
    from
    (
    select 
        sname 姓名,
        [数学]=max(case when cname='数学' then score else 0 end),
        [物理]=max(case when cname='物理' then score else 0 end),
        [英语]=max(case when cname='英语' then score else 0 end),
        [语文]=max(case when cname='语文' then score else 0 end)
    from 
        Class 
    group by sname) bselect 
        sname 姓名,
        [数学]=max(case when cname='数学' then score else 0 end),
        [物理]=max(case when cname='物理' then score else 0 end),
        [英语]=max(case when cname='英语' then score else 0 end),
        [语文]=max(case when cname='语文' then score else 0 end),
        [总成绩]=sum(score),
        [平均成绩]=avg(score)
    from 
        Class 
    group by sname
      

  10.   

    sql 2000动态sql
    declare @sql varchar(8000)
    set @sql = 'select ID,ProductName 产品名称'
    select @sql = @sql + ' , (case AttributeName when ''' + AttributeName+ ''' then AttributeValue  end) [' + AttributeName + ']'
     from (select distinct AttributeName from SelectProduct) as a
    set @sql = @sql + ' from 表名 group by ProductName,ID'
    exec(@sql) 
      

  11.   

    select ProductName 产品名称,[颜色]颜色,[净度]净度 from a
    pivot(max(AttributeValue) for AttributeName  in([颜色],[净度])) as pvt