需要格式为yyyy-mm的日期,并且应该分组以获取 计数,但当我给出
group by date is says invalid
我找到了这个解决方案
任何人都可以通过提供其他解决方案来帮助我
select
count(*) as count,
Convert(char(10), RH.updated_datetime, 105) as date,
SUM(
datediff(SECOND,
PRI.procedure_performed_datetime ,RH.updated_datetime
)
)/count(*) as average_reporting_tat
from
report R,
report_history RH,
study S,
procedure_runtime_information PRI,
priorities PP,
patient P,
"procedure" PR
where
RH.report_fk=R.pk and RH.pk IN (
select pk from (
select * from report_history where report_fk=r.pk
) as result
where old_status_fk IN (21, 27)
) AND R.study_fk = S.pk
AND S.procedure_runtime_fk = PRI.pk
AND PRI.procedure_fk = PR.pk
AND S.priority_fk = PP.pk
AND PRI.patient_fk = P.pk
AND RH.updated_datetime >= '2012-05-01'
AND RH.updated_datetime <= '2013-09-12'
group by Convert(char(10), RH.updated_datetime, 105)
答案 0 :(得分:5)
我认为最简单的方法如下:
CONVERT(VARCHAR(7), RH.updated_datetime, 126)
但是,我从来不喜欢在应用程序层之前将日期转换为字符串,所以如果是我,我会将其保留为日期格式,但使用以下日期将每个日期转换为:
DATEADD(MONTH, DATEDIFF(MONTH, 0, RH.updated_datetime), 0)
这意味着您的应用程序会将该列作为日期接收,并可以作为日期进行操作,按日期排序等,如果您确实要将其显示为yyyy-mm
,则可以在最后执行格式化分钟。
答案 1 :(得分:0)
试试这个:
select count(*) as count,Convert(char(10), RH.updated_datetime, 105) as date,
(CAST(MONTH(RH.updated_datetime) AS VARCHAR(2)) + '-' + CAST(YEAR(RH.updated_datetime) AS VARCHAR(4))),
SUM(datediff(SECOND,PRI.procedure_performed_datetime ,RH.updated_datetime))/count(*) as average_reporting_tat
from
report R,
report_history RH,
study S,
procedure_runtime_information PRI,
priorities PP,
patient P,
"procedure" PR
where
RH.report_fk=R.pk and RH.pk IN (select pk from (select * from report_history where report_fk=r.pk ) as result where old_status_fk IN (21, 27))
AND R.study_fk = S.pk
AND S.procedure_runtime_fk = PRI.pk
AND PRI.procedure_fk = PR.pk
AND S.priority_fk = PP.pk
AND PRI.patient_fk = P.pk
AND RH.updated_datetime >= '2012-05-01'
AND RH.updated_datetime <= '2013-09-12'
group by (CAST(MONTH(RH.updated_datetime) AS VARCHAR(2)) + '-' + CAST(YEAR(RH.updated_datetime) AS VARCHAR(4))),date
答案 2 :(得分:0)
试试这个 -
SELECT
[count] = COUNT(1)
, [date] = CAST(YEAR(RH.updated_datetime) AS VARCHAR(4)) + '-' + CAST(MONTH(RH.updated_datetime) AS VARCHAR(2))
, average_reporting_tat = SUM(DATEDIFF(SECOND, PRI.procedure_performed_datetime ,RH.updated_datetime))/COUNT(1)
FROM dbo.report R
JOIN dbo.report_history RH ON RH.report_fk=R.pk
JOIN dbo.study S ON R.study_fk = S.pk
JOIN dbo.procedure_runtime_information PRI ON S.procedure_runtime_fk = PRI.pk
JOIN dbo.priorities PP ON S.priority_fk = PP.pk
JOIN dbo.patient P ON PRI.patient_fk = P.pk
JOIN dbo.[procedure] PR ON PRI.procedure_fk = PR.pk
WHERE RH.pk IN (
SELECT pk
FROM report_history
WHERE report_fk=r.pk AND old_status_fk IN (21, 27)
)
AND RH.updated_datetime BETWEEN '2012-05-01' AND '2013-09-12'
GROUP BY CAST(YEAR(RH.updated_datetime) AS VARCHAR(4)) + '-' + CAST(MONTH(RH.updated_datetime) AS VARCHAR(2))