行的内容变换到列显示,并且添加行求和,列求和
来源:互联网 发布:网络教育全托 编辑:程序博客网 时间:2024/05/21 23:37
StoreCode GoodsCode Size Color Num
s1 g1 165 01 2
s1 g2 170 01 1
s1 g1 170 02 3
s2 g1 165 01 4
s2 g2 170 01 5
......
显示结果如下:
GoodsCode Size Color s1 s2 ColSum
g1 165 01 2 4 6
g1 170 02 3 0 3
g2 170 01 1 5 6
6 9 15
也有可能这样显示
GoodsCode s1 s2 ColSum
g1 5 4 9
g2 1 5 6
null 6 9 15
解答
Insert TEST
Select 's1','g1','165','01','2'
Union All
Select 's1','g2','170','01','1'
Union All
Select 's1','g1','170','02','3'
Union All
Select 's2','g1','165','01','4'
Union All
Select 's2','g2','170','01','5'
Union All
Select 's3','g2','170','01','5'
Union All
Select 's4','g3','170','01','5'
--测试
Declare @S Varchar(1000)
Set @S='Select GoodsCode'
Select @S=@S+','+'SUM(Case StoreCode When '''+StoreCode+''' Then Num Else 0 End) As '+StoreCode
from (Select Distinct StoreCode from TEST ) A Order by StoreCode
Set @S=@S+' ,SUM(Num) As ColSum from TEST Group By GoodsCode With Rollup '
print @s
EXEC(@S)
--删除测试环境
Drop Table TEST
--结果
/**//*
GoodsCode s1 s2 ColSum
g1 5 4 9
g2 1 5 6
NULL 6 9 15
*/
- 行的内容变换到列显示,并且添加行求和,列求和
- 列的累加求和
- python中pandas.DataFrame对行与列求和及添加新行与列示例
- python中pandas.DataFrame对行与列求和及添加新行与列示例
- 【pandas】对矩阵的某一行、某一列进行求和
- oracle中行,列的转换(列求和)
- GridView指定列显示其他表求和后的数值
- gridview某列求和后显示
- 两个相邻列求和
- ASP.NET 列求和
- GridView指定列求和
- linux 文件列求和
- DataTable中列求和的方法
- EXCEL自动填充列索引号 && EXCEL的列求和、行求和 && 如何获得网页上的歌曲 && EXCEL第一行锁定 && EXCEL曲线图 && WORD行间距规整
- 矩阵按行(列)求和CUDA并行算法设计
- [shell/awk]按列求和
- shell awk 多列求和
- shell-awk 按列求和
- 靠自己
- There you go 就这样了
- [英语阅读]The Baby Eagle 小鹰的故事
- 如何下班早回家
- SQL基本语句(Insert,Select,Update和Delete)
- 行的内容变换到列显示,并且添加行求和,列求和
- 站在人生的十字路口
- MSI Windows Installer 被破坏后引起软件无法安装的解决
- 第二人生的源码分析(9)登录界面显示
- [12-28][Visual Studio 2005 Team Suite 集成 SP1 MSDN2][BT][5.5G][ISO][15天]
- jsp中按钮的submit 和 button类型
- 推荐几本JAVA的书
- 老掉牙:成员变量与成员方法的讨论
- Ajax 同一页面同时执行多个 XMLHTTP