使用T-SQL逐月比较没有开放帐户和已关闭帐户

时间:2013-12-03 12:21:43

标签: sql

我有一个名为caseheader的表和另一个名为lookup Item的表。我想通过使用这个公式计算案件结案率:

  

(一个月内关闭的总案例数)/ month * 100

中的总公开案例数

计算opencases CaseCreationDate用于在一个月内打开案件,无论它是否关闭。

计算ClosedCase CaseCompletionDate用于获取一个月内的已结案例。

某一年内关闭的案件总数

SELECT DISTINCT
  YEAR(CasecompletionDate) AS YearCompleted
 ,DATENAME( MONTH , DATEADD( MONTH , MONTH(ch.CaseCompletionDate) , -1 ) ) AS 'Month Name Closed'   
 ,MONTH(CasecompletionDate) AS MonCompleted
 ,COUNT(CaseHeaderId) OVER(PARTITION BY MONTH(casecompletiondate)) AS ClosedBYMonth
FROM  CaseHeader CH
    JOIN LookupItem LI3
        ON LI3.LookupItemId = CH.CaseStageLKId  
        AND LI3.IsDeleted = 0
WHERE YEAR(CaseCompletionDate) = 2013 
AND LI3.ItemName IN ('Screening', 'In the form required')
AND ch.isdeleted = 0
ORDER BY MONTH(CasecompletionDate)

特定年份的总开放案例

SELECT DISTINCT
  YEAR(CaseCreationDate) AS YearOpen
 ,DATENAME( MONTH , DateAdd( MONTH , MONTH(ch.CaseCreationDate) , -1 ) ) AS 'Month Name Opened' 
 ,MONTH(CaseCreationDate) AS MonOpen
 ,COUNT(CaseHeaderId) OVER(PARTITION BY MONTH(CaseCreationDate)) as NewBYMonth
FROM  CaseHeader CH
    JOIN LookupItem LI3
        ON LI3.LookupItemId = CH.CaseStageLKId  
        AND LI3.IsDeleted = 0
WHERE year(CaseCreationDate) = 2013 
AND LI3.ItemName not like 'Restor%'
AND ch.isdeleted = 0
ORDER BY MONTH(CaseCreationDate) 

我应该如何根据给定的公式加入这个以获得关闭率?

2 个答案:

答案 0 :(得分:0)

这样的事情会起作用吗?

SELECT
    2013 AS intYear,
    MonCompleted AS intMonth,
    DATENAME( MONTH, (MonCompleted AS VARCHAR) + '/01/2013' ) AS MonthName,
    ClosedBYMonth,
    NewBYMonth,
    ClosedBYMonth/NewBYMonth * 100 AS calc
FROM
(
SELECT
    MONTH(CasecompletionDate) AS MonCompleted,
    COUNT(CaseHeaderId) AS ClosedBYMonth

FROM  
    CaseHeader CH
INNER JOIN 
    LookupItem LI3
            ON LI3.LookupItemId = CH.CaseStageLKId        
WHERE   
CaseCompletionDate BETWEEN '01/01/2013' AND '12/31/2013'
AND LI3.ItemName IN ('Screening', 'In the form required')
AND ch.isdeleted = 0
AND LI3.IsDeleted = 0
GROUP BY 
    MONTH(CasecompletionDate)
) AS ClosedCases

INNER JOIN
(
SELECT 
 ,MONTH(CaseCreationDate) AS MonOpen
 ,COUNT(CaseHeaderId) as NewBYMonth
FROM  
    CaseHeader CH
INNER JOIN 
    LookupItem LI3
            ON LI3.LookupItemId = CH.CaseStageLKId          
WHERE 
CaseCreationDate BETWEEN '01/01/2013' AND '12/31/2013'
AND LI3.ItemName not like 'Restor%'
AND ch.isdeleted = 0
AND LI3.IsDeleted = 0
GROUP BY 
MONTH(CaseCreationDate) 
) AS AS OpenCases
    ON ClosedCases.MonCompleted = OpenCases.MonOpen

答案 1 :(得分:0)

宣布@YEAR AS INT SET @YEAR = 2013 选择     @Year AS intYear,     MonCompleted AS intMonth,     MonthNameCompleted AS MonthName,     ClosedBYMonth,     NewBYMonth,     CAST(ClosedBYMonth AS DECIMAL(9,2))/(NewBYMonth)* 100作为ClosureRate

这     (     选择         MONTH(CasecompletionDate)作为MonCompleted,         DateName(月,DateAdd(月,MONTH(CaseCompletionDate), - 1))AS'YenderNameCompleted',         COUNT(CaseHeaderId)AS ClosedBYMonth

FROM  
    CaseHeader CH
INNER JOIN 
    LookupItem LI3
            ON LI3.LookupItemId = CH.CaseStageLKId        
WHERE   
YEAR(CaseCompletionDate) =@YEAR 
AND LI3.ItemName IN ('Screening', 'In the form required')
AND ch.isdeleted = 0
AND LI3.IsDeleted = 0
GROUP BY MONTH(CasecompletionDate)
) as ClosedCases

INNER JOIN     (选择       MONTH(CaseCreationDate)AS MonOpen      ,DateName(月,DateAdd(月,MONTH(CaseCreationDate), - 1))AS'OnthNameNameOpen'      ,COUNT(CaseHeaderId)为NewBYMonth     从
        CaseHeader CH     内部联接         LookupItem LI3                 ON LI3.LookupItemId = CH.CaseStageLKId
    哪里     年(CaseCreationDate)= @YEAR     AND LI3.ItemName不喜欢'Restor%'     AND ch.isdeleted = 0     AND LI3.IsDeleted = 0     按月分组(CaseCreationDate)     )AS OpenCases     ON ClosedCases.MonCompleted = OpenCases.MonOpen