如何根据开始日期和结束日期计算季节
How to calculate season base on startdate and end date
我想在此温度中添加季节描述和季节 table
基于参数 startDate 和 endDate 与 catchdate 比较的日期选择
seaon 定义每年的季节从 10 月 1 日开始,到次年的 9 月 30 日结束
例如,如果捕获日期在 2013 年 10 月 1 日到 2014 年 9 月 30 日之间,这将是 2013 年的季节,季节描述将是 2013 年 10 月 1 日到 2014 年 9 月 30 日
我正在考虑编写函数以根据日期选择获取季节,但不知道如何开始。这是我的温度 table
enter code here
If(OBJECT_ID('tempdb..#tempcatch') Is Not Null)/****** Script for SelectTopNRows command from SSMS ******/
drop table #tempcatch;
create table #tempcatch
(
[id] int ,
[customerId] int,
[catchDate] datetime,
[submissionDate] datetime,
[speciesId] int,
[catchStatusId] int,
[approvedDate] datetime
)
INSERT INTO #tempcatch VALUES (1, 123,'01-01-2013','',1,1,'' );
INSERT INTO #tempcatch VALUES (2, 124,'05-30-2013','',1,1,'' );
INSERT INTO #tempcatch VALUES (3, 125, '06-01-2013','',1,1,'' );
INSERT INTO #tempcatch VALUES (4, 126, '07-12-2013','',1,1,'' );
INSERT INTO #tempcatch VALUES (5, 123, '08-01-2013','',1,1,'' );
INSERT INTO #tempcatch VALUES (6, 124, '10-01-2013','',1,1,'' );
INSERT INTO #tempcatch VALUES (7, 125, '10-28-2013','',1,1,'' );
INSERT INTO #tempcatch VALUES (8, 126, '09-10-2013','',1,1,'' );
INSERT INTO #tempcatch VALUES (9, 127, '01-01-2014','',1,1,'' );
INSERT INTO #tempcatch VALUES (10, 128, '02-15-2014','',1,1,'' );
INSERT INTO #tempcatch VALUES (11, 129, '03-10-2014','',1,1,'' );
INSERT INTO #tempcatch VALUES (12, 130, '03-11-2014','',1,1,'' );
INSERT INTO #tempcatch VALUES (13, 131,'04-20-2014','',1,1,'' );
INSERT INTO #tempcatch VALUES (14, 132,'06-29-2014','',1,1,'' );
INSERT INTO #tempcatch VALUES (15, 133, '08-25-2014','',1,1,'' );
INSERT INTO #tempcatch VALUES (16, 134, '07-12-2014','',1,1,'' );
INSERT INTO #tempcatch VALUES (17, 135, '08-01-2014','',1,1,'' );
INSERT INTO #tempcatch VALUES (18, 136, '10-01-2014','',1,1,'' );
INSERT INTO #tempcatch VALUES (19, 137, '12-28-2014','',1,1,'' );
INSERT INTO #tempcatch VALUES (20, 138, '01-10-2015','',1,1,'' );
INSERT INTO #tempcatch VALUES (21, 139, '01-12-2015','',1,1,'' );
INSERT INTO #tempcatch VALUES (22, 140, '02-09-2015','',1,1,'' );
INSERT INTO #tempcatch VALUES (23, 141, '02-20-2015','',1,1,'' );
INSERT INTO #tempcatch VALUES (24, 142, '02-26-2015','',1,1,'' );
select id,customerId,catchDate,submissionDate,speciesId,catchStatusId,approvedDate,'seasonDescription'='','Season'=''
from #tempcatch
一个简单的 case 表达式就可以做到。
SELECT id
, customerId
, catchDate
, submissionDate
, speciesId
, catchStatusId
, approvedDate
, '' as 'seasonDescription'
, case when MONTH(catchDate) >= 10 then YEAR(catchDate) + 1 else YEAR(catchDate) end as Season
FROM #tempcatch
我想在此温度中添加季节描述和季节 table 基于参数 startDate 和 endDate 与 catchdate 比较的日期选择 seaon 定义每年的季节从 10 月 1 日开始,到次年的 9 月 30 日结束 例如,如果捕获日期在 2013 年 10 月 1 日到 2014 年 9 月 30 日之间,这将是 2013 年的季节,季节描述将是 2013 年 10 月 1 日到 2014 年 9 月 30 日 我正在考虑编写函数以根据日期选择获取季节,但不知道如何开始。这是我的温度 table
enter code here
If(OBJECT_ID('tempdb..#tempcatch') Is Not Null)/****** Script for SelectTopNRows command from SSMS ******/
drop table #tempcatch;
create table #tempcatch
(
[id] int ,
[customerId] int,
[catchDate] datetime,
[submissionDate] datetime,
[speciesId] int,
[catchStatusId] int,
[approvedDate] datetime
)
INSERT INTO #tempcatch VALUES (1, 123,'01-01-2013','',1,1,'' );
INSERT INTO #tempcatch VALUES (2, 124,'05-30-2013','',1,1,'' );
INSERT INTO #tempcatch VALUES (3, 125, '06-01-2013','',1,1,'' );
INSERT INTO #tempcatch VALUES (4, 126, '07-12-2013','',1,1,'' );
INSERT INTO #tempcatch VALUES (5, 123, '08-01-2013','',1,1,'' );
INSERT INTO #tempcatch VALUES (6, 124, '10-01-2013','',1,1,'' );
INSERT INTO #tempcatch VALUES (7, 125, '10-28-2013','',1,1,'' );
INSERT INTO #tempcatch VALUES (8, 126, '09-10-2013','',1,1,'' );
INSERT INTO #tempcatch VALUES (9, 127, '01-01-2014','',1,1,'' );
INSERT INTO #tempcatch VALUES (10, 128, '02-15-2014','',1,1,'' );
INSERT INTO #tempcatch VALUES (11, 129, '03-10-2014','',1,1,'' );
INSERT INTO #tempcatch VALUES (12, 130, '03-11-2014','',1,1,'' );
INSERT INTO #tempcatch VALUES (13, 131,'04-20-2014','',1,1,'' );
INSERT INTO #tempcatch VALUES (14, 132,'06-29-2014','',1,1,'' );
INSERT INTO #tempcatch VALUES (15, 133, '08-25-2014','',1,1,'' );
INSERT INTO #tempcatch VALUES (16, 134, '07-12-2014','',1,1,'' );
INSERT INTO #tempcatch VALUES (17, 135, '08-01-2014','',1,1,'' );
INSERT INTO #tempcatch VALUES (18, 136, '10-01-2014','',1,1,'' );
INSERT INTO #tempcatch VALUES (19, 137, '12-28-2014','',1,1,'' );
INSERT INTO #tempcatch VALUES (20, 138, '01-10-2015','',1,1,'' );
INSERT INTO #tempcatch VALUES (21, 139, '01-12-2015','',1,1,'' );
INSERT INTO #tempcatch VALUES (22, 140, '02-09-2015','',1,1,'' );
INSERT INTO #tempcatch VALUES (23, 141, '02-20-2015','',1,1,'' );
INSERT INTO #tempcatch VALUES (24, 142, '02-26-2015','',1,1,'' );
select id,customerId,catchDate,submissionDate,speciesId,catchStatusId,approvedDate,'seasonDescription'='','Season'=''
from #tempcatch
一个简单的 case 表达式就可以做到。
SELECT id
, customerId
, catchDate
, submissionDate
, speciesId
, catchStatusId
, approvedDate
, '' as 'seasonDescription'
, case when MONTH(catchDate) >= 10 then YEAR(catchDate) + 1 else YEAR(catchDate) end as Season
FROM #tempcatch