当前位置:首页 » 编程语言 » 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-15 00:07:52 浏览:270
win7php环境搭建 发布:2024-11-15 00:06:55 浏览:17
erpjava 发布:2024-11-14 23:52:23 浏览:253
电脑版地平线四怎么连上服务器 发布:2024-11-14 23:46:42 浏览:472
ios怎么变安卓 发布:2024-11-14 23:46:36 浏览:333
win7共享xp打印机拒绝访问 发布:2024-11-14 23:45:29 浏览:750
引起资源配置失效的原因有哪些 发布:2024-11-14 23:35:22 浏览:15
c语言打字 发布:2024-11-14 23:11:06 浏览:893
存储程序和程序控制的原理 发布:2024-11-14 22:53:23 浏览:323
python读取json数据 发布:2024-11-14 22:51:52 浏览:931