sql动态行转列
Ⅰ sql 行转列 如图想把左边的数据库结构改成右边的动态的,请问怎么用SQL实现
静态方法
SELECT [part_no],
SUM(CASE [createdate] WHEN '2015-07' THEN [qtt] ELSE 0 END) AS '2015-07',
SUM(CASE [createdate] WHEN '2015-08' THEN [qtt] ELSE 0 END) AS '2015-08'
FROM [Table]
GROUP BY [part_no]
动态方法
DECLARE @sql VARCHAR(8000)
SET @sql = 'SELECT [part_no],'
SELECT @sql = @sql + 'SUM(CASE [createdate] WHEN '''+[createdate]+''' THEN [Source] ELSE 0 END) AS '''+QUOTENAME([createdate])+''','
FROM (SELECT DISTINCT [createdate] FROM [createdate]) AS a
SELECT @sql = LEFT(@sql,LEN(@sql)-1) + ' FROM [createdate] GROUP BY [part_no]'
PRINT(@sql)
EXEC(@sql)
GO
Ⅱ sql中一对多关系的查询结果的多行转换成一行多列
--用动态sql实现行转列。因用到了row_number,只适用于sqlserver2005及以上版本
--测试数据
with
[user](ID,name,roleid)
as(
select1,'bobo','r1'unionall
select2,'coco','r1'unionall
select3,'dodo','r1'unionall
select4,'eoeo','r2'unionall
select5,'fofo','r2'),
[role](ID,name)
as(
select'r1','admin'unionall
select'r2','user')
--两表联合查询后暂存入临时表
selectb.IDroleid,b.namerolename,a.nameusername,row_number()over(partitionbyb.IDorderbya.ID)seq
into#t
from[user]a
innerjoin[role]bona.roleid=b.ID;
--拼接动态sql
declare@sqlvarchar(max);
set@sql='';
select@sql=@sql+
',max(caseseqwhen'+cast(tt.seqasvarchar)+'thenusernameelse''''end)user'+cast(tt.seqasvarchar)
from(selectdistinctseqfrom#t)tt
orderbyseq;
set@sql='selectrolename'+@sql+'from#tgroupbyroleid,rolename';
--打印动态sql
select@sql;
--执行动态sql
exec(@sql);
--删除临时表
droptable#t;
生成的动态sql为:
selectrolename,
max(caseseqwhen1thenusernameelse''end)user1,
max(caseseqwhen2thenusernameelse''end)user2,
max(caseseqwhen3thenusernameelse''end)user3
from#tgroupbyroleid,rolename
最终查询结果为:
Ⅲ sql 动态行转列只能写存储过程吗
如果使用的频率不高,可以不用写成存储过程,定义个临时变量执行下
如果使用频率高还是用存储过程把,sql代码涉及字符串拼接,建议用存储过程封装起来好。
Ⅳ sqlserver2008 特殊动态行转列 急!!!
你先把你上边的查询语句建立个视图吧
这个会吧?
我就直接拿你的数据建表了
createtablet(bf_org_shop_IDvarchar(10),
prod_IDvarchar(10),
sales_priceint,
sales_qtyint)insertintotvalues('单位1','aa',12,13)
insertintotvalues('单位1','bb',14,15)
insertintotvalues('单位2','aa',12,17)
insertintotvalues('单位2','bb',14,19)
insertintotvalues('单位3','aa',12,21)
insertintotvalues('单位3','bb',14,23)
执行
declare@sqlvarchar(4000)
set@sql='select[prod_ID],[sales_price]'
select@sql=@sql+',sum(isnull(case[bf_org_shop_ID]when'''+[bf_org_shop_ID]+'''then[sales_qty]end,0))as
['+[bf_org_shop_ID]+']'
from(selectdistinct[bf_org_shop_ID]from[t])asa
select@sql=@sql+'from[t]groupby[prod_ID],[sales_price]'
exec(@sql)
截图
Ⅳ sql语句行转列 怎么转啊
--声明变量
declare@sqlvarchar(1000),@num_dataint,@num_allvarchar(2000),@num_numint,@table_sqlvarchar(2000)
set@num_num=0
--判断并创建表
ifexists(select*fromdbo.sysobjectswhereid=object_id(N'[dbo].[records]')andOBJECTPROPERTY(id,N'IsUserTable')=1)
droptable[dbo].[records]
createtablerecords(
[id]int,
[name]varchar(50),
[sex]varchar(10),
[num]int
)
--插入数据
insertintorecordsvalues(1,'tom','男',2)
insertintorecordsvalues(1,'tom','男',3)
insertintorecordsvalues(1,'tom','男',4)
insertintorecordsvalues(1,'tom','男',5)
--全选表中数据
select*fromrecords
--全选num列数据
selectnumas'数据'fromrecords
--释放游标
deallocateselect_num
--为‘selectnumfromrecords’建立游标
declareselect_numscrollcursorforselectnumas'shuju'fromrecords
--打开游标
openselect_num
--获得第一条数据
fetchnextfromselect_numinto@num_data
set@num_all=convert(varchar,@num_data)+','
set@num_num=@num_num+1;
--如果获取成功,继续获得数据
while@@fetch_status=0
begin
fetchnextfromselect_numinto@num_data
set@num_num=@num_num+1;
set@num_all=@num_all+convert(varchar,@num_data)+','
end
--关闭游标
closeselect_num
print@num_num
--set@num_num=@num_num-1;
declare@iint
set@i=1
print@num_num
print@i
set@table_sql='createtablenumall(idint,namevarchar(50),sexvarchar(10)'
print@table_sql
while@num_num>=1
begin
set@table_sql=@table_sql+',num'+convert(varchar,@i)+'int'
set@num_num=@num_num-1;
set@i=@i+1
end
set@table_sql=@table_sql+')'
print@table_sql
ifexists(select*fromdbo.sysobjectswhereid=object_id(N'[dbo].[numall]')andOBJECTPROPERTY(id,N'IsUserTable')=1)
droptable[dbo].[numall]
exec(@table_sql)
declare@insert_sqlvarchar(2000)
set@insert_sql='insertintonumallvalues(1,'+'''tom'','+'''男'''
print@insert_sql
openselect_num
--获得第一条数据
fetchnextfromselect_numinto@num_data
set@insert_sql=@insert_sql+','+convert(varchar,@num_data)
--如果获取成功,继续获得数据
while@@fetch_status=0
begin
fetchnextfromselect_numinto@num_data
set@insert_sql=@insert_sql+','+convert(varchar,@num_data)
end
set@insert_sql=@insert_sql+')'
print@insert_sql
exec(@insert_sql)
--insertintonumallvalues(1,'tom','男',2,3,4,5,5)
select*fromnumall
试试吧,数据虽然有点出入,但已经说明问题了!!!
Ⅵ sql 2000 结合两张表“动态”实现 行转列
select
bdate,
max(case when area='保养一区' then 1 else 0 end),
max(case when area='保养二区' then 1 else 0 end),
max(case when area='保养三区' then 1 else 0 end),
……
from tablename
group by bdate
Ⅶ sql动态多行转列,PIVOT怎么能转两列
动态最好用存储过程(年月不确定,列名不确定),对sql进行拼接,再写一个第二条语句关于办结的,然后把这个受理与办结通过事项进行关联查询得到两列结果就好
Ⅷ sql server 行转列
创建测试表
createtabletest
(_keyvarchar(10),
_valuevarchar(10),
idint)
insertintotestvalues('ceshi','测试值',10)
insertintotestvalues('ceshi','测试值',11)
insertintotestvalues('ceshi2','测试值2',11)
执行
declare@sqlvarchar(4000)
set@sql='selectid'
select@sql=@sql+',max(case_keywhen'''+[_key]+'''then[_value]end)as
['+_key+']'
from(selectdistinct_keyfromtest)asa
select@sql=@sql+'fromtestgroupbyid'
exec(@sql)
结果
额,我那个第三条数据id写错了,不过方法还是这个
Ⅸ 急求 sql 动态行变列
CREATETABLETestRowCol(
nameVARCHAR(10),
placeVARCHAR(10),
valuwINT
);
INSERTINTOTestRowColVALUES('张三','东',1);
INSERTINTOTestRowColVALUES('张三','南',2);
INSERTINTOTestRowColVALUES('张三','西',3);
INSERTINTOTestRowColVALUES('张三','北',4);
INSERTINTOTestRowColVALUES('李四','东',5);
INSERTINTOTestRowColVALUES('李四','南',6);
INSERTINTOTestRowColVALUES('李四','西',7);
INSERTINTOTestRowColVALUES('李四','北',8);
注:下面的SQL是在SQLServer2005及以上版本可用。(SQLServer2000及以下版本的,下面的写法不支持)
使用CASEWHEN方式的动态处理.
BEGIN
--主SQL.
DECLARE@MainSqlvarchar(2000);
--产生动态列的SQL.
DECLARE@ColumnSqlvarchar(1000);
--设置产生动态列的SQL.
SET@ColumnSql=
(
SELECT
',SUM(CASEWHENplace='''+place+'''THENvaluwELSE0END)AS'+place
FROM
TestRowCol
GROUPBY
place
FORXMLPATH('')
);
--设置主SQL.
SET@MainSql=
'SELECTname'+@ColumnSql+'FROMTestRowColGROUPBYname';
--执行主SQL.
EXECUTE(@MainSql);
END
GO
name北东南西
------------------------------------------------------
李四8567
张三4123
使用PIVOT方式的动态处理.
BEGIN
--主SQL.
DECLARE@MainSqlvarchar(2000);
--产生动态列的SQL.
DECLARE@ColumnSqlvarchar(1000);
--设置产生动态列的SQL.
SET@ColumnSql=
STUFF(
(
SELECT
','+place
FROM
TestRowCol
GROUPBY
place
FORXMLPATH('')
),1,1,'');
--设置主SQL.
SET@MainSql=
'SELECT*FROMTestRowCol'
+'PIVOT(SUM(valuw)FORplaceIN('
+@ColumnSql
+'))tmpORDERBYname';
--执行主SQL.
EXECUTE(@MainSql);
END
GO
name北东南西
------------------------------------------------------
李四8567
张三4123
(2行受影响)
Ⅹ 怎样实现sql行转列
--试一试。。。
createtable#test
(
姓名nvarchar(10),
学号nvarchar(10),
题号nvarchar(10),
答案nvarchar(10)
)
go
insertinto#test
select'张三','001','1','A'unionall
select'张三','001','2','C'unionall
select'李四','002','1','B'unionall
select'李四','002','2','D'
select*from#test
dECLARE@sqlVARCHAR(max)
SET@sql=''
SELECT@sql=@sql+',['+题号+']'FROM#testGROUPBY题号
SET@sql=STUFF(@sql,1,1,'')
SET@sql='select*from#testpivot(max(答案)for题号in('+@sql+'))a'
PRINT@sql
exec(@sql)