『壹』 mssql2008將一列值轉換多行,不要用while循環,
CREATEFunctionSplit
(
@pvarchar(8000),
@dvarchar(10)
)
returns@tTable(tokenvarchar(100))
As
Begin
Declare@iInt
Set@p=RTrim(LTrim(@p))
Set@i=CharIndex(@d,@p)
While@i>=1
Begin
Insert@tValues(Left(@p,@i-1))
Set@p=SubString(@p,@i+1,Len(@p)-@i)
Set@i=CharIndex(@d,@p)
End
If@p<>''
Insert@tValues(@p)
Return
End
witht(id,name)as(select1,'a1:a2:a3:a4'
unionallselect2,'b1:b2:b3')
select*fromtcrossapplysplit(t.name,':')
『貳』 sqlserver怎麼行轉列
1,首先創建數據表
2,接著插入演示數據
3,需要把學生的每門成績集中到一行
4,編寫PIVOT函數實現
5,運行以後實現行轉列
6,最後還可以用case when來實現
『叄』 mssql根據條件數據列分解成多行數據,急
創建測試表
createtablet
(idint,
namevarchar(10),
typevarchar(10))
insertintotvalues(1,'蘋果','003')
insertintotvalues(2,'香蕉','A04')
insertintotvalues(3,'西瓜',NULL)
insertintotvalues(4,'石榴','002')
運行
selectt.*,t.name+s.name2name2fromtleftjoin
(selectdistincttype,
casewhenisnumeric(replace(type,0,''))=0then
left(replace(type,0,''),1)+cast(t1.idasvarchar)+'級'elsecast(t1.idasvarchar)+'級'endname2
fromt,
(select1inionall
select2inionall
select3inionall
select4id)t1
wheretypeisnotnull
andt1.id<=right(replace(type,0,''),1))s
ont.type=s.type
結果
selectdistincttype,
casewhenisnumeric(replace(type,0,''))=0then
left(replace(type,0,''),1)+cast(t1.idasvarchar)+'級'elsecast(t1.idasvarchar)+'級'endname2
fromt,
(select1inionall
select2inionall
select3inionall
select4id)t1
wheretypeisnotnull
andt1.id<=right(replace(type,0,''),1)
『肆』 sqlserver如何列轉行
CREATETABLE#test(
snINT,
enINT
);
INSERTINTO#testVALUES(1,5);
INSERTINTO#testVALUES(6,7);
INSERTINTO#testVALUES(8,8);
INSERTINTO#testVALUES(9,12);
go
SELECTDISTINCT
m.number
FROM
#testt
JOINmaster..spt_valuesm
ON(
t.sn<=m.number
ANDt.en>=m.number
);
go
查詢結果:
number
-----------
1
2
3
4
5
6
7
8
9
10
11
12
(12行受影響)
『伍』 sqlserver2005 縱向的一列變成一行輸出(一行一列)。
看效果是不是這樣:
測試案例:
SELECT*fromTab
--sql語句如下:
select[type],STUFF(
(SELECT','+namefromTabt2WHEREt1.type=t2.typeFORXMLPATH(''))--通過Type過濾轉換成文本同時以(逗號、)分隔
,1,1,'')infofromtabt1GROUPby[type]orderbyt1.[type],info//分組後排序
效果圖如下:
--關鍵這段代碼:你拿去資料庫中執行一下將表的行式轉成XML格式字元串當遇到標簽與字元相加時會去掉標簽!
SELECT','+namefromTabt2FORXMLPATH('')
希望能幫到你!
『陸』 sqlserver 行列轉換問題
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([col1] varchar(1),[col2] varchar(6))
insert [tb]
select 'A','a' union all
select 'B','b,c,d' union all
select 'C','c,aa,e' union all
select 'E','e'
go
select
a.col1,
col2=substring(a.col2,b.number,charindex(',',col2+',',b.number)-b.number)
from
tb a,master..spt_values b
where
b.type='P'
and
substring(','+a.col2,b.number,1)=','
/**
col1 col2
---- ------
A a
B b
B c
B d
C c
C aa
C e
E e
(8 行受影響)
**/
『柒』 如何把sqlserver中的列轉換成行,行轉換成列,顯示
create database arron
go
use arron
go
-- createTable init Data
create table students (
name varchar(25),
class varchar(25),
grade int
)
insert into students values ('張三','語文',20)
insert into students values ('張三','數學',90)
insert into students values ('張三','英語',50)
insert into students values ('李四','語文',81)
insert into students values ('李四','數學',60)
insert into students values ('李四','英語',90)
-- solution1
select * from students
pivot(
max(grade)
FOR [class] IN ([語文],[數學],[英語])
) AS pvt
-- solution2 相當於自連接
select A.Name,A.grade as 語文,B.grade as 數學,C.grade as 英語
from students A,students B,students C
where A.Name=B.Name and B.Name=C.Name
and A.class='語文' and B.class='數學'
and C.class='英語'
-- solution3
select name,
max(case when s.class='語文' then s.grade end) as 語文,
max(case when s.class='數學' then s.grade end) as 數學,
max(case when s.class='英語' then s.grade end) as 英語
from students s group by name
--在有id 的情況下
create table students2 (
id int primary key identity(1,1),
name varchar(25),
class varchar(25),
grade int
)
insert into students2 values ('張三','語文',20)
insert into students2 values ('張三','數學',90)
insert into students2 values ('張三','英語',50)
insert into students2 values ('李四','語文',81)
insert into students2 values ('李四','數學',60)
insert into students2 values ('李四','英語',90)
-- 原先的solution1(有問題)
select * from students2
pivot(
max(grade)
FOR [class] IN ([語文],[數學],[英語])
) AS pvt
-- 原先的solution2 (ok)
select A.Name,A.grade as 語文,B.grade as 數學,C.grade as 英語
from students A,students2 B,students2 C
where A.Name=B.Name and B.Name=C.Name
and A.class='語文' and B.class='數學'
and C.class='英語'
-- 原先的solution3 (ok)
select name,
max(case when s.class='語文' then s.grade end) as 語文,
max(case when s.class='數學' then s.grade end) as 數學,
max(case when s.class='英語' then s.grade end) as 英語
from students s group by name
--unpivot 函數使用
create table test1(id int,name varchar(20), Q1 int, Q2 int, Q3 int, Q4 int)
insert into test1 values(1,'a',1000,2000,4000,5000)
insert into test1 values(2,'b',3000,3500,4200,5500)
--實現的sql
select * from test1
select id ,[name],[ji],[xiaoshou] from test1
unpivot
(
xiaoshou for ji in
([q1],[q2],[q3],[q4])
)
as f
--- 以下的sql 可以替換上面的sql
select id,[name],
ji='Q1',
xiaoshou=(select Q1 from test1 where id=a.id)
from test1 as a
union
select id,[name],
ji='Q2',
xiaoshou=(select Q2 from test1 where id=a.id)
from test1 as a
union
select id,[name],
ji='Q3',
xiaoshou=(select Q3 from test1 where id=a.id)
from test1 as a
union
select id,[name],
ji='Q4',
xiaoshou=(select Q4 from test1 where id=a.id)
from test1 as a
『捌』 sqlserver列轉行 查詢
select*fromA_14072201
IDUSERID
4685938130
46859888
4681138130
4681137761
4647037836
4647037836
4643037836
4643037836
selectID,USERIDfromA_14072201
whereIDin(selectIDfrom(selectID,USERIDfromA_14072201groupbyID,USERID)astableAgroupbyIDhavingCOUNT(*)=2)
IDUSERID
4685938130
46859888
4681138130
4681137761
selecttable1.ID,table1.USERID,table2.USERID
from(selectID,USERIDfrom(selectID,USERID,ROW_NUMBER()over(orderbyID)ROWNUMBERfromA_14072201whereIDin(selectIDfrom(selectID,USERIDfromA_14072201groupbyID,USERID)astableAgroupbyIDhavingCOUNT(*)=2))astableBwhereROWNUMBER%2=1)table1
innerjoin
(selectID,USERIDfromA_14072201whereIDin(selectIDfrom(selectID,USERIDfromA_14072201groupbyID,USERID)astableAgroupbyIDhavingCOUNT(*)=2))table2
ontable1.ID=table2.IDandtable1.USERID<>table2.USERID
IDUSERIDUSERID
468113813037761
4685938130888
--結果是對的,寫的有點亂,你捋一捋然後優化一下,優化不了就直接改成表值函數去處理,我工作去了,話說你這個問題是半夜2:39發的,要注意身體啊。
『玖』 MS SQL SERVER 如何把多列的值 , 變成一列多行 .
行轉列,列轉行是我們在開發過程中經常碰到的問題
行轉列一般通過CASE WHEN 語句來實現,也可以通過 SQL SERVER 2005 新增的運算符PIVOT來實現。用傳統的方法,比較好理解。層次清晰,而且比較習慣。
但是PIVOT 、UNPIVOT提供的語法比一系列復雜的SELECT...CASE 語句中所指定的語法更簡單、更具可讀性.
--行轉列的靜態方案一:CASE WHEN,兼容sql2000
select custid,
3 sum(case when YEAR(orderdate)=2002 then qty end) as [2002],
sum(case when YEAR(orderdate)=2003 then qty end) as [2003],
sum(case when YEAR(orderdate)=2004 then qty end) as [2004]
from orders
group by custid;--行轉列的靜態方案二:PIVOT,sql2005及以後版本
select *
from (select custid,YEAR(orderdate) as years,qty from orders) as ord
pivot(sum(qty) for years in([2002],[2003],[2004]))as p