--测试环境
Create table Test(dobillno varchar(10),billno int)
insert into Test select 'a',1
union all select 'b',1
union all select 'c',2
union all select 'd',2
union all select 'e',2
union all select 'f',3
--建函数
Create function F_GetStr(@billno int)
returns varchar(200)
as
begin
declare @return varchar(200)
set @return=''
select @return=@return+'/'+dobillno from Test where billno=@billno
return stuff(@return,1,1,'')
end
--查询
select 
billno,
billline=count(*),
dobillno=dbo.F_GetStr(billno)
from Test
group by billno--结果
billno      billline    dobillno  
1           2           a/b
2           3           c/d/e
3           1           f
--删除测试环境
drop table Test
Drop function F_GetStr(3 row(s) affected)

解决方案 »

  1.   

    我是用在VFP8.0里的,不可能把SQL查询写得这么复杂,有没有其他简便的方法?
      

  2.   

    很久没搞过VFP了,试试select 0
    create table c_test(billno n(10),billline n(10),dobillno c(100))
    use c_test
    select 0
    use yourtable
    insert c_test select billno,count(*),"" from yourtable group by billno order by billno
    s=""select c_test
    if !bof() then go 1 endif
    do while !eof()
      nRecno=recno()
      nBillno=billno
      select * from yourtable where billno=nbillno into cursor c_test1
      select c_test1
      do while !eof()
         s=s & dobillno & "/"
         skip
      enddo
      use c_test1
      select c_test
      update c_test set dobillno=left(s,len(s)-1)
      s=""
      skip
    enddo
    select c_test
    browse