sql列合并
① 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里
然后循环读取行的第二和第三个值
在写到一个字符串里