2

需要格式为 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)
4

3 回答 3

9

我认为最简单的方法如下:

CONVERT(VARCHAR(7), RH.updated_datetime, 126)

但是,我从来都不喜欢在应用程序层之前将日期转换为字符串,所以如果是我,我会将其保留为日期格式,但使用以下方法将每个日期转换为每月的第一天:

DATEADD(MONTH, DATEDIFF(MONTH, 0, RH.updated_datetime), 0)

这意味着您的应用程序将列作为日期接收,并且可以作为日期进行操作,按日期排序等,然后如果您确实想要显示它,yyyy-mm您可以在最后一分钟进行格式化。

于 2013-06-26T07:37:53.153 回答
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
于 2013-06-26T07:01:48.183 回答
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))
于 2013-06-26T07:04:32.313 回答