1,建自定义函数
Create function getstr(@id Nchar(100))
returns Nvarchar(4000)
as 
begin
declare @str Nvarchar(4000),@分隔符 Nvarchar(10)
set @分隔符=N','
set @str=N''
select @str=@str+rtrim(Col)+@分隔符 from 表名
--------------------------------^^^^要相加的字段名
where 相加条件字段=@id
set @str=left(@str,len(@str)-1)落 ---却除最后一个分隔符
return @str
end
GO2,调用
select 条件字段,dbo.getstr(条件字段) from 表名 group by 条件字段

解决方案 »

  1.   

    to  CrazyFor(冬眠的鼹鼠) 谢谢
    我用的是 sql7.0  用一个复杂的sql 能行吗?
      

  2.   

    假如所有重复的字段都一样可以这样:SELECT IDENTITY * FROM A
      

  3.   

    SELECT distinct * FROM A
      

  4.   

    select distinct * from A group by 第一个字段名
      

  5.   

    to DigJim(挖土)  不行。
      

  6.   

    噢,天哪,我又写错了!!
    SELECT distinct * FROM A
      

  7.   

    我也来凑个热闹,我也有一个SQL查询写不出来,请高手顺便看看。
    (1)***************************************************************** 
    select '长德' as '地区',
    count(distinct mt_id) as '有效卡数',
    sum(mt_consume_num) as '次数',
    sum(mt_consume_num)/count(distinct mt_id) as '频率',
    sum(mt_consume_num)/30 as '平均来客次数',
    sum(mt_consume_amt)/sum(mt_consume_num) as '平均客单价',
             sum(mt_consume_amt) as '总金额'
          from mem_personal,mem_transaction
             where mp_id=mt_id and mt_month='2003/10' and
         mt_id<>'03999999' and mt_id<>'03588888' and
         mem_personal.mp_address like '%长德%'
    该查询执行后结果为:
    地区 有效卡数   次数   频率   平均来客次数    平均客单价   总金额
    长德  555       2590    4.66   86.33            59.31      153631.80
    (2)**********************************************************************
        select '长德',count(mp_id) from mem_personal where mp_address like '%长德%'
      该查询执行的结果为:
    地区  总卡数
    长德   649(3)**************************************************************************
    而我想要这样的效果
    地区 有效卡数   次数   频率   平均来客次数    平均客单价   总金额      总卡数
    长德  555       2590    4.66   86.33            59.31      153631.80     649
    应该如何实现啊??????????????????????
      

  8.   

    select * from table1 where id (select id from table1 group by id )
      

  9.   

    to hunxin1(hu):
      第二个查询语句改为
    select *,count(mp_id) 总卡数 from mem_personal where mp_address like '%长德%'
      

  10.   

    select *,identity(int,1,1) as tid into #t from Aselect a.id,a.field2,a.field3 
    from #t a join 
         (  select id,min(tid) 
            from #t  
            group by id
         ) b on a.tid=b.tid
    godrop table #t
      

  11.   

    select * from t where id in (select id from t group by id)or:select * from t where id in (select distinct id from t)
      

  12.   

    --如果对于同一个字段1, 字段2的值不重复,可以用:
    select * from 表 a where 字段2=(select max(字段2 from 表 where 字段1=a.字段1)
    order by a
      

  13.   

    --下面是例子:
    --测试数据
    create table #t(a int,b varchar(10))
    insert into #t
    select 3,'** **'
    union all select 4,'** **'
    union all select 5,'** **'
    union all select 5,'**  **'
    union all select 5,'**  **'
    union all select 5,'**  **'
    union all select 6,'**  **'
    union all select 7,'** **'
    union all select 7,'**  **'--查询
    select * from #t a where b=(select max(b) from #t where a=a.a)
    order by a
    go
    --删除测试
    drop table #t/*--测试结果
    a           b          
    ----------- ---------- 
    3           ** **
    4           ** **
    5           ** **
    6           **  **
    7           ** **(所影响的行数为 5 行)
    --*/
      

  14.   

    --如果不满足这个条件,就得用临时表:
    select id=identity(int,1,1),* into #t from 表
    select 字段1,字段2 from #t a where id=(select max(id) from #t where 字段1=a.字段1)
    drop table #t
      

  15.   

    SELECT * FROM A tem where 列2=(select top 1 列2 from a where 列1=tem.列1)