[sql server] 问题总结9- 字符串拼接
来源:互联网 发布:虚拟充值退款淘宝介入 编辑:程序博客网 时间:2024/04/29 15:01
比如先想实现一个简单的
三条记录(或者更多条)
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] 问题总结9- 字符串拼接
- SQL SERVER:select 字符串拼接问题
- SQL Server 拼接字符串
- sql server 拼接字符串
- 关于sql server拼接字符串!
- SQL server 字符串变量拼接
- SQL字符串拼接 引号问题
- SQL字符串拼接 引号问题
- sql server FOR XML PATH 字符串拼接
- MySQL、SQL Server中字符串拼接
- SQL Server将一列的多行内容拼接成一行或一个字符串的问题
- SQL语句中和字符串的拼接问题
- oracle数据库拼接sql语句字符串问题
- SQL SERVER拼接字符串(字符串中有变量)
- SQL SERVER拼接字符串(字符串中有变量)
- sql语句字符串拼接的两种方法 SQL Server
- SQL Server 整型变量拼接字符串并打印
- SQL Server中将多行数据拼接为一个字符串
- struts2和struts1.x的标签库
- 内核,initrd,ramdisk,关系,用途
- 小记
- 第一天
- 复制字符串strcpy —— 编译器的诡异
- [sql server] 问题总结9- 字符串拼接
- linux(fedora)开机启动服务
- HashMap遍历的两种方式
- 套接口地址结构及IP端口转换函数简介
- 向量
- 新建自己的windows service
- Android自由的游戏开发框架
- type parameter, type argument & parameteried type
- 入库,出库数据,自动生成库存,简单实用的仓库管理表格或ERP系统.请发一份到我的邮箱545451757@qq.com.谢谢