create table tttt(FCode varchar(10),FName varchar(20),FMoney money)
go
insert tttt select '001','基本工资',2000
insert tttt select '001', '奖金',3000
insert tttt select '002','奖金',2000
insert tttt select '002','基本工资',3000
go
select fcode,sum(case when fname='基本工资' then FMoney else 0 end) as 基本工资,sum(case when fname='奖金' then FMoney else 0 end) as 奖金
from tttt
group by fcode
drop table tttt
go
insert tttt select '001','基本工资',2000
insert tttt select '001', '奖金',3000
insert tttt select '002','奖金',2000
insert tttt select '002','基本工资',3000
go
select fcode,sum(case when fname='基本工资' then FMoney else 0 end) as 基本工资,sum(case when fname='奖金' then FMoney else 0 end) as 奖金
from tttt
group by fcode
drop table tttt
set @sql='select 员工编号'
select @sql=@sql+',sum(case when 工资条目名称='''+工资条目名称+''' then 金额 end) as ['+工资条目名称+']' from (select distinct 工资条目名称 from table1) a
set @sql=@sql+' from table1 group by 员工编号'exec(@sql)
名称 'select employee_code,sum(case when payroll_item_uuid='工资条目名称1' then 金额 end) as [工资条目名称1],sum(case when payroll_item_uuid='工资条目名称2' then 金额 end) as [工资条目名称2'],sum(case when payroll_item_uuid=工资条目名称3' then 金额 end) as ...请问语句错在哪里?
~~ ~~ ~~ ~~
金额 end) as ...你这些地方有问题,查一下吧。
AS
declare @sql varchar(8000)
set @sql='select employee_code'
select @sql=@sql+',sum(case when payroll_item_uuid='''+payroll_item_uuid+''' then amount end) as ['+payroll_item_uuid+']'
from (select distinct payroll_item_uuid from employee_payroll_item_detail) a
set @sql=@sql+' from employee_payroll_item_detail group into T_EmployeePayrollDetail by employee_code'
exec @sql
GO我想把查询的结果放到一临时表中T_EmployeePayrolDetail,数据取得的表名为Employee_Payroll_Item_Detail。执行后提示上贴的错误。请大家帮忙看一下!
AS
declare @sql varchar(8000)
set @sql='select employee_code'
select @sql=@sql+',sum(case when payroll_item_uuid='''+payroll_item_uuid+''' then amount end) ['+payroll_item_uuid+']'
from (select distinct payroll_item_uuid from employee_payroll_item_detail) a
set @sql=@sql+' into T_EmployeePayrollDetail from employee_payroll_item_detail group by employee_code'
exec (@sql)
GO
AS
declare @sql varchar(8000)
set @sql='select employee_code'
select @sql=@sql+',sum(case when payroll_item_uuid='''+payroll_item_uuid+''' then amount end) as ['+payroll_item_uuid+']'
from (select distinct payroll_item_uuid from employee_payroll_item_detail) a
set @sql=@sql+' into T_EmployeePayrollDetail from employee_payroll_item_detail group by employee_code' --into T_EmployeePayrollDetail 应该放在from语句之前
exec(@sql)
GO