當前位置:首頁 » 操作系統 » 資料庫行轉列

資料庫行轉列

發布時間: 2022-03-07 22:59:57

『壹』 請幫忙把下面資料庫錶行變列吧

select t.欄位1,
sum(decode(t.欄位3, '2', 欄位4,null)) as 欄位3,
sum(decode((t.欄位3, 600', 欄位4,null)) as 欄位3,
sum(decode(t.欄位3, '50', 欄位4,null)) as 欄位3 ,
sum(decode(t.欄位3, '100', 欄位4,null)) as 欄位3
from 表名字 t
group by t.欄位1
order by t.欄位1

『貳』 sql中行變成列

這是一個行列轉換的問題嘛,可以用case when then來做
select sno,
sum(case course when '語文' then source else 0 end) as 語文,
sum(case course when '數學'then source else 0 end) as 數學
from 表名 group by sno

關於行列轉換的詳細介紹你可以在網上找找,這個問題個人感覺比較重要,建議多看看

『叄』 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 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行轉列

SELECT e.NAME as name,
count(case when DATEPART(year,c.START_TIME)=2014 then 1 else null end ) as count2014,
count(case when DATEPART(year,c.START_TIME)=2015 then 1 else null end ) as count2015
from ENVI_DATA_STATISTIC_COMP c
LEFT JOIN ENVI_CITY e on e.CITY_ID=c.CITY_ID
where c.COMP_NAME like '%藍天白雲%' and INDEX_TYPE='12'
and (c.START_TIME like '%2014-08%' or c.START_TIME like '%2015-08%' ) and STATISTIC_TYPE='1' and c.VALID_FLAG='1'
group by name

應該是醬紫的,使用case when 轉換~

『陸』 怎樣實現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)

『柒』 資料庫SQL實現行轉列。請懂的人幫忙,謝謝!

沒有環境,試試下面語句可否實現(oracle):
select Customer.CustomerId,
Customer.CustomerName
wm.concat(Phone.PhoneNumber)
from Customer left join
(
select CustomerPhone.CustomerId,
Phone.PhoneNumber
from CustomerPhone inner join Phone on CustomerPhone.PhoneId=Phone.PhoneId
order by CustomerPhone.CustomerId
)Phone on Customer.CustomerId=Phone.CustomerId

『捌』 sql 行轉列

最簡答的方法:使用程序數組,例如你現在的代碼是:
do while not rs.eof
response.write rs("....")
rs.movenext
end do

那麼可以下面這樣輸出:
'先為每個欄位定義數組
dim a(1)
dim b(1)
n=1
do while not rs.eof
a(n)=rs("a")
b(n)=rs("b")
'...有多少欄位寫多少行....
n=n+1
rs.movenext
end do
'下面再輸出
response.write "<table>";
response.write "<tr><td>" & join("<td>",a)
response.write "<tr><td>" & join("<td>",b)
response.write "</table>";

你的ASP程序不可能顯示多少條,一般每頁顯示20條左右,用數組在顯示的轉換是可行的。

『玖』 SQL 行轉列

我這里兩種都給你介紹,行轉列和列轉行:

列轉行——

錄入經營范圍時候會遇到列傳行的問題解決方案如下:

在temp1 表有一下欄位內容:

熱點內容
電視系統密碼是多少 發布:2024-09-24 09:15:38 瀏覽:270
我手機的密碼是什麼 發布:2024-09-24 08:50:53 瀏覽:12
pythonlist轉換str 發布:2024-09-24 08:40:25 瀏覽:564
怎麼通過crt連接阿里雲伺服器 發布:2024-09-24 08:30:42 瀏覽:85
豐巢快遞櫃的密碼是多少 發布:2024-09-24 08:10:33 瀏覽:500
高級java學習 發布:2024-09-24 07:57:20 瀏覽:887
訪問書訪問 發布:2024-09-24 07:47:28 瀏覽:142
強混合加密 發布:2024-09-24 07:46:03 瀏覽:288
android可見性 發布:2024-09-24 07:29:08 瀏覽:18
大通g20房車哪個配置性價比高 發布:2024-09-24 07:28:29 瀏覽:232