在一个RS报表中需要实现一年内各月份"报价总额"的统计,但是报价表中并不是每个月都有数据,某些月份没有数据,那么需要一个月份表(1-12月)并使用left join来连接报价表。如何用简单等方法生产一个月份表?
with MonthList(m)
as
(
select m =1
union all
select m + 1 from MonthList where m < 12
)
select m from MonthList
结果:
1
2
3
4
5
6
7
8
9
10
11
12
with MonthList(m)
as
(
select m =1
union all
select m + 1 from MonthList where m < 12
)
select m from MonthList
结果:
1
2
3
4
5
6
7
8
9
10
11
12
A common table expression (CTE) can be thought of as a temporary result set that is defined within the execution scope of a single SELECT, INSERT, UPDATE, DELETE, or CREATE VIEW statement. A CTE is similar to a derived table in that it is not stored as an object and lasts only for the duration of the query. Unlike a derived table, a CTE can be self-referencing and can be referenced multiple times in the same query.
http://msdn2.microsoft.com/en-us/library/ms190766.aspx
本文介绍了一个在RS报表中实现一年内各月份报价总额统计的方法。针对报价表中部分月份缺失数据的问题,通过创建一个包含全年12个月份的月份表,并使用LEFT JOIN将其与报价表进行连接,从而确保报表中每个月份都有对应的记录。文章还提供了生成月份表的具体SQL代码示例。
1698

被折叠的 条评论
为什么被折叠?



