sql实现提取当前月份及当前月份后面5个月的数据,并有跨年度存在
表结构说明
表中包含了某产品08-09年的出货记录,有月份和年份的字段。
需要得到的结果为显示本月份及本月份后面5个月份的数据。如果为9月份的话,将要在结果中添加2009年度
1月份和2月份的数据
table_1
order_id order_year m1 m2 m3 m4 m5 m6 m7 m8 m9 m10 m11 m12 prd_id
1 2008 1 2 3 4 5 6 7 8 9 10 11 12 prd_1
2 2009 11 22 33 44 55 66 0 0 0 0 0 0 prd_1
本月份为4月,那么需要的结果为
order_id prd_id 2008_m4 2008_m5 2008_m6 2008_m7 2008_m8 2008_m9
1 prd_id 4 5 6 7 8 9
如果本月份为9,那么需要的结果为
order_id 2008_m9 2008_m10 2008_m11 2008_m12 2008_m1 2009_m2
1 9 10 11 12 11 22
建表语句如下
表中包含了某产品08-09年的出货记录,有月份和年份的字段。
需要得到的结果为显示本月份及本月份后面5个月份的数据。如果为9月份的话,将要在结果中添加2009年度
1月份和2月份的数据
table_1
order_id order_year m1 m2 m3 m4 m5 m6 m7 m8 m9 m10 m11 m12 prd_id
1 2008 1 2 3 4 5 6 7 8 9 10 11 12 prd_1
2 2009 11 22 33 44 55 66 0 0 0 0 0 0 prd_1
本月份为4月,那么需要的结果为
order_id prd_id 2008_m4 2008_m5 2008_m6 2008_m7 2008_m8 2008_m9
1 prd_id 4 5 6 7 8 9
如果本月份为9,那么需要的结果为
order_id 2008_m9 2008_m10 2008_m11 2008_m12 2008_m1 2009_m2
1 9 10 11 12 11 22
建表语句如下
create table table_1(order_id smallint,order_year smallint,m1 smallint,m2 smallint,m3 smallint,m4 smallint,m5 smallint,m6 smallint,m7 smallint,m8 smallint,m9 smallint,m10 smallint,m11 smallint,m12 smallint,prd_id char(20))
insert into table_1(order_id,order_year,m1,m2,m3,m4,m5,m6,m7,m8,m9,m10,m11,m12,prd_id)
select 1,2008,1,2,3,4,5,6,7,8,9,10,11,12,'prd_1'
union all select 2,2009,11,22,33,44,55,66,0,0,0,0,0,0,'prd_1'
...全文
相关推荐
SQL对数据进行按月统计或对数据进行按星期统计的实例代码
主要介绍了SQL对数据进行按月统计或对数据进行按星期统计的实例代码,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
SQL按照日、周、月、年统计数据的方法分享
–按日 select sum(consume),day([date]) from consume_record where year([date]) = ‘2006’ group by day([date]) –按周quarter select sum(consume),datename(week,[date]) from consume_record where year([date]) = ‘2006’ group by datename(week,[date]) –按月 select sum(consume),month([date]) from consume_record wh
根据月份返回该月份的所在季度的经典SQL
此方法可将月份值代入公式直接求出所在季度,不需要IF CASE 之类的判断,纯数学公式得到,下载前可自己先动手试试难度量。适用于不能用 判断语句 的编程环境
SQL存储过程根据年份和月份,返回全月所有日期和星期
SQL存储过程根据年份和月份,返回全月所有日期和星期几
mysql 月份查询该月有付款的数据
mysql选择月份查询该月有付款的数据(间隔x月并提前5天付款)



