如何将上图中的两行数据合并到一行中。
合并后的数据为:
列名:InvestorsName,TypeName,IDcard,Investment,InvestmentRatio,InvestorsName1,TypeName1,IDcard1,Investment1,InvestmentRatio1
对应数据:
姚雅,法人,4564564564564564,10,10,曹,股东,36688899545588/411489,90,90
在线等,救急!
8 个解决方案
#1
第二行的数据搞到第一行对应字段里面?
#2
这个不是数据库干的工作,想想在别的地方解决吧。
#3
是的,把第二行的数据在列名后面加个数据区别,然后追加到第一条数据后面
#4
如果就只有两行数据的话,可以自己手动拼接进去
select a.*,b.* from tb a,tb b where a.id=1 and b.id=2
列名你自己输入
select a.*,b.* from tb a,tb b where a.id=1 and b.id=2
列名你自己输入
#5
----------------------------------------------------------------
-- Author :DBA_Huangzj(發糞塗牆)
-- Date :2013-02-20 16:07:09
-- Version:
-- Microsoft SQL Server 2008 R2 (SP1) - 10.50.2500.0 (X64)
-- Jun 17 2011 00:54:03
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition (64-bit) on Windows NT 6.1 <X64> (Build 7601: Service Pack 1, v.721)
--
----------------------------------------------------------------
--> 测试数据:[huang]
if object_id('[huang]') is not null drop table [huang]
go
create table [huang]([id] int,[company_id] int,[investorsName] varchar(4),[typename] varchar(4),[idcard] bigint,[investment] int,[investmentratio] int,[operationperson] varchar(5),[createdate] datetime,[updatedate] sql_variant)
insert [huang]
select 1,2,'姚亚','法人',135461564591,10,10,'admin','2013-01-22',null union all
select 2,2,'曹','股东',1687961987852,90,90,'admin','2013-01-22',null
--------------开始查询--------------------------
SELECT MAX(CASE WHEN company_id =2 THEN InvestorsName ELSE NULL END) InvestorsName,
MAX(CASE WHEN company_id =2 THEN TypeName ELSE NULL END) TypeName,
MAX(CASE WHEN company_id =2 THEN IDcard ELSE NULL END)IDcard,
MAX(CASE WHEN company_id =2 THEN InvestmentRatio ELSE NULL END)Investment,
MAX(CASE WHEN company_id =2 THEN InvestmentRatio ELSE NULL END)InvestmentRatio,
MAX(CASE WHEN company_id =2 THEN InvestorsName ELSE NULL END)InvestorsName1,
MAX(CASE WHEN company_id =2 THEN TypeName ELSE NULL END)TypeName1,
MAX(CASE WHEN company_id =2 THEN IDcard ELSE NULL END)IDcard1,
MAX(CASE WHEN company_id =2 THEN Investment ELSE NULL END)Investment1,
MAX(CASE WHEN company_id =2 THEN InvestmentRatio ELSE NULL END)InvestmentRatio1
FROM huang
GROUP BY company_id
----------------结果----------------------------
/*
InvestorsName TypeName IDcard Investment InvestmentRatio InvestorsName1 TypeName1 IDcard1 Investment1 InvestmentRatio1
------------- -------- -------------------- ----------- --------------- -------------- --------- -------------------- ----------- ----------------
姚亚 股东 1687961987852 90 90 姚亚 股东 1687961987852 90 90
(1 行受影响)
*/
#6
不就是列转行而已嘛
#7
你也知道了?
#8
你就是牛逼的!你就是李春波
#1
第二行的数据搞到第一行对应字段里面?
#2
这个不是数据库干的工作,想想在别的地方解决吧。
#3
是的,把第二行的数据在列名后面加个数据区别,然后追加到第一条数据后面
#4
如果就只有两行数据的话,可以自己手动拼接进去
select a.*,b.* from tb a,tb b where a.id=1 and b.id=2
列名你自己输入
select a.*,b.* from tb a,tb b where a.id=1 and b.id=2
列名你自己输入
#5
----------------------------------------------------------------
-- Author :DBA_Huangzj(發糞塗牆)
-- Date :2013-02-20 16:07:09
-- Version:
-- Microsoft SQL Server 2008 R2 (SP1) - 10.50.2500.0 (X64)
-- Jun 17 2011 00:54:03
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition (64-bit) on Windows NT 6.1 <X64> (Build 7601: Service Pack 1, v.721)
--
----------------------------------------------------------------
--> 测试数据:[huang]
if object_id('[huang]') is not null drop table [huang]
go
create table [huang]([id] int,[company_id] int,[investorsName] varchar(4),[typename] varchar(4),[idcard] bigint,[investment] int,[investmentratio] int,[operationperson] varchar(5),[createdate] datetime,[updatedate] sql_variant)
insert [huang]
select 1,2,'姚亚','法人',135461564591,10,10,'admin','2013-01-22',null union all
select 2,2,'曹','股东',1687961987852,90,90,'admin','2013-01-22',null
--------------开始查询--------------------------
SELECT MAX(CASE WHEN company_id =2 THEN InvestorsName ELSE NULL END) InvestorsName,
MAX(CASE WHEN company_id =2 THEN TypeName ELSE NULL END) TypeName,
MAX(CASE WHEN company_id =2 THEN IDcard ELSE NULL END)IDcard,
MAX(CASE WHEN company_id =2 THEN InvestmentRatio ELSE NULL END)Investment,
MAX(CASE WHEN company_id =2 THEN InvestmentRatio ELSE NULL END)InvestmentRatio,
MAX(CASE WHEN company_id =2 THEN InvestorsName ELSE NULL END)InvestorsName1,
MAX(CASE WHEN company_id =2 THEN TypeName ELSE NULL END)TypeName1,
MAX(CASE WHEN company_id =2 THEN IDcard ELSE NULL END)IDcard1,
MAX(CASE WHEN company_id =2 THEN Investment ELSE NULL END)Investment1,
MAX(CASE WHEN company_id =2 THEN InvestmentRatio ELSE NULL END)InvestmentRatio1
FROM huang
GROUP BY company_id
----------------结果----------------------------
/*
InvestorsName TypeName IDcard Investment InvestmentRatio InvestorsName1 TypeName1 IDcard1 Investment1 InvestmentRatio1
------------- -------- -------------------- ----------- --------------- -------------- --------- -------------------- ----------- ----------------
姚亚 股东 1687961987852 90 90 姚亚 股东 1687961987852 90 90
(1 行受影响)
*/
#6
不就是列转行而已嘛
#7
你也知道了?
#8
你就是牛逼的!你就是李春波