日期:2014-05-17 浏览次数:20799 次
----------------------------
-- 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 &nbs