我需要修改此现有代码,以按财政年度(从7月1日开始)和季度进行分组,我意识到,这是查询的下面部分,需要修改,但无法正常工作
datepart(yy,EnquiryRaised)*100+datepart(mm,EnquiryRaised) as MonthYear,
year(EnquiryRaised) as year,
datename(month,(EnquiryRaised)) as Month
完整查询是
SELECT
enquiry_number,
service_code,
service_name,
subject_code,
subject_name,
EnquiryRaised,
NoAction,
DATEDIFF(dd, EnquiryRaised, NoAction) as NoActionDuration,
ActionScheduled,
DATEDIFF(dd, EnquiryRaised, ActionScheduled) as ScheduledDuration,
datepart(yy,EnquiryRaised)*100+datepart(mm,EnquiryRaised) as MonthYear,
year(EnquiryRaised) as year,
datename(month,(EnquiryRaised)) as Month
FROM
(SELECT
dbo.central_enquiry.enquiry_number,
dbo.central_enquiry.service_code,
dbo.type_of_service.service_name,
dbo.central_enquiry.subject_code,
dbo.enquiry_subject.subject_name,
(SELECT TOP 1
dbo.enquiry_status_log.logged_date
FROM dbo.enquiry_status_log
WHERE dbo.enquiry_status_log.enquiry_number = dbo.central_enquiry.enquiry_number AND
dbo.enquiry_status_log.enq_status_code = 'E010'
ORDER BY dbo.enquiry_status_log.enquiry_log_number) as EnquiryRaised,
(SELECT TOP 1
dbo.enquiry_status_log.logged_date
FROM dbo.enquiry_status_log
WHERE dbo.enquiry_status_log.enquiry_number = dbo.central_enquiry.enquiry_number AND
dbo.enquiry_status_log.enq_status_code IN ('E030', 'I030', 'N010', 'R030', 'XXXX')
ORDER BY dbo.enquiry_status_log.enquiry_log_number) as NoAction,
(SELECT TOP 1
dbo.enquiry_status_log.logged_date
FROM dbo.enquiry_status_log
WHERE dbo.enquiry_status_log.enquiry_number = dbo.central_enquiry.enquiry_number AND
dbo.enquiry_status_log.enq_status_code = 'W010'
ORDER BY dbo.enquiry_status_log.enquiry_log_number) as ActionScheduled
FROM dbo.central_enquiry
LEFT JOIN dbo.type_of_service ON dbo.central_enquiry.service_code = dbo.type_of_service.service_code
LEFT JOIN dbo.enquiry_subject ON dbo.central_enquiry.subject_code = dbo.enquiry_subject.subject_code
) as EnquiryLog
ORDER BY enquiry_number
样本数据
enquiry_number,service_code,service_name,subject_code,subject_name,enquiryraised,noaction,noactionduration,actionscheduled,scheduledduration,monthyear,year,month
144033,BP,Buildings and Properties,BPBM,Building Maintenance,1/01/2019 17:52:30,,,7/01/2019 15:38:44,6,201901,2019,January
144034,PS,Park Services,PSPI,Park Infrastructure,2/01/2019 06:59:16,21/01/2019 15:40:23,19,,,201901,2019,January
144035,PSIN,Park Services Internal Request,PIOT,Other,2/01/2019 08:32:55,,,13/02/2019 07:45:57,42,201901,2019,January
144036,PS,Park Services,TRIO,Inspection of Tree,2/01/2019 08:36:16,2/01/2019 09:42:20,0,,,201901,2019,January
144037,PS,Park Services,PSSG,Sports Ground Maintenance,2/01/2019 08:41:08,,,3/01/2019 09:38:22,1,201901,2019,January
144038,GW,Glenworks,GWDR,Drainage Glenworks,2/01/2019 08:47:45,,,8/01/2019 09:54:10,6,201901,2019,January
144039,PSIN,Park Services Internal Request,PIOT,Other,2/01/2019 08:48:37,,,13/02/2019 07:44:33,42,201901,2019,January
144040,BP,Buildings and Properties,BPBM,Building Maintenance,2/01/2019 08:53:41,,,2/01/2019 09:22:26,0,201901,2019,January
144041,PSIN,Park Services Internal Request,PIOT,Other,2/01/2019 09:02:53,,,13/02/2019 07:43:22,42,201901,2019,January
144042,PSIN,Park Services Internal Request,PIRB,Rubbish,2/01/2019 09:13:18,,,21/01/2019 15:41:18,19,201901,2019,January
144043,PS,Park Services,PSPI,Park Infrastructure,2/01/2019 09:16:48,,,2/01/2019 13:17:32,0,201901,2019,January
144044,PSIN,Park Services Internal Request,PISP,Supplies,2/01/2019 09:18:13,3/01/2019 12:03:42,1,,,201901,2019,January
144045,GW,Glenworks,GWGP,Graffiti Private Property,2/01/2019 09:32:37,,,2/01/2019 10:04:26,0,201901,2019,January
144046,PS,Park Services,TRIO,Inspection of Tree,2/01/2019 09:31:58,2/01/2019 10:00:49,0,,,201901,2019,January
144047,PS,Park Services,TRIO,Inspection of Tree,2/01/2019 09:34:11,,,3/01/2019 10:49:46,1,201901,2019,January
144048,GW,Glenworks,GWGP,Graffiti Private Property,2/01/2019 09:40:04,,,3/01/2019 08:02:54,1,201901,2019,January
144049,GW,Glenworks,GWGP,Graffiti Private Property,2/01/2019 09:41:20,2/01/2019 13:50:10,0,,,201901,2019,January
144050,GW,Glenworks,GWGP,Graffiti Private Property,2/01/2019 09:42:07,,,3/01/2019 07:21:35,1,201901,2019,January
144051,GW,Glenworks,GWGP,Graffiti Private Property,2/01/2019 09:43:13,3/01/2019 07:00:55,1,,,201901,2019,January
144052,GW,Glenworks,GWGP,Graffiti Private Property,2/01/2019 09:44:09,3/01/2019 07:02:47,1,,,201901,2019,January
144053,GW,Glenworks,GWGP,Graffiti Private Property,2/01/2019 09:46:02,,,3/01/2019 07:44:25,1,201901,2019,January
如下所示的布局,但显示的是财务Yaer ^ qtrs,而不是月份
答案 0 :(得分:0)
如果您需要将日期转换为从7月和相应的年度季度开始的财政年度,请从该日期减去6个月,并获得year&quarter:
var newcar = [];
var myvar = [];
for (var i=0; i<(car.length)-1; i++) {
if(car[i].make == car[i+1].make)
{
myvar.push(car[i]);
if(i+1 == car.length-1)
{
myvar.push(car[i+1]);
newcar.push(myvar[0]);
var lastitem = myvar.pop();
newcar.push(lastitem);
}
}
else
{
myvar.push(car[i]);
newcar.push(myvar[0]);
var lastitem = myvar.pop();
newcar.push(lastitem);
myvar = [];
}
}
答案 1 :(得分:0)
下面是用于计算"Fruit": [
{
"Name": "Mango",
"Color": "Yellow",
"Size": "Large"
},
{
"Name": "Grape",
"Color": "Black",
"Size": "Small"
},
{
"Name": "Banana",
"Color": "Yellow",
"Size": "Medium"
}
],
"Vehicle": [
{
"Name": "Hurricane",
"Company": "Lamborgini",
"Wheels": "4"
},
{
"Name": "Ninja",
"Company": "Kawasaki",
"Wheels": "2"
}
]
和quarter
列的代码。由于您没有提供四分之一符号的确切定义,因此我做了两种选择(从一月(定义1)或七月(定义2)开始)。
从7月开始,请注意year_quarter
定义中的year - 1
。这样可以确保year_quarter_2
长期滞后于2018Q3
,这更像是如何定义学校宿舍。如果使用其他定义,则可以轻松更改它们。
代码:
2018Q2
输出(重复的行将被忽略):
select
/* quarter*/
-- ordinary
floor( (month(EnquiryRaised) - 1) / 3 + 1) as quarter_1,
-- start from July
case
when month(EnquiryRaised) in (1,2,3) then 3
when month(EnquiryRaised) in (4,5,6) then 4
when month(EnquiryRaised) in (7,8,9) then 1
when month(EnquiryRaised) in (10,11,12) then 2
end as quarter_2,
/* year-quarter */
-- ordinary
case
when month(EnquiryRaised) in (1,2,3) then cast(year(EnquiryRaised) as varchar) + 'Q1'
when month(EnquiryRaised) in (4,5,6) then cast(year(EnquiryRaised) as varchar) + 'Q2'
when month(EnquiryRaised) in (7,8,9) then cast(year(EnquiryRaised) as varchar) + 'Q3'
when month(EnquiryRaised) in (10,11,12) then cast(year(EnquiryRaised) as varchar) + 'Q4'
end as year_quarter_1,
-- start from July
case
when month(EnquiryRaised) in (1,2,3) then cast(year(EnquiryRaised)-1 as varchar) + 'Q3'
when month(EnquiryRaised) in (4,5,6) then cast(year(EnquiryRaised)-1 as varchar) + 'Q4'
when month(EnquiryRaised) in (7,8,9) then cast(year(EnquiryRaised) as varchar) + 'Q1'
when month(EnquiryRaised) in (10,11,12) then cast(year(EnquiryRaised) as varchar) + 'Q2'
end as year_quarter_2
from #raw; -- #raw is the data you provide