where语句里面含有动态变量

elvalin1559 2012-06-12 09:51:56
想在reporting service里面的查询语句用动态语句 变量是一个日期范围起始月份和终止月份

假设输入变量是1月-3月

最后的输出是

销售(今年迄今)
1月 a
2月 b
3月 c

那查询语句就是
select sum(sales)
from sales
where year = 2012 and month <= @month

a 就是当@month =1 b的值 就是当@month =2时候 sum(sales)的值 以此类推

但是怎么才能报表里面用这样一个动态的值呢?

可以建立临时表么?
...全文
258 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
shoppo0505 2012-06-21
  • 打赏
  • 举报
回复
添加两个变量,@monthfrom 和 @monthto,可以利用内值函数自己随意定义初始值
SQL执行的时候,读取这两个变量,也可以自己给入,在刷新报表。
十林 2012-06-17
  • 打赏
  • 举报
回复
其实Reporting service 里面有一个函数RunningValue(Field!sales.value,Sum,nothing)可以实现这个事情,

--查询语句
select month,sum(sales) as sales
from sales
where year = 2012 and month between @monthfrom and @monthto
group by month
order by month/*order by 这里很关键,因为RunningValue是在一个group范围内,将上一条记录的值累加到本条,所以顺序很关键。上面的nothing 表示全局范围。如果你想在某个group的范围内进行累加,可以把nothing 改成 “GroupName”*/


--报表的设计使用表格,在sales的值得表格单元中填入=RunningValue(Field!sales.value,Sum,nothing)
设计大体如下:
month sales
=Field!month.value =RunningValue(Field!sales.value,Sum,nothing)

楼主如果不太懂,可以先看看RunningValue这个函数的用法。
叶子 2012-06-12
  • 打赏
  • 举报
回复
select sum(sales)
from sales
where year = 2012 and month <= @month and @month=2
union all
select b
from sales
where year = 2012 and month <= @month and @month=1


貌似是这个意思??

695

社区成员

发帖
与我相关
我的任务
社区描述
提出问题
其他 技术论坛(原bbs)
社区管理员
  • community_281
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