0

我开发了如下存储过程。我想排除每个 inputdate 列值为空的行(如果有一个输入日期不为空,则该行应保留)。我不知道该怎么做。我搜索了网络,但没有任何结果。感谢你的帮助。谢谢。

create procedure [dbo].[sp_select_staff_time_inputs]
@startDate date,
@enddate date,
@teamid int,
@functionid int,
@servicefunctionid int

as

-- create variables, @columns = inputdates (columns of pivoted table)
DECLARE @columns NVARCHAR(2000)
declare @query nvarchar(4000)

select distinct inputdate into #temp_table_input_dates 
from timeinputs 
where inputdate >= @startDate and inputdate <= @enddate
order by inputdate

--select * from #temp_table_input_dates 

select @columns = isnull(@columns + ',', '') + '[' +  convert(varchar,convert(date,inputdate)) + ']' FROM #temp_table_input_dates

--select @columns

create table #temp_table_joins (
    soeid varchar(7),
    firstname varchar(50),
    lastname varchar(50),
    teamid int,
    team varchar(50),
    functionid int,
    function varchar(50),
    inputdate date,
    noofhours float,
    servicefunctionid int,
    servicefunction varchar(50),
    servicephaseid int,
    servicephase varchar(50)
    )

insert into #temp_table_joins
--select * into #temp_table_joins from 
SELECT 
u.SOEID, u.Firstname, u.Lastname, u.teamid,
t.team, t.functionid,
f.function,
ti.inputdate, ti.noofhours, ti.servicefunctionid,
sf.servicefunction, sf.servicephaseid,
sp. servicephase

from users u 
inner join teams t on u.teamid = t.teamid
inner join functions f on t.functionid = f.functionid
inner join timeinputs ti on u.userid = ti.userid
inner join servicefunctions sf on ti.servicefunctionid = sf.servicefunctionid
inner join servicephases sp on sf.servicephaseid = sp.servicephaseid

--select * from #temp_table_joins

set @query = 'select soeid, firstname, lastname, teamid, team, functionid,
    function, servicefunctionid, servicefunction, servicephaseid,
    servicephase' + @columns + ' from
(select * from #temp_table_joins
) p
pivot (sum (noofhours) for inputdate in (' + @columns + ')) as asd'

--select @query
--select *, noofhours, userid from timeinputs

execute(@query)

drop table #temp_table_joins
drop table #temp_table_input_dates 

GO
4

2 回答 2

1

我不确定,但是

DECLARE @columnscondition NVARCHAR(2000)
...
SELECT @columnscondition = isnull(@columns + ' AND ', ' WHERE ') + convert(varchar,convert(date,inputdate)) + ' IS NOT NULL ' FROM #temp_table_input_dates
...
set @query = 'select soeid, firstname, lastname, teamid, team, functionid,
    function, servicefunctionid, servicefunction, servicephaseid,
    servicephase' + @columns + ' from
(select * from #temp_table_joins
) p'
+ @columnscondition +
'pivot (sum (noofhours) for inputdate in (' + @columns + ')) as asd'

也许,动态部分缺少括号。

于 2013-07-05T07:38:05.463 回答
1

谢谢你。你的解决方案对我来说并不像预期的那样,但你启发了我去创造我自己的。

请看下面的代码:

create procedure [dbo].[sp_select_staff_time_inputs]
@startDate date,
@enddate date,
@gvoteamid int,
@gvofunctionid int,
@servicefunctionid int

as

-- create variables, @columns = inputdates (columns of pivoted table)
DECLARE @columns NVARCHAR(2000)
declare @query nvarchar(4000)
DECLARE @columnscondition NVARCHAR(2000)

select distinct inputdate into #temp_table_input_dates 
from timeinputs 
where inputdate >= @startDate and inputdate <= @enddate
order by inputdate

--select * from #temp_table_input_dates 

select @columns = isnull(@columns + ',', '') + '[' +  convert(varchar,convert(date,inputdate)) + ']' FROM #temp_table_input_dates
select @columnscondition = isnull(@columnscondition + ' and ', '') + '[' +  convert(varchar,convert(date,inputdate)) + '] is not null' FROM #temp_table_input_dates

--SELECT @columnscondition


--select @columns

create table #temp_table_joins (
    soeid varchar(7),
    firstname varchar(50),
    lastname varchar(50),
    gvoteamid int,
    gvoteam varchar(50),
    gvofunctionid int,
    gvofunction varchar(50),
    inputdate date,
    noofhours float,
    servicefunctionid int,
    servicefunction varchar(50),
    servicephaseid int,
    servicephase varchar(50)
    )

insert into #temp_table_joins
--select * into #temp_table_joins from 
SELECT 
u.SOEID, u.Firstname, u.Lastname, u.gvoteamid,
t.gvoteam, t.gvofunctionid,
f.gvofunction,
ti.inputdate, ti.noofhours, ti.servicefunctionid,
sf.servicefunction, sf.servicephaseid,
sp. servicephase

from users u 
inner join gvoteams t on u.gvoteamid = t.gvoteamid
inner join gvofunctions f on t.gvofunctionid = f.gvofunctionid
inner join timeinputs ti on u.userid = ti.userid
inner join servicefunctions sf on ti.servicefunctionid = sf.servicefunctionid
inner join servicephases sp on sf.servicephaseid = sp.servicephaseid

--select * from #temp_table_joins

set @query = 'select soeid, firstname, lastname, gvoteamid, gvoteam, gvofunctionid,
    gvofunction, servicefunctionid, servicefunction, servicephaseid,
    servicephase, ' + @columns + ' from
(select * from #temp_table_joins where noofhours is not null
) p 

pivot (sum (noofhours) for inputdate in (' + @columns + ')) as asd
where (' + @columnscondition +')'

--select @query

--select @query
--select *, noofhours, userid from timeinputs

execute(@query)

drop table #temp_table_joins
drop table #temp_table_input_dates 



GO
于 2013-07-15T14:21:19.203 回答