查询将一年中每个月的日期作为列表返回?

时间:2014-06-06 14:47:12

标签: sql-server datetime days monthcalendar

我已经在线查看,但找不到可以返回我需要的解决方案,并且希望本网站的专家可以像过去一样帮助我。

我有一个SQL数据库,用于保存与建筑设备相关的带时间戳的维护呼叫。

我可以查询维护信息,例如通话次数,平均工作时间,通话平均开放时间以及按月分组结果(以给出一年中的趋势)。

下一阶段是我提供设备正常运行时间的指示。我有停机时间(为了这个目的,这是打电话的时间),我可以添加一个计数来给出设备的总数。我想根据一年中每个月的小时数,项目数和打电话的总时间来显示设备可供使用的时间百分比(正常运行时间)。

e.g。

  • 月 - 号召唤 - 平均工具时间(小时) - 平均开放时间(小时) - (必需)正常运行时间

  • Jan - 4 - 0.37 - 1.00 - 92%

  • 2月 - 3 - 0.83 - 35.6 - 87%

  • 3月 - 5 - 0.50 - 1.85 - 85%

  • 4月 - 10 - 0.72 - 119.5 - 72%

  1. 有人会建议一个可以返回每个月份中我可以在现有查询中使用的日期的查询吗?

  2. 此外(对不起,请稍等一下)...如果2月份召集电话而4月份没有关闭,我怎能在3月份将停机时间包括在内?有没有办法考虑到这一点?

  3. 我已经在下面提供了完整的查询,我提前为格式化(和质量)道歉。我应该补充一点,这是一个托管数据库,所以除了返回值之外,我不能添加表或做很多事情。

    非常感谢您提供任何帮助或指导。

    以下示例查询

    `SELECT Description as 'Month', 
    (SELECT COUNT(TA_SEQ)
    FROM F_TASKS, FASSET, FAREALO, F_CONTRACT  
    WHERE TA_TASK_DESC IN ('BREAKDOWN')
    AND TA_FKEY_AR_SEQ = AR_SEQ
    AND LO_SEQ = AR_FKEY_LO_SEQ
    AND ( (LO_DESCRIPTION LIKE 'Facility%') OR ((LO_ROOM_NUMBER IN ('G.19', 'G.20')) AND (LO_DESCRIPTION LIKE '%EXIT') ) )
    AND TA_FKEY_CTR_SEQ = CTR_SEQ
    AND CTR_SEQ IN  (100,101)
    AND (TA_SHORT_DESC LIKE '%Door%' AND AR_DESCRIPTION LIKE '%DOOR%')
    AND (TA_HIST_STATUS IS NULL OR TA_HIST_STATUS = 'COMPLETE')
    AND TA_STATUS <> 'CANCELLED'
    AND DATEPART(mm, TA_DUE_DATE)= A.Number
    AND DATEPART(year, TA_DUE_DATE)= DATEPART(year, GETDATE())) AS 'No. Calls Raised', 
    ((ISNULL((SELECT SUM(TA_TIME_TAKEN)
    FROM F_TASKS, F_PPM_DETAILS, F_CONTRACT, FASSET, FAREALO
    WHERE TA_TASK_DESC = 'PPM'
    AND TA_FKEY_AR_SEQ = AR_SEQ
    AND LO_SEQ = AR_FKEY_LO_SEQ
    AND ( (LO_DESCRIPTION LIKE 'Facility%') OR (LO_ROOM_NUMBER IN ('G.19', 'G.20')) )
    AND AR_DESCRIPTION LIKE '%DOOR%'
    AND TA_FKEY_CTR_SEQ = CTR_SEQ 
    AND CTR_SEQ NOT IN (100,101)
    AND PDET_FKEY_TA_SEQ = TA_SEQ
    AND DATEPART(year, TA_DUE_DATE) = DATEPART(year, GETDATE())
    AND DATEPART(month, TA_DUE_DATE)= A.Number ),0)) +
    (ISNULL((SELECT SUM(TA_TIME_TAKEN)
    FROM F_TASKS, F_BD_DETAILS, F_CONTRACT, FASSET, FAREALO
    WHERE TA_TASK_DESC = 'BREAKDOWN'
    AND TA_FKEY_AR_SEQ = AR_SEQ
    AND LO_SEQ = AR_FKEY_LO_SEQ
    AND ( (LO_DESCRIPTION LIKE 'Facility%') OR ((LO_ROOM_NUMBER IN ('G.19', 'G.20')) AND (LO_DESCRIPTION LIKE '%EXIT') ) )
    AND (TA_SHORT_DESC LIKE '%Door%' AND AR_DESCRIPTION LIKE '%DOOR%')
    AND TA_FKEY_CTR_SEQ = CTR_SEQ
    AND CTR_SEQ IN  (100,101)
    AND BDET_FKEY_TA_SEQ = TA_SEQ
    AND BDET_CALLER_SOURCE NOT IN ('Minor')
    AND DATEPART(year, TA_DUE_DATE) = DATEPART(year, GETDATE())
    AND DATEPART(month, TA_DUE_DATE)= A.Number )*1.00 /
    (SELECT COUNT(TA_SEQ)
    FROM F_TASKS, FASSET, FAREALO, F_CONTRACT  
    WHERE TA_TASK_DESC IN ('BREAKDOWN')
    AND TA_FKEY_AR_SEQ = AR_SEQ
    AND LO_SEQ = AR_FKEY_LO_SEQ
    AND ( (LO_DESCRIPTION LIKE 'Facility%') OR ((LO_ROOM_NUMBER IN ('G.19', 'G.20')) AND (LO_DESCRIPTION LIKE '%EXIT') ) )
    AND TA_FKEY_CTR_SEQ = CTR_SEQ
    AND CTR_SEQ IN  (100,101)
    AND (TA_SHORT_DESC LIKE '%Door%' AND AR_DESCRIPTION LIKE '%DOOR%')
    AND (TA_HIST_STATUS IS NULL OR TA_HIST_STATUS = 'COMPLETE')
    AND TA_STATUS <> 'CANCELLED'
    AND DATEPART(mm, TA_DUE_DATE)= A.Number
    AND DATEPART(year, TA_DUE_DATE)= DATEPART(year, GETDATE())) ,0))) AS 'Avg Tool Time (hrs)',
    ISNULL(((SELECT SUM(CASE
    WHEN TA_FINISH_DATE < TA_DUE_DATE THEN DATEDIFF(hh, TA_FINISH_DATE, TA_DUE_DATE)
    WHEN TA_FINISH_DATE >= TA_DUE_DATE THEN DATEDIFF(hh, TA_DUE_DATE, ISNULL(TA_FINISH_DATE,GETDATE()))
    ELSE 0 END)
    FROM F_TASKS, F_BD_DETAILS, F_CONTRACT, FASSET, FAREALO
    WHERE TA_TASK_DESC = 'BREAKDOWN'
    AND TA_FKEY_AR_SEQ = AR_SEQ
    AND LO_SEQ = AR_FKEY_LO_SEQ
    AND ( (LO_DESCRIPTION LIKE 'Facility%') OR ((LO_ROOM_NUMBER IN ('G.19', 'G.20')) AND (LO_DESCRIPTION LIKE '%EXIT') ) )
    AND (TA_SHORT_DESC LIKE '%Door%' AND AR_DESCRIPTION LIKE '%DOOR%')
    AND TA_FKEY_CTR_SEQ = CTR_SEQ
    AND CTR_SEQ IN (100,101)
    AND BDET_FKEY_TA_SEQ = TA_SEQ
    AND BDET_CALLER_SOURCE NOT IN ('Minor','Request')
    AND DATEPART(year, TA_DUE_DATE) = DATEPART(year, GETDATE())
    AND DATEPART(month, TA_DUE_DATE)= A.Number)*1.00)/
    (SELECT COUNT(TA_SEQ)
    FROM F_TASKS, FASSET, FAREALO, F_CONTRACT  
    WHERE TA_TASK_DESC IN ('BREAKDOWN')
    AND TA_FKEY_AR_SEQ = AR_SEQ
    AND LO_SEQ = AR_FKEY_LO_SEQ
    AND ( (LO_DESCRIPTION LIKE 'Facility%') OR ((LO_ROOM_NUMBER IN ('G.19', 'G.20')) AND (LO_DESCRIPTION LIKE '%EXIT') ) )
    AND TA_FKEY_CTR_SEQ = CTR_SEQ
    AND CTR_SEQ IN (100,101)
    AND (TA_SHORT_DESC LIKE '%Door%' AND AR_DESCRIPTION LIKE '%DOOR%')
    AND (TA_HIST_STATUS IS NULL OR TA_HIST_STATUS = 'COMPLETE')
    AND TA_STATUS <> 'CANCELLED'
    AND DATEPART(mm, TA_DUE_DATE)= A.Number
    AND DATEPART(year, TA_DUE_DATE)= DATEPART(year, GETDATE())),0) AS 'Avg Time Open (hrs)'
    FROM Intervals A
    WHERE Type = 'Month' 
    ORDER BY Number`
    

    查询结束

    非常感谢 加里

