[sql server] 问题总结9- 字符串拼接
2011-03-07 19:31
330 查看
比如先想实现一个简单的
三条记录(或者更多条)
count price type
1 10 1
2 3 1
3 4 1
4 5 1
5 2 2
6 1 2
我想输出 最后的结果表达式 type1: (1*10)+(2*3)+(3*4)+(4*5)=48
type2: (5*2)+(6*1)=16
这个要怎么做才能分组显示出想要的字符串呢
------------------------
CREATE TABLE tb
(
pid int,
id int,
[type] int
)
insert tb
select 1, 10, 1 union all
select 2, 3, 1 union all
select 3, 4, 1 union all
select 4, 5, 1 union all
select 5, 2, 2 union all
select 6, 1, 2
go
-----------------------------
select ltrim(SUM(pid*id)) from tb group by type
select 'type'+ltrim([type])+stuff( (select '+('+ltrim(pid)+'*'+ltrim(id)+')' from tb where [type]=ta.[type] for xml path('')),1,1,'')+'='+ltrim(SUM(pid*id)) as col
from tb as ta
group by [type]
三条记录(或者更多条)
count price type
1 10 1
2 3 1
3 4 1
4 5 1
5 2 2
6 1 2
我想输出 最后的结果表达式 type1: (1*10)+(2*3)+(3*4)+(4*5)=48
type2: (5*2)+(6*1)=16
这个要怎么做才能分组显示出想要的字符串呢
------------------------
CREATE TABLE tb
(
pid int,
id int,
[type] int
)
insert tb
select 1, 10, 1 union all
select 2, 3, 1 union all
select 3, 4, 1 union all
select 4, 5, 1 union all
select 5, 2, 2 union all
select 6, 1, 2
go
-----------------------------
select ltrim(SUM(pid*id)) from tb group by type
select 'type'+ltrim([type])+stuff( (select '+('+ltrim(pid)+'*'+ltrim(id)+')' from tb where [type]=ta.[type] for xml path('')),1,1,'')+'='+ltrim(SUM(pid*id)) as col
from tb as ta
group by [type]
相关文章推荐
- SQL SERVER:select 字符串拼接问题
- SQL SERVER:select 字符串拼接问题
- [sql server] 问题总结13---分解字符串并查询相关数据
- SQL Server将一列的多行内容拼接成一行或一个字符串的问题
- SQL SERVER中一些常见性能问题的总结[转]
- Sql server一些常见性能问题的总结
- [sql server] 行转列问题总结1 - 行转列
- [转摘] JSP连接SQL SERVER问题总结
- js拼接字符串时,字符串首出现undefined的问题
- [转]SQL Server一些常见性能问题的总结
- 探讨js字符串数组拼接的性能问题
- SQL Server将一列的多行内容拼接成一行的问题讨论
- String,StringBuilder字符串拼接性能问题
- SQL Server 拼接字符串 [SQL Server]
- sql语句字符串拼接的两种方法 SQL Server
- 【转】[sql server] 行转列问题总结1 - 行转列
- python字符串拼接编码问题
- sprintf做字符串拼接的问题分析
- js拼接传递参数字符串问题
- SQL SERVER中一些常见性能问题的总结