例如:
select cNo,cColor,sum(qty) as qty from table1 group by cNo,cColor
这个结果中没有唯一值的列。
用select identityCol,cNo,cColor,sum(qty) as qty from table1 group by cNo,cColor时,提示不能有group by子句;如果用 identity(int,1,1)生成唯一列时得用into子句,也不想用此方法。
有无其他的办法呢?

解决方案 »

  1.   

    如果你不想用into ,是不是不喜欢创建一个新表?可以用临时表的方式:
    select identityCol,cNo,cColor,sum(qty) as qty into #t from table1 group by cNo,cColorselect * from #t这样也不会在你的数据库中留下实际的数据表。别的方法---------------不会!!!!!!期待高手!
      

  2.   

    哦!错了,应该是select identity(int,1,1) as dd,cNo,cColor,sum(qty) as qty into #t from table1 group by cNo,cColorselect * from #t
      

  3.   


    因为你group by中是两个字段值,所以这两个字段共同组成一个唯一值。没有单独的唯一字段,若要单独的就不能在group by中放置两个字段。在用select identityCol,cNo,cColor,sum(qty) as qty from table1 group by cNo,cColor时,中,你当然不能加上identity了
      

  4.   

    你的语句还有一个问题,就是如果要用一个唯一的cno字段的话那group by中就不能有两个字段,
    可以强制得到一个唯一字段,但会丢失数据。使
    得结果不完整。ccolor在前的那个组字段可以返回,其它的就只能是丢弃了