--建立测试环境
Create Table T
(TID Int Identity(1,1),
 item1 Int,
 item2 Int,
 item3 Int,
 item4 Int)Create Table Q
(item Varchar(10),
 count Int)
--插入数据
Insert T Values(1,      0,       0,       1)
Insert T Values(0,      1,       1,       0)
Insert T Values(0,      0,       0,       1)
--测试
Insert Q
Select 'item1',SUM(item1) from T
Union All
Select 'item2',SUM(item2) from T
Union All
Select 'item3',SUM(item3) from T
Union All
Select 'item4',SUM(item4) from TSelect * from Q
--删除测试环境
Drop Table T,Q
--结果
/*
item count
item1 1
item2 1
item3 1
item4 2
*/

解决方案 »

  1.   

    这样的话,select语句也太多了吧,而且如果t表属性列比较多(上百个)那这么多的union会不会查询时间很长?还有没有更简单点的,效率高些的方法啊
      

  2.   

    还有啊,要是我想再加个条件,只在q表中插入sum(itemn)>=2的应该放在那
      

  3.   

    --测试数据
    if object_id('t') is not null drop table t
    go
    Create Table T
    (TID Int Identity(1,1),
     item1 Int,
     item2 Int,
     item3 Int,
     item4 Int)
    Insert T Values(1,      0,       0,       1)
    Insert T Values(0,      1,       1,       0)
    Insert T Values(0,      0,       0,       1)if object_id('q') is not null drop table q
    go
    create table q(item varchar(10), counts int)
    select* from t
    --定义一个游标进行逐列处理, 具体使用时,将t, q改为实际的表名就可以了
    declare @colname varchar(10)
    declare @str varchar(100)
    declare cur cursor
    for select name from syscolumns 
    where id=object_id('t') and colid>1
    set @str='insert q select '+char(0x27)+@colname+char(0x27)+
    ',sum('+@colname+') from t '
    open cur
    fetch cur into @colname
    while @@fetch_status=0
    begin
     set @str='insert q select '+char(0x27)+@colname+char(0x27)+
    ',sum('+@colname+') from t '
    -- print @str
     exec(@str)
    fetch cur into @colname
    end
    close cur
    deallocate curselect* from q
      

  4.   

    "还有啊,要是我想再加个条件,只在q表中插入sum(itemn)>=2的应该放在那"?
    是不是说,对于sum值小于2的列,不插入到Q表中?
      

  5.   

    因为你的要求是这样,没有办法。如果你不想写那么长的语句,可以写个动态语句来执行。--建立测试环境
    Create Table T
    (TID Int Identity(1,1),
     item1 Int,
     item2 Int,
     item3 Int,
     item4 Int)Create Table Q
    (item Varchar(10),
     count Int)
    --插入数据
    Insert T Values(1,      0,       0,       1)
    Insert T Values(0,      1,       1,       0)
    Insert T Values(0,      0,       0,       1)
    --测试
    Declare @S Varchar(1000)
    Set @S='Insert Q Select * from ('
    Select @S=@S+'Select '''+Name+''' As Item,SUM('+Name+') As Count from T Union All '
    from SysColumns Where ID=OBJECT_ID('T')  And ColID>1
    Select @S=Left(@S,Len(@S)-10)+' ) A'
    --Select @S=Left(@S,Len(@S)-10)+' ) A Where Count>=2' --如果要加上条件,就在这里加 
    EXEC(@S)Select * from Q
    --删除测试环境
    Drop Table T,Q
    --结果
    /*
    item count
    item1 1
    item2 1
    item3 1
    item4 2
    */
      

  6.   

    to paoluo(一天到晚游泳的鱼) :哦,谢谢,我再试试.实际上昨天我用for语句动态的生成了你说的那些,的确也插入q表.可是我又想起来还要加上sum()>=2的条件,我用group by 和having老是报错,所以又问了一次.不好意思,我实在是愚笨的很:)
    to filebat(Mark) :谢谢你的回复,可是我对游标很不熟悉,说实话你的回复我好多都看不懂,所以对我不太实用.不过非常感谢你给我回复
      

  7.   

    to 游鱼:呵呵,刚才吃饭的时候想了一下:准备用动态sql语句。现在上来一看,原来你就是这样做的。
    to 杰娜:看看这个可能对你有帮助
    select sum(item1) 
    from t 
    having sum(item1)>1