1 个答案:

答案 0 :(得分:0)

您可能想要检查一个月中的小时数是否应仅包含工作日的工作时数,否则,您的百分比可能会有所偏差。这适用于sql server 2012和DATEFROMPARTS。您可能需要为此提出自己的功能。为简洁起见,我省略了大部分查询,并将字段设置为零。但基本概念是相似的。

SELECT
    Detail.Month,
    Detail.Number,
    SumNoCallsMade=SUM(NoCallsRaised),
    AvgAvgToolTime=AVG(AvgToolTimeHrs),
    AvgAvgTimeOpen=AVG(AvgTimeOpenHrs),
    HoursInMonth=MAX(HoursInMonth),
    AverageUpTime=AVG(AvgToolTimeHrs) / CAST(MAX(Detail.HoursInMonth) AS DECIMAL(18,2)),
    AverageOpenTime=AVG(AvgTimeOpenHrs) / CAST(MAX(Detail.HoursInMonth) AS DECIMAL(18,2))
FROM
(
    SELECT 
        A.Month,
        A.Number,
        NoCallsRaised=0,
        AvgToolTimeHrs=0,
        AvgTimeOpenHrs=0,
        HoursInMonth=(24 * DATEPART(DAY, DATEADD(DAY, -1, DATEADD(MONTH, 1, DATEFROMPARTS( DATEPART(YEAR,GETDATE()),A.Number,1)))))

    FROM 
        Intervals A
    WHERE 
        A.Type = 'Month' 
)AS Detail
GROUP BY
    Detail.Month,
    Detail.Number
ORDER BY 
    Detail.Number