有个表如下:no     qty
1      3
1      4
2      1
2      2
3      5
3      4
4      3
--------------------------------
需要的结果:把no相同的qty * qty 
no      qty
1       12
2       2
3       20
4       3

解决方案 »

  1.   

    用程序或存储过程实现吧,一条SQL恐怕写不出来的
      

  2.   

    这种问题以前有很多类似的问题!
    这是以前的一个解决的帖子,跟你参考一下1
    有这样一个表: 
    人名 年 工资 
    a 2001 45544 
    a 2002 45455 
    a 2003 54566 
    b 2001 25455 
    b 2002 15455 
    b 2003 54566 
    怎么把2002年与2001的工资差额大于1000的记录查出来?? create table temp_calu( 
    e_name char(10), 
    wage_01 int,--01年工资 
    wage_02 int)--02年工资 将各个员工的人名,01年工资,02年工资放到这表里基可实现 delete from temp_calu -- 清除临时表原有记录***不可缺 
    insert into temp_calu(e_name) -- 为每个员工建立一条记录 
    select e_name from payment group by e_name --payment指你提供的这表 update temp_calu --为每个人插入01年工资 
    set wage_01 = 
    (select wage from payment 
    where payment.e_name = temp_calu.e_name and payment.year = 2001) update temp_calu --为每个人插入02年工资 
    set wage_02 = 
    (select wage from payment 
    where payment.e_name = temp_calu.e_name and payment.year = 2002) 求出结果: 
    select * from temp_calu 
    where abs(wage_02 - wage_01) > 1000 
      

  3.   

    select  distinct (a.qty*b.qty) as ji
    from dbo.TABLE1 a,dbo.TABLE1 b 
    where a.id<>b.id and  a.[no]=b.[no] 
    union
    select sum(qty) from dbo.TABLE1 group by no having count([no])=1在sql server 2000中通过
      

  4.   

    什么数据库?
    如果是sql server 2000,自定义函数是个很好的方法!
      

  5.   

    还是不写先了,要不不是sql server 2000,写了不是浪费!记得版主建议提问写清楚数据库的!