當前位置:首頁 » 編程語言 » sql列合並

sql列合並

發布時間: 2022-01-22 14:32:06

sql中如何合並兩個特定的欄位

1、創建模擬的數據表

create table GoodsCate(

Mid int not null,

Code varchar(10) not null,

Name varchar(20) not null,

)

② sql 怎樣合並列 將一行中多列 合並成指定數量的列

先轉字元型再加就可以了。
select id,cast(col1 as varchar) + '~' + cast(col2 as varchar) as col from 你的表

③ 怎樣用SQL語句合並兩個表中的兩個列

你給個條件好讓兩條合並成一條啊。如
select a.names, b.names as typ from table1 as a ,table2 as bwhere a.id=b.id

④ SQL語句怎麼實現幾列數據合並成一條

select DISTINCT ta.sCode, ta.sQuality, ta.fNum, tb.sQuality, tb.fNum, tc.sQuality, tc.fNum
from
(

select sCode, sQuality, max(fNum) as fNum from 表2
where sQuality = 'a'
group by sCode, sQuality

) as ta left join (

select sCode, sQuality, max(fNum) as fNum from 表2
where sQuality = 'b'
group by sCode, sQuality

) as tb on tb.sCode = ta.sCode left join (

select sCode, sQuality, max(fNum) as fNum from 表2
where sQuality = 'c'
group by sCode, sQuality

) as tc on tc.sCode = ta.sCode

⑤ SQL怎麼列合並

1、我用Toad做演示,我先新建兩張table,create table #AA(ID int,name nvarchar(10),age int)

create table #BB(ID int,name nvarchar(10),age int )。

⑥ 怎麼將sql查詢出來的結果中一列合並

select
distinct PHYSIC_NAME,
count(名稱) over (partition by 名稱),
sum(需要合計欄位) over (partition by 名稱),
from table

⑦ SQL Server 列合並

創建表,數據

createtable表1
(idint,
qtyint)

createtable表2
(idint,
aaaint)

insertinto表1values(1,2)
insertinto表1values(1,3)
insertinto表1values(2,4)

insertinto表2values(1,5)
insertinto表2values(2,3)
insertinto表2values(2,6)

執行:

selecta.id,a.qty,b.aaafrom
(selectid,qty,row_number()over(partitionbyidorderbyqty)rnfrom表1)ainnerjoin
(selectid,aaa,row_number()over(partitionbyidorderbyaaa)rnfrom表2)bona.id=b.idanda.rn=b.rn
union
selecta.id,a.qty,b.aaafrom
(selectid,qty,row_number()over(partitionbyidorderbyqty)rnfrom表1)aleftjoin
(selectid,aaa,row_number()over(partitionbyidorderbyaaa)rnfrom表2)bona.id=b.idanda.rn=b.rn
union
selecta.id,b.qty,a.aaafrom
(selectid,aaa,row_number()over(partitionbyidorderbyaaa)rnfrom表2)aleftjoin
(selectid,qty,row_number()over(partitionbyidorderbyqty)rnfrom表1)bona.id=b.idanda.rn=b.rn

結果:

⑧ 求教sql sever如何把兩列合並

主要看兩列是什麼類型,一般就是數字或者字元,如果都是字元就直接用「+」連接即可,如果是數字的話,需要將數字轉成字元再連接。
如,test表,這里id是數字類型
id name
1 張三
2 李四
現在要將兩列連接

1

select cast(id as varchar)+name as str from test;

得到的結果
str
1張三
2李四
如果id列為字元類型

1
select id+name as str from test;

⑨ sql多列合並為一列多行

CREATE
TABLE
#t
(
id
int,
a
char(2),
b
char(2),
c
char(2),
d
char(2)
)
go
INSERT
INTO
#t
SELECT
1,
'a1',
'b1',
'c1',
'd1'
UNION
ALL
SELECT
2,
'a2',
'b2',
'c2',
'd2'
UNION
ALL
SELECT
3,
'a3',
'b3',
'c3',
'd3'
GO
1>
SELECT
2>
CASE
3>
WHEN
ROW_NUMBER()
OVER(partition
by
id
order
by
[all])
=
1
THEN
cast(id
as
varchar(2))
4>
ELSE
''
END
AS
id,
5>
[all]
6>
FROM
7>
(
8>
SELECT
id,
a
AS
[all]
FROM
#t
9>
UNION
ALL
10>
SELECT
id,
b
AS
[all]
FROM
#t
11>
UNION
ALL
12>
SELECT
id,
c
AS
[all]
FROM
#t
13>
UNION
ALL
14>
SELECT
id,
d
AS
[all]
FROM
#t
15>
)
t
16>
GO
id
all
--
---
1
a1
b1
c1
d1
2
a2
b2
c2
d2
3
a3
b3
c3
d3
(12
行受影響)

⑩ SQL語句怎麼實現幾列數據合並成一行

你這樣做的話 就這兩個表是不行的。
本來就是一對多的關系 你強行的要把其變成一對一的關系那是不可能。

如果你是要在程序里實現可以這樣:
001,a,10
001,b,10
001,c,10
寫到一個DataTable里
然後循環讀取行的第二和第三個值
在寫到一個字元串里

熱點內容
微信如何評論圖片安卓 發布:2024-11-14 11:56:34 瀏覽:848
游戲遍地腳本 發布:2024-11-14 11:56:32 瀏覽:759
怎樣編譯一個背單詞系統 發布:2024-11-14 11:54:47 瀏覽:176
傳奇這個配置怎麼樣 發布:2024-11-14 11:48:25 瀏覽:376
配置橫向防火牆是什麼意思 發布:2024-11-14 11:42:34 瀏覽:731
手機緩存文件能打開嗎 發布:2024-11-14 11:41:00 瀏覽:492
存儲系統集成 發布:2024-11-14 11:14:54 瀏覽:584
雲伺服器搭建方法圖解 發布:2024-11-14 11:14:53 瀏覽:309
挑戰伺服器吃雞是什麼 發布:2024-11-14 10:59:07 瀏覽:932
自繳社保演算法 發布:2024-11-14 10:43:53 瀏覽:554