问一个简单的查询语句(100分)

  • 主题发起人 主题发起人 蜉蝣小生
  • 开始时间 开始时间

蜉蝣小生

Unregistered / Unconfirmed
GUEST, unregistred user!
数据库中有三条记录<br>字段1 &nbsp; &nbsp;字段2 &nbsp; &nbsp;字段3<br>A &nbsp; &nbsp; &nbsp; &nbsp;1 &nbsp; &nbsp; &nbsp; &nbsp; B<br>A &nbsp; &nbsp; &nbsp; &nbsp;3 &nbsp; &nbsp; &nbsp; &nbsp; C<br>A &nbsp; &nbsp; &nbsp; &nbsp;5 &nbsp; &nbsp; &nbsp; &nbsp; D<br>我想用汇总查出以下的结果<br>字段1 &nbsp; &nbsp;字段2 &nbsp; 字段3<br>A &nbsp; &nbsp; &nbsp; &nbsp; 9 &nbsp; &nbsp; &nbsp; B/C/D<br>请问这样的SQL 语句应该怎么写
 
用SQL查?關注此問題
 
写一个函数调用
 
自定义函数<br>create &nbsp; function &nbsp; f_union(@id &nbsp; varchar(20)) &nbsp; <br>&nbsp; returns &nbsp; varchar(100) &nbsp; <br>&nbsp; as &nbsp; <br>&nbsp; begin &nbsp; <br>&nbsp; &nbsp; &nbsp; declare &nbsp; @sql &nbsp; varchar(100) &nbsp; <br>&nbsp; &nbsp; &nbsp; set &nbsp; @sql='' &nbsp; <br>&nbsp; &nbsp; &nbsp; select &nbsp; @sql=@sql+'/'+字段3 &nbsp;from &nbsp; test &nbsp; where &nbsp; 字段1=@id &nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp; return(stuff(@sql,1,1,'')) &nbsp; <br>&nbsp; end &nbsp; <br>&nbsp; go &nbsp; <br>--调用自定义函数得到结果: &nbsp; <br>&nbsp; select &nbsp; 字段1 ,sum(字段2), a=dbo.f_union(字段1)as 字段3 &nbsp; &nbsp; from &nbsp; test &nbsp;group by &nbsp;字段1
 
楼上可以,来晚了<br>自定义函数<br>create &nbsp; function &nbsp; f_union(@id &nbsp; varchar(20)) &nbsp; <br>&nbsp; returns &nbsp; varchar(100) &nbsp; <br>&nbsp; as &nbsp; <br>&nbsp; begin &nbsp; <br>&nbsp; &nbsp; &nbsp; declare &nbsp; @sql &nbsp; varchar(100) &nbsp; <br>&nbsp; &nbsp; &nbsp; set &nbsp; @sql='' &nbsp; <br>&nbsp; &nbsp; &nbsp; select &nbsp; @sql=@sql+'/'+ [red]ltrim(字段3)[/red] &nbsp;from &nbsp; test &nbsp; where &nbsp; 字段1=@id &nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp; return(stuff(@sql,1,1,'')) &nbsp; <br>&nbsp; end &nbsp; <br>&nbsp; go &nbsp; <br>--调用自定义函数得到结果: &nbsp; <br>&nbsp; select &nbsp; 字段1 ,sum(字段2), [red]dbo.f_union(字段1)as 字段3[/red] &nbsp; &nbsp; from &nbsp; test &nbsp;group by &nbsp;字段1
 
字段二是什么类型呀
 
多人接受答案了。
 
后退
顶部