22,300
社区成员




declare @s nvarchar(4000)
set @s=''
Select @s=@s+','+quotename(N'类型'+CAST([type]AS VARCHAR))+'=max(case when [type]='+quotename([type],'''')+' then isnull(isnull(BasicFee,0)+isnull(ChangeFee,0)+isnull(DiffPriceFee,0),0) else 0 end)'
from [huang] group by [type]
exec('select [No],[Name],[Month],[OpData]'+@s+' from [huang] group by [No],[Name],[Month],[OpData]')
试试,如果不行的话,用个临时表先把sum算出来,也就是得出你一开始的结果集然后再运算----------------------------------------------------------------
-- Author :DBA_HuangZJ(发粪涂墙)
-- Date :2014-05-06 10:53:18
-- Version:
-- Microsoft SQL Server 2008 R2 (RTM) - 10.50.1600.1 (X64)
-- Apr 2 2010 15:48:46
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: ) (Hypervisor)
--
----------------------------------------------------------------
--> 测试数据[huang]
if object_id('[huang]') is not null drop table [huang]
go
create table [huang]([No] int,[Name] nvarchar(4),[Type] int,[Month] nvarchar(14),[Sum] int,[OpData] nvarchar(14))
insert [huang]
select 1,N'张三',2,'2014-02',30,'2014-05' union all
select 1,N'张三',3,'2014-02',40,'2014-05' union all
select 2,N'张三',2,'2014-02',30,'2014-05'
--------------生成数据--------------------------
declare @s nvarchar(4000)
set @s=''
Select @s=@s+','+quotename(N'类型'+CAST([type]AS VARCHAR))+'=max(case when [type]='+quotename([type],'''')+' then [Sum] else 0 end)'
from [huang] group by [type]
exec('select [No],[Name],[Month],[OpData]'+@s+' from [huang] group by [No],[Name],[Month],[OpData]')
----------------结果----------------------------
/*
No Name Month OpData 类型2 类型3
----------- ---- -------------- -------------- ----------- -----------
1 张三 2014-02 2014-05 30 40
2 张三 2014-02 2014-05 30 0
*/
declare @s nvarchar(4000) set @s=''
Select @s=@s+','+quotename(CAST([FeeType]AS VARCHAR))+'=max(case when [FeeType]='+quotename([FeeType],'''')+
'then isnull(isnull(BasicFee,0)+isnull(ChangeFee,0)+isnull(DiffPriceFee,0),0) else 0 end)'from [V_ReceiptFee] WHERE BasicFee IS NOT NULL OR ChangeFee IS NOT NULL OR DiffPriceFee IS NOT NULL group by [FeeType]
exec('select [InvoiceID],[InvoiceNo],[RoomName],[CustomerName],[PayType],[OpDate],u.username as OpName,[Month]'+@s+' from [V_ReceiptFee]
left join JQ_T_User u on u.tr_id=OpName where 1=1 and PayType!=''预交冲帐''
and ProID in(10001) and BuildID=''10005''
and OpDate between ''2014-03-20'' and ''2014-05-05''
group by [InvoiceID],[InvoiceNo],[RoomName],[CustomerName],[PayType],[OpDate],u.username,[OpName],[Month]
order by InvoiceID desc
')
declare @s nvarchar(4000) set @s=''
Select @s=@s+','+quotename(CAST([FeeType]AS VARCHAR))+'=max(case when [FeeType]='+quotename([FeeType],'''')+
'then isnull(isnull(BasicFee,0)+isnull(ChangeFee,0)+isnull(DiffPriceFee,0),0) else 0 end)'from [V_ReceiptFee] WHERE isnull(isnull(BasicFee,0)+isnull(ChangeFee,0)+isnull(DiffPriceFee,0),0)<>0 group by [FeeType]
exec('select [InvoiceID],[InvoiceNo],[RoomName],[CustomerName],[PayType],[OpDate],u.username as OpName,[Month]'+@s+' from [V_ReceiptFee]
left join JQ_T_User u on u.tr_id=OpName where 1=1 and PayType!=''预交冲帐''
and ProID in(10001) and BuildID=''10005''
and OpDate between ''2014-03-20'' and ''2014-05-05''
group by [InvoiceID],[InvoiceNo],[RoomName],[CustomerName],[PayType],[OpDate],u.username,[OpName],[Month]
order by InvoiceID desc
')
declare @s nvarchar(4000) set @s=''
Select @s=@s+','+quotename(CAST([FeeType]AS VARCHAR))+'=max(case when [FeeType]='+quotename([FeeType],'''')+
'then isnull(isnull(BasicFee,0)+isnull(ChangeFee,0)+isnull(DiffPriceFee,0),0) else 0 end)'from [V_ReceiptFee] group by [FeeType]
exec('select [InvoiceID],[InvoiceNo],[RoomName],[CustomerName],[PayType],[OpDate],u.username as OpName,[Month]'+@s+' from [V_ReceiptFee]
left join JQ_T_User u on u.tr_id=OpName where 1=1 and PayType!=''预交冲帐''
and ProID in(10001) and BuildID=''10005''
and OpDate between ''2014-03-20'' and ''2014-05-05''
group by [InvoiceID],[InvoiceNo],[RoomName],[CustomerName],[PayType],[OpDate],u.username,[OpName],[Month]
order by InvoiceID desc
')