1

Not sure how to do this so was hoping that someone could help, I have a multivalue parameter we shall call 'Week', this has a drop down of 1 through to 4. My data set example is :-

select total from tableA where Week in (@Week)

what I want to do is divide the total by the number of options I pick from the drop down, e.g. if I picked Week 1 & 2, I would want the TSQL statement to use the count of 2 as the value to divide by e.g.

select total/2 from tableA where Week in (@Week)

is this possible?

Thanks P

4

2 回答 2

2

Have a look into using the Average function,

SELECT AVG(total) FROM tableA WHERE Week IN(@Week)
于 2013-07-09T13:14:25.140 回答
1

我将使用用户定义的拆分函数将多值参数转换为表中的行。这些已经发布了好几次了。然后你可以简单地做这样的事情:

select sum(A.total), count(distinct Weeks.items)
from tableA as A
inner join dbo.Split(@week) as Weeks on Weeks.items = A.Weeks
于 2013-07-09T15:31:21.020 回答