日期:2014-05-16 浏览次数:20396 次
----------------------------------------------------------------
-- Author :fredrickhu(小F,向高手学习)
-- Date :2014-03-26 11:20:33
-- 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)
--
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([ChargeAmount] numeric(8,4),[Payee] int,[YetMoney] numeric(8,4),[ObjectId] int,[IsFree] int,[chargeProjectId] int,[ChargePlanItemId] int,[ObjName] varchar(6),[ChargeProjectName] varchar(6))
insert [tb]
select 6000.0000,2,6000.0000,12,0,1,3951,'三年级','学费' union all
select 500.0000,71,400.0000,12,0,2,3952,'三年级','住宿费' union all
select 500.0000,72,50.0000,12,0,2,3952,'三年级','住宿费' union all
select 500.0000,99,50.0000,12,0,2,3952,'三年级','住宿费' union all
select 100.0000,97,100.0000,12,0,5,3953,'三年级','考试费' union all
select 100.0000,97,100.0000,12,0,6,3954,'三年级','体检费' union all
select 100.0000,54,100.0000,12,0,7,3955,'三年级','校车费'
--------------开始查询--------------------------
if object_id('F_Str') is not null
drop function F_Str
go
create function F_Str(@Col1 VARCHAR(1000))
returns nvarchar(100)
as
begin
declare @S nvarchar(100)
select @S=isnull(@S+',','')+LTRIM(Payee) from Tb where ChargeProjectName=@Col1
return @S
end
go
Select ChargeAmount, dbo.F_Str(ChargeProjectName) AS Payee, SUM(YetMoney) AS YetMoney,ObjectId, IsFree, chargeProjectId, ChargePlanItemId,
ObjName, ChargeProjectName from Tb
GROUP BY ChargeAmount,ObjectId, IsFree, chargeProjectId, ChargePlanItemId,
ObjName, ChargeProjectName,dbo.F_Str(ChargeProjectName)
go
----------------结果----------------------------
/* ChargeAmount Payee &nbs