當前位置:首頁 » 編程語言 » sql查詢將列轉化成行顯示
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

sql查詢將列轉化成行顯示

發布時間: 2023-01-27 00:58:34

㈠ 怎麼sql語句實現把查詢結果的一列並為一行顯示

光靠SQL語句是辦不到的。因為一條SQL你查詢的時候默認的就是返回的一個表格的形式, 如果想要得到你要的效果的話當然程序裡面是很容易辦到的, 但是如果你是只靠SQL來完成的話那麼你就要寫個簡單的存儲過程了這樣就能滿足你的條件了

㈡ sql資料庫把一列數據轉換成一行數據

這是一個典型的 行列轉換問題。這些數據應該有著統一的一列吧,例如:標識人員的身份證號碼之類的。方法別人博客中都很詳盡,我就不在此羅列了。 可檢索關鍵字 「SQL 行列轉換」或者看下面鏈接
http://www.cnblogs.com/zhangzt/archive/2010/07/29/1787825.html
http://hi..com/wrgcxfcoybinpur/item/90f388e2ec0f853286d9decc

㈢ sql怎麼將列轉換成一行

selectordernum,
[values]=stuff((select'/'+[列名]fromtbtwhereordernum=tb.ordernumforxmlpath('')),1,1,'')
fromtb
groupbyordernum

你這是一個查詢吧,第二列連列名都沒有

sqlserver2005以上版本支持,tb是表名,自己換一下

㈣ sql語句怎麼把列變成行

create table rotatetable1 (序號 int,company char(66),box_weight char(12),廢塑料numeric(10,2)),廢五金 numeric(10,2)),廢鋼鐵 numeric(10,2)),廢紙 numeric(10,2)),廢有色 numeric(10,2)),廢纖維 numeric(10,2)),其它 numeric(10,2)),合計 numeric(10,2)));
insert into rotatetable1(company,box_weight) select name ,'weight' from sum1 group by name;
insert into rotatetable1(company,box_weight) select name ,'box' from sum1 group by name;
update rotatetable1 set 廢塑料=box from sum1as a where a.name=rotatetable1.company and box_weight='box' and hsname='廢塑料';
update rotatetable1 set 廢塑料=weight from sum1as a where a.name=rotatetable1.company and box_weight='weight' and hsname='廢塑料';
::: :::
update rotatetable1 set 其它=box from sum1as a where a.name=rotatetable1.company and box_weight='box' and hsname='其它';
update rotatetable1 set 其它=weight from sum1as a where a.name=rotatetable1.company and box_weight='weight' and hsname='其它';
::: :::
update rotatetable1 set 合計=廢塑料+廢五金+廢鋼鐵+廢紙+廢有色+廢纖維+其它;

(所有涉及表的行列轉換均可按照這種方式實現。)

㈤ SQL表列數據轉成行數據

create table #A03(AId varchar(30),
CardNo varchar(30),SumMoney money,Type varchar(30)) --臨時表
create table #A07(AId varchar(30),
CardNo varchar(30),SumMoney money,Type varchar(30))
insert into #A03 select AId,CardNo,SumMoney,Type from A
where AID='B6BA' and CardNo='996000010003'
insert into #A70 select AId,CardNo,SumMoney,Type from A
where AID='B6BA' and CardNo='996000010070'

select a.AID,a.CardNo,a.SumMoney,a.Type,
b.CardNo,b.SumMoney,b.Type
from #A03 a right join #A70 b on a.CardNo='996000010003' and b.CardNo='996000010070'
--你試試

㈥ SQL 查詢 表格的轉換,將列轉換成行顯示

SELECTSN,SUM(CASEWHENField_Name='BL1_Ver'THENField_ValueEND)BL1_Ver,
SUM(CASEWHENField_Name='BL2'THENField_ValueEND)BL2,
SUM(CASEWHENField_Name='BL3'THENField_ValueEND)BL3
FROM表名
GROUPBYSN

㈦ SQL把查詢出的一個列轉換成行

if not object_id('Class') is null
drop table Class
Go
Create table Class([Student] nvarchar(2),[Course] nvarchar(2),[Score] int)
Insert Class
select N'張三',N'語文',78 union all
select N'張三',N'數學',87 union all
select N'張三',N'英語',82 union all
select N'張三',N'物理',90 union all
select N'李四',N'語文',65 union all
select N'李四',N'數學',77 union all
select N'李四',N'英語',65 union all
select N'李四',N'物理',85
Go

declare @s nvarchar(4000)
select @s=isnull(@s+',','')+quotename(Name)
from syscolumns where ID=object_id('Class') and Name not in('Student')
order by Colid
exec('select Student,[Course],[Score] from Class unpivot ([Score] for [Course] in('+@s+'))b')

go
select
Student,[Course],[Score]
from
Class
unpivot
([Score] for [Course] in([數學],[物理],[英語],[語文]))b

㈧ sql語句列轉行

我整理的行轉列的問題:

--創建tb表
createtabletb(姓名varchar(10),課程varchar(10),分數int)
insertintotbvalues('張三','語文',74)
insertintotbvalues('張三','數學',83)
insertintotbvalues('張三','物理',93)
insertintotbvalues('李四','語文',74)
insertintotbvalues('李四','數學',84)
insertintotbvalues('李四','物理',94)
go

select*Fromtb

--SQLSERVER2000靜態行轉列
select姓名as姓名,
max(case課程when'語文'then分數elsenullend)語文,
max(case課程when'數學'then分數elsenullend)數學,
max(case課程when'物理'then分數elsenullend)物理
fromtb
groupby姓名

--SQLSERVER2000動態SQL,指課程不止語文、數學、物理這三門課程。(以下同)
declare@sqlvarchar(8000)
set@sql='select姓名'
select@sql=@sql+',max(case課程when'''+課程+'''then分數else0end)['+課程+']'
from(selectdistinct課程fromtb)asa
set@sql=@sql+'fromtbgroupby姓名'
exec(@sql)

--SQLSERVER2005靜態SQL。
select*from(select*fromtb)apivot(max(分數)for課程in(語文,數學,物理))b

--SQLSERVER2005動態SQL。
declare@sqlvarchar(8000)
select@sql=isnull(@sql+'],[','')+課程fromtbgroupby課程
set@sql='['+@sql+']'
exec('select*from(select*fromtb)apivot(max(分數)for課程in('+@sql+'))b')

希望對你的學習有幫助。

㈨ SQL 怎樣將查詢出某列的多行數據,變為一行顯示 通過SQL語句查詢出結果 AutoID cT

你好,你看看參考一下。

DECLARE@tTABLE(idint,valuevarchar(10))
INSERT@tSELECT1,'出國'
UNIONALLSELECT2,'回國'

SELECT[values]=STUFF((REPLACE(REPLACE((SELECTvalueFROM@tFORXMLPATH('')),'<value>',','),'</value>','')),1,1,'')

㈩ SQL 如何把一列的值轉換成一行顯示

declare @sql varchar(1000)
set @sql = ''
select @sql = @sql+ a + ','
from tb

select left(@sql,len(@sql)-1)