sql统计上一年的值,时间如何转换
sql统计上一年的值,时间怎么转换
统计上一年的值,提示字符串时间转换出错,请教大神或是这个该怎么写呢
alter proc sp_datefour
@yy char(20), --输入的格式有 年,年月
@kind varchar(10)
as
--存储过程作用说明:传入@yy参数,输入年份,统计上一年的所有的account量,表中的datem是char类型
--@yy 可能输入两种格式 1.年 '2015' 2.年月'201501'
--所以在and条件中加了 or
-- exec sp_datefour '201501','A'
select sum(acount) from datefour
where name = @kind
and ( left(convert(char(10),dateadd(yy,-1,convert(date,datem+'01')),112),4) = @yy-1
or left(convert(char(10),dateadd(yy,-1,convert(date,datem+'01')),112),6)
= left(convert(char(10),dateadd(yy,-1,convert(date,@yy+'01')),112),6) )
测试表脚本如下:
create table datefour
(name varchar(10),datem char(10), acount int)
insert into datefour values
('A','201203',25), ('A','201206',3),
('A','201209',15),('A','201304',8),
('A','201309',4),('A','201312',11),
('A','201403',7),('A','201405',21),
('A','201409',2),('A','201410',10),
('A','201503',21),('A','201509',3),
('B','201203',5),('B','201206',14),
('B','201209',6),('B','201303',9),
('B','201309',11),('B','201310',14),
('B','201405',9), ('B','201409',5),
('B','201410',9),('B','201505',2),
('B','201507',4), ('B','201509',13),
('C','201201',9),('C','201203',4),
('C','201212',11),('C','201303',3),
('C','201308',12),('C','201311',8),
('C','201312',3),('C','201403',8),
('C','201408',5),('C','201410',7),
('C','201503',14),('C','201509',25)
------解决思路----------------------
select convert(int, convert(nvarchar(4), '201501'))-1 as [201501前一年], -- 年月
convert(int, convert(nvarchar(4), '2015'))-1 as [2015前一年] -- 年
------------------------------
结果
201501前一年 2015前一年
2014 2014
------------------------------
declare @kind nvarchar(10)
declare @yy char(10)
set @kind ='A'
set @yy = '201501'
;with datefour(name,datem, acount) as
(
select 'A','201203',25 union all
select 'A','201206',3 union all
select 'A','201209',15 union all
select 'A','201304',8 union all
select 'A','201309',4 union all
select 'A','201312',11 union all
select 'A','201403',7 union all
select 'A','201405',21 union all
select 'A','201409',2 union all
select 'A','201410',10 union all
select 'A','201503',21 union all
select 'A','201509',3 union all
select 'B','201203',5 union all
select 'B','201206',14 union all
select 'C','201201',9 union all
select 'C','201203',4
)
select sum(acount) cnt from datefour
where name = @kind
and convert(nvarchar(4), datem) = convert(int, convert(nvarchar(4), @yy))-1
--------------------------------------
结果:
cnt
40
统计上一年的值,提示字符串时间转换出错,请教大神或是这个该怎么写呢
alter proc sp_datefour
@yy char(20), --输入的格式有 年,年月
@kind varchar(10)
as
--存储过程作用说明:传入@yy参数,输入年份,统计上一年的所有的account量,表中的datem是char类型
--@yy 可能输入两种格式 1.年 '2015' 2.年月'201501'
--所以在and条件中加了 or
-- exec sp_datefour '201501','A'
select sum(acount) from datefour
where name = @kind
and ( left(convert(char(10),dateadd(yy,-1,convert(date,datem+'01')),112),4) = @yy-1
or left(convert(char(10),dateadd(yy,-1,convert(date,datem+'01')),112),6)
= left(convert(char(10),dateadd(yy,-1,convert(date,@yy+'01')),112),6) )
测试表脚本如下:
create table datefour
(name varchar(10),datem char(10), acount int)
insert into datefour values
('A','201203',25), ('A','201206',3),
('A','201209',15),('A','201304',8),
('A','201309',4),('A','201312',11),
('A','201403',7),('A','201405',21),
('A','201409',2),('A','201410',10),
('A','201503',21),('A','201509',3),
('B','201203',5),('B','201206',14),
('B','201209',6),('B','201303',9),
('B','201309',11),('B','201310',14),
('B','201405',9), ('B','201409',5),
('B','201410',9),('B','201505',2),
('B','201507',4), ('B','201509',13),
('C','201201',9),('C','201203',4),
('C','201212',11),('C','201303',3),
('C','201308',12),('C','201311',8),
('C','201312',3),('C','201403',8),
('C','201408',5),('C','201410',7),
('C','201503',14),('C','201509',25)
------解决思路----------------------
select convert(int, convert(nvarchar(4), '201501'))-1 as [201501前一年], -- 年月
convert(int, convert(nvarchar(4), '2015'))-1 as [2015前一年] -- 年
------------------------------
结果
201501前一年 2015前一年
2014 2014
------------------------------
declare @kind nvarchar(10)
declare @yy char(10)
set @kind ='A'
set @yy = '201501'
;with datefour(name,datem, acount) as
(
select 'A','201203',25 union all
select 'A','201206',3 union all
select 'A','201209',15 union all
select 'A','201304',8 union all
select 'A','201309',4 union all
select 'A','201312',11 union all
select 'A','201403',7 union all
select 'A','201405',21 union all
select 'A','201409',2 union all
select 'A','201410',10 union all
select 'A','201503',21 union all
select 'A','201509',3 union all
select 'B','201203',5 union all
select 'B','201206',14 union all
select 'C','201201',9 union all
select 'C','201203',4
)
select sum(acount) cnt from datefour
where name = @kind
and convert(nvarchar(4), datem) = convert(int, convert(nvarchar(4), @yy))-1
--------------------------------------
结果:
cnt
40