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)