如何将上图中的两行数据合并到一行中。
合并后的数据为:
列名:InvestorsName,TypeName,IDcard,Investment,InvestmentRatio,InvestorsName1,TypeName1,IDcard1,Investment1,InvestmentRatio1
对应数据:
姚雅,法人,4564564564564564,10,10,曹,股东,36688899545588/411489,90,90
在线等,救急!
------解决方案--------------------
----------------------------
-- 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