google “sqlserver 行转列”
或者写个存储过程,通过游标遍历委托方,拼接sql,组成你想要的数据集
或者写个存储过程,通过游标遍历委托方,拼接sql,组成你想要的数据集
解决方案 »
- 如何读取SQL Server内容至TextBox
- 菜鸟弱弱的问:System.NullReferenceException: 未将对象引用设置到对象的实例
- 如何用C#在窗体上显示SQL数据库?
- 请问 怎样给GridView每行增加一个序号?
- 一个比较菜鸟级的问题,帮忙指点~~ 谢谢各位~~~
- 怎么样获取文本的行数啊???
- 并行生成文件冲突问题
- 简单小问题!错误 13 未能找到类型或命名空间名称“Feature”(是否缺少 using 指令或程序集引用?)
- 两个.aspx文件,相同的命名空间,是否可以调用对方的服务器端控件?
- 用了Gautil安装了一些dll后,在哪里看安装了哪些DLL,如何反安装
- C#编写动态网页,Update语句报错
- C#WPF用代码新建文本框不能实现拖动问题
create table tb(name varchar(10) , kch varchar(10) , fshu int)
insert into tb values('张三' , '语文' , 74)
insert into tb values('张三' , '数学' , 83)
insert into tb values('张三' , '物理' , 93)
insert into tb values('李四' , '语文' , 74)
insert into tb values('李四' , '数学' , 84)
insert into tb values('李四' , '物理' , 94)
GO--姓名 语文 数学 物理
--李四 74 84 94
--张三 74 83 93
select name as 姓名 ,
max(case kch when '语文' then fshu else 0 end) 语文,
max(case kch when '数学' then fshu else 0 end) 数学,
max(case kch when '物理' then fshu else 0 end) 物理
from tb
group by NAME
把读出来的DataTable解析到自定义的表中
Consignor 票数 应收 应付 利润 进出口
青岛川汇国际物流有限公司 28 6851.0000 4153.6400 2697.3600 0
青岛正乐食品有限公司 30 2438.0000 536.3600 1901.6400 0
青岛正乐食品有限公司 30 720.0000 139.9200 580.0800 1
万华化学集团股份有限公司 8 840.0000 186.5600 653.4400 0
青岛中远国际货运有限公司 253 98838.0000 26353.2800 72484.7200 0
青岛中远国际货运有限公司 253 6020.0000 146.6400 5873.3600 1
青岛中远国际货运有限公司济南分公司 18 2398.3200 373.1200 2025.2000 0
把读出来的DataTable解析到自定义的表中
(
Consignor varchar(100),
票数 int,
应收 decimal(18,4),
应付 decimal(18,4),
利润 decimal(18,4),
进出口 varchar(1)
)
--Consignor 票数 应收 应付 利润 进出口
select '青岛川汇国际物流有限公司', 28 ,6851.0000, 4153.6400, 2697.3600, '0'
union all
select '青岛正乐食品有限公司' ,30, 2438.0000, 536.3600, 1901.6400, '0'
union all
select '青岛正乐食品有限公司', 30 ,720.0000 ,139.9200 ,580.0800 ,'1'
select '万华化学集团股份有限公司', 8, 840.0000, 186.5600, 653.4400, '0'
union all
select '青岛中远国际货运有限公司' ,253 ,98838.0000 ,26353.2800 ,72484.7200 ,'0'
union all
select '青岛中远国际货运有限公司' ,253, 6020.0000, 146.6400, 5873.3600 ,'1'
union all
select '青岛中远国际货运有限公司济南分公司', 18 ,2398.3200, 373.1200 ,2025.2000 ,'0'
--(
--Consignor varchar(100),
--票数 int,
--应收 decimal(18,4),
--应付 decimal(18,4),
--利润 decimal(18,4),
--进出口 varchar(1)
--)
--INSERT INTO #temp
----Consignor 票数 应收 应付 利润 进出口
--select '青岛川汇国际物流有限公司', 28 ,6851.0000, 4153.6400, 2697.3600, '0'
--union all
--select '青岛正乐食品有限公司' ,30, 2438.0000, 536.3600, 1901.6400, '0'
--union all
--select '青岛正乐食品有限公司', 30 ,720.0000 ,139.9200 ,580.0800 ,'1'
--UNION ALL
--select '万华化学集团股份有限公司', 8, 840.0000, 186.5600, 653.4400, '0'
--union all
--select '青岛中远国际货运有限公司' ,253 ,98838.0000 ,26353.2800 ,72484.7200 ,'0'
--union all
--select '青岛中远国际货运有限公司' ,253, 6020.0000, 146.6400, 5873.3600 ,'1'
--union all
--select '青岛中远国际货运有限公司济南分公司', 18 ,2398.3200, 373.1200 ,2025.2000 ,'0'--SELECT consignor,票数,利润,进出口 FROM #tempdeclare @s nvarchar(MAX)
DECLARE @s1 NVARCHAR(max)
set @s=''
SET @s1=''
Select @s=@s+','+quotename(consignor)+'=sum(case when [consignor]='+quotename(consignor,'''')+' and [进出口]='+quotename(进出口,'''')+' then [票数] else 0 end)'
--+','+quotename(consignor)+'=sum(case when [consignor]='+quotename(consignor,'''')+' and [进出口]='+quotename(进出口,'''')+' then [利润] else 0 end)'
from #temp group by [进出口],consignor
Select @s1=@s1+','+quotename(consignor)+'=sum(case when [consignor]='+quotename(consignor,'''')+' and [进出口]='+quotename(进出口,'''')+' then [利润] else 0 end)'
--+','+quotename(consignor)+'=sum(case when [consignor]='+quotename(consignor,'''')+' and [进出口]='+quotename(进出口,'''')+' then [利润] else 0 end)'
from #temp group by [进出口],consignor
exec('select [进出口],''票数'' '+@s+' from #temp group by [进出口] union all select [进出口],''利润'''+@s1+' from #temp group by [进出口] order by 进出口 ')/*
进出口 青岛川汇国际物流有限公司 青岛正乐食品有限公司 青岛中远国际货运有限公司 青岛中远国际货运有限公司济南分公司 万华化学集团股份有限公司 青岛正乐食品有限公司 青岛中远国际货运有限公司
---- ---- --------------------------------------- --------------------------------------- --------------------------------------- --------------------------------------- --------------------------------------- --------------------------------------- ---------------------------------------
0 利润 5394.7200 3803.2800 72484.7200 2025.2000 653.4400 0.0000 0.0000
0 票数 56.0000 60.0000 253.0000 18.0000 8.0000 0.0000 0.0000
1 利润 0.0000 0.0000 0.0000 0.0000 0.0000 1160.1600 5873.3600
1 票数 0.0000 0.0000 0.0000 0.0000 0.0000 60.0000 253.0000*/
sql 列转行 遇到 的值 有 括号怎么办 啊
declare @s nvarchar(MAX)
DECLARE @s1 NVARCHAR(max)
set @s=''
SET @s1=''
Select @s=@s+','+quotename(consignor)+'=MAX(case when [consignor]='+quotename(consignor,'''')+' and [进出口]='+quotename(进出口,'''')+' then [票数] else 0 end)'
--+','+quotename(consignor)+'=sum(case when [consignor]='+quotename(consignor,'''')+' and [进出口]='+quotename(进出口,'''')+' then [利润] else 0 end)'
from #temp group by [进出口],consignor
Select @s1=@s1+','+quotename(consignor)+'=MAX(case when [consignor]='+quotename(consignor,'''')+' and [进出口]='+quotename(进出口,'''')+' then [利润] else 0 end)'
--+','+quotename(consignor)+'=sum(case when [consignor]='+quotename(consignor,'''')+' and [进出口]='+quotename(进出口,'''')+' then [利润] else 0 end)'
from #temp group by [进出口],consignor
exec('select [进出口],''票数'' '+@s+' from #temp group by [进出口] union all select [进出口],''利润'''+@s1+' from #temp group by [进出口] order by 进出口 ')