如指定PostID为4的值查询后面的三个字段值为1的字段,后面三个字段不指定.可能以后不至这三个字段.
AwardsID    PostID      质量奖   安全奖   考勤奖
----------- ----------- ----- ----- -----
1           4           NULL  1     NULL
2           2           NULL  1     NULL
3           2           NULL  NULL  1
4           3           NULL  NULL  1

解决方案 »

  1.   

    select * from table1 where PostID=4 and (质量奖+安全奖+考勤奖)=3
      

  2.   

    select * from table1 where PostID=4 and 质量奖 =1 and 安全奖=1 and 考勤奖=1
      

  3.   

    如查询了PostID=4 结果要是这样
    PostID 安全奖
    ------ -----
      4     1其它的没有就不查询出来
      

  4.   


    declare @PostID int
    set  @PostID=4
    select * from table1 where PostID=@PostID and (质量奖+安全奖+考勤奖)=3--其它條件為固定條件就行了,或用動態判斷列名
      

  5.   

    Msg 8117, Level 16, State 1, Line 3
    操作数数据类型 bit 对于 add 运算符无效。报错,我不明白为什么这样.但感觉不是我要的结果
      

  6.   

    Msg 8117, Level 16, State 1, Line 3
    操作数数据类型 bit 对于 add 运算符无效。
    ----------------
    把轉換一下declare @PostID int
    set  @PostID=4
    select * from table1 where PostID=@PostID and (cast(质量奖 as smallint)+安全奖+考勤奖)=3
      

  7.   


    不是我要的结果
    如查询了PostID=4 结果要是这样
    PostID 安全奖
    ------ -----
    4        1
      

  8.   

    这个貌似是你想要的.
    create table tb(AwardsID int,PostID int,质量奖 int,安全奖 int,考勤奖 int)
    insert into tb select 1,4,NULL,1,NULL
    insert into tb select 2,2,NULL,1,NULL
    insert into tb select 3,2,NULL,NULL,1
    insert into tb select 4,3,NULL,NULL,1
    go
    declare @id int,@s as nvarchar(30)
    set @id=3   --改变这个可以改变所引用的列,但众多列中只选第一个不为NULL的.
    declare @str nvarchar(4000)
    select @str=isnull(@str+'+','')+'ltrim(isnull('+a.name+',0))' from syscolumns a inner join sysobjects b on a.id=b.id where b.type='U' and b.name='tb' and a.colid>2 order by a.colid
    set @str='select @s='+@str+' from tb where AwardsID='+ltrim(@id)
    exec sp_executesql @str,N'@s nvarchar(30) output ',@s output
    select @str='select AwardsID,PostID,'+a.name+' from tb where AwardsID='+ltrim(@id) from syscolumns a inner join sysobjects b on a.id=b.id where b.type='U' and b.name='tb' and a.colid=2+charindex('1',@s)
    exec (@str)
    /*
    AwardsID    PostID      考勤奖
    ----------- ----------- -----------
    3           2           1(1 行受影响)*/
    go
    drop table tb
      

  9.   

    程序中的 @id=3 是指表中 AwardsID 为 3 的行.
      

  10.   

    exec sp_executesql 
    from syscolumns a inner join sysobjects b on a.id=b.id where b.type='U' and b.name='tb' and a.colid=2+charindex('1',@s)
    有点不是很明白这个意思系统列表系统对象表还有type=U这是什么意思还得麻烦你告诉我一下你的思路.你说的是我要的结果.但没看明白你的思路
      

  11.   

    晴天的SQL成功的前提是:楼主所谓的PostID后面的N个字段必须在建表时物理结构就是在PostID的后面,而且除了这些用于统计的字段不能再有其他任何字段。
    我觉得这么搞没什么意思,毕竟这样的限制对于现有系统来讲只能是拼人品。
    可通过xml配置或数据库表配置,将每个表需要统计的字段写到xml或配置表中,查询的时候再根据表名来取出这些字段然后拼成SQL用EXEC来执行
      

  12.   

    用动态语句来实现,先取出表的所有字段,再根据字段类型拼接成SQL语句执行
      

  13.   

    我建表时创建这两个字段
    AwardsID    PostID.后面的N个字段由用户自已添加.
      

  14.   

    晴天.我刚才测试出不是我要的结果了
    create table tb(AwardsID int,PostID int,质量奖 int,安全奖 int,考勤奖 int)
    insert into tb select 1,4,1,1,NULL
    insert into tb select 2,2,NULL,1,NULL
    insert into tb select 3,2,1,NULL,1
    insert into tb select 4,3,NULL,1,1这样的话,只显示第一列质量.后面任意列不会.只会检查第一个列是否为null了.有没有办法任意呢?
      

  15.   

    --下面这个不限制列数的
    create table tb(AwardsID int,PostID int,质量奖 int,安全奖 int,考勤奖 int)
    insert into tb select 1,4,1,1,NULL
    insert into tb select 2,2,NULL,1,NULL
    insert into tb select 3,2,1,NULL,1
    insert into tb select 4,3,NULL,1,1
    declare @sql nvarchar(4000)
    declare @PostID int
    set @PostID=2
    declare @var nvarchar(4000)
    select @sql=isnull(@sql+'+','')+'case when sum('+name+')>=0 then '+quotename(','+name,'''')+' else '''' end ' from syscolumns where id=object_id('tb') and name not in ('AwardsID','PostID')
    set @sql='select  @a='+@sql+' from tb where PostID='+ltrim(@PostID)
    print (@sql)
    exec sp_executesql @sql,N'@a varchar(4000) output',@var outputset @sql='select AwardsID,PostID'+@var+' from tb where PostID='+ltrim(@PostID)
    exec(@sql)
      

  16.   


    谢谢
    那我把任意列的值改成这样该怎么改.
    select case (select a.name from tb where PostID = @id) when 1 then (select Amount from Awards where AwardsName=a.name) end也就是值为1的时候查询AwardsName表的Amount值
      

  17.   

    那我把任意列的值改成这样该怎么改.
    select case (select a.name from tb where PostID = @id) when 1 then (select Amount from Awards where AwardsName=a.name) end也就是值为1的时候查询AwardsName表的Amount值
    你上面是select a.name from tb where PostID = @id when 1
    就是name=1吗?没看懂得你语句的意思,
    另外这句话“也就是值为1的时候查询AwardsName表的Amount值”
    是指什么值为1的时候候查询AwardsName表的Amount值?
      

  18.   

    select case (select a.name from tb where PostID = @id) when 1 then (select Amount from Awards where AwardsName=a.name) end红色部分的a.name是整型的,它可以等于整数1,是吗?
    蓝色部分的a.name是什么意思?
    注意:红色部分的a.name查询的值应该是一个值,不能是多个
    同样:select Amount from Awards where AwardsName=a.name,
       查询后的Amount也应该是一个值,不能是多个值,
    否则都会报错
      

  19.   

    set @sql='select AwardsID,PostID'+@var+' from tb where PostID='+ltrim(@PostID)
    也就是@var值为1时查询AwardsName表相应的Amount值如@var值为质量奖 
    就查询Awards表里面AwardsName为质量奖的Amount值
      

  20.   

    这上面的@var值是一个字段组合列表,如'质量奖,安全奖',并不是@var值为1时