sql server - SQL query pivot INSIDE a case statement erroring -
the below code looks should work, only 1 expression can specified in select list when subquery not introduced exists. think refering pivot of avg(testresults) on testrgstr_testname.
i have table full of results tests @ various times of day. want take results sludge, dust , particle tests date range , average them on single row.
this has led use of pivot. issue test names change depending on time of year james, frank others names, has led use of case statement allow different test names , hence different columns after pivot.
apologies errors stripped down version of production code , can't test it.
with testdata ( select testname, convert(decimal(10,2),result) testresult testreg resultdate between @pfromdate , @ptodate , (testrgstr_testname '%sludge%' or testrgstr_testname '%dust%' or testrgstr_testname '%particle%') ) select case when @pcamptype = 'james' ( select [jdtd james cutting sludge at] s_at, [jdtd james cutting sludge ao] s_ao, [jdtd james dust at] d_at, [jdtd james dust ao)] d_ao, [jdtd james particle_at] p_at, [jdtd james particle_ao] p_ao testdata pivot ( avg(testresult) testrgstr_testname in ([jdtd james cutting sludge at],[jdtd james cutting sludge ao], [jdtd james dust at, [jdtd james dust ao], [jdtd james particle_at],[jdtd james particle_ao]) ) pvt ) when @pcamptype = 'frank' ( select [jdtd frank cutting sludge at] s_at, [jdtd james frank sludge ao] s_ao, [jdtd frank dust at] d_at, [jdtd frank dust ao)] d_ao, [jdtd frank particle_at] p_at, [jdtd frank particle_ao] p_ao testdata pivot ( avg(testresult) testrgstr_testname in ([jdtd james cutting sludge at],[jdtd james cutting sludge ao], [jdtd james dust at, [jdtd james dust ao], [jdtd james particle_at],[jdtd james particle_ao]) ) pvt ) else ( select 'error' ) end
the problem caused fact should return scalar then:
select [jdtd james cutting sludge at] s_at, [jdtd james cutting sludge ao] s_ao, [jdtd james dust at] d_at, [jdtd james dust ao)] d_ao, [jdtd james particle_at] p_at, [jdtd james particle_ao] p_ao ... returns result set, not single value.
you can either use if statement , perform statement each of case - @pcamptype = 'james' / @pcamptype = 'frank' or use dynamic sql:
declare @sql nvarchar(max) = ' testdata ( select testname, convert(decimal(10,2),result) testresult testreg resultdate between @pfromdate , @ptodate , (testrgstr_testname ''%sludge%'' or testrgstr_testname ''%dust%'' or testrgstr_testname ''%particle%'') )' declare @innerselectsql nvarchar(max) = n'' if (@pcamptype = 'james') @innerselectsql = n' select [jdtd james cutting sludge at] s_at, [jdtd james cutting sludge ao] s_ao, [jdtd james dust at] d_at, [jdtd james dust ao)] d_ao, [jdtd james particle_at] p_at, [jdtd james particle_ao] p_ao ' else if (@pcamptype = 'frank') @innerselectsql = n' select [jdtd frank cutting sludge at] s_at, [jdtd james frank sludge ao] s_ao, [jdtd frank dust at] d_at, [jdtd frank dust ao)] d_ao, [jdtd frank particle_at] p_at, [jdtd frank particle_ao] p_ao ' set @sql = @sql + char(13) + char(10) + innerselectsql + char(13) + char(10) + n' testdata pivot ( avg(testresult) testrgstr_testname in ([jdtd james cutting sludge at],[jdtd james cutting sludge ao], [jdtd james dust at, [jdtd james dust ao], [jdtd james particle_at],[jdtd james particle_ao]) ) pvt' dynamic sql uglier, avoid repetition.
Comments
Post a Comment