日期:2014-05-16 浏览次数:20732 次
----------------------------------------------------------------
-- Author :fredrickhu(小F,向高手学习)
-- Date :2014-02-28 15:09:58
-- Verstion:
-- Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86)
-- Jul 9 2008 14:43:34
-- Copyright (c) 1988-2008 Microsoft Corporation
-- Enterprise Edition on Windows NT 6.1 <X86> (Build 7601: Service Pack 1)
--
----------------------------------------------------------------
--> 测试数据:[a]
if object_id('[a]') is not null drop table [a]
go
create table [a]([CUST_ID] int,[a] int)
insert [a]
select 101,100 union all
select 102,60
--> 测试数据:[b]
if object_id('[b]') is not null drop table [b]
go
create table [b]([CUST_ID] int,[a] int)
insert [b]
select 101,50 union all
select 102,35
--------------开始查询--------------------------
SELECT
a.CUST_ID,
LTRIM(CAST(b.a*100.0/a.a AS dec(18,1)))+'%' AS c
FROM
a INNER JOIN b ON a.CUST_ID=b.CUST_ID
----------------结果----------------------------
/* CUST_ID c
----------- ------------------------------------------
101 50.0%
102 58.3%
(2 行受影响)
*/
select 101 CUST_ID , cast(50.0 as varchar(100))+'%' C