SqlServer:每天“未解决”问题的数量

时间:2019-01-30 20:23:02

标签: sql sql-server

此查询出现错误“消息102,级别15,状态1,第12行 ';'“附近的语法不正确,并且已经使用了几个小时。我正在尝试计算给定日期(理想情况是在一个时间范围内,但现在仅包含条目的日期)的“未解决”问题。

我的数据简化为: IssueID,CreationDate,CompletionDate

我想统计未解决的问题,即Year(CompletionDate)= 1900并且它们在未解决时是累积的,即:如果昨天有1个问题在今天也有1个问题在开,那么今天的计数打开的值为2。解决后,它们应该下降(Year(CompletionDate)<> 1900)。请帮我以为我要关闭了吗?

SELECT
x.created_date,
aOpen + Open_Issue - Resolved_Issue as totopen
from(
    select 
    convert(varchar(10), cast(i.CreationDate as date), 101) as created_date, 
    sum( case when YEAR(i.CompletionDate)='1900' then 1 else 0 end) as aOpen,
    sum( case when YEAR(i.CompletionDate)<>'1900' AND (i.CompletionDate >= 
  i.CreationDate) then 1 else 0 end ) as Open_Issue,
coalesce(tot,0) as Resolved_Issue
    FROM Issues i 
LEFT JOIN (SELECT count(IssueID) as tot, CompletionDate as resolved 
    from Issues where YEAR(CompletionDate)<>'1900' group by CompletionDate, 
    count(IssueID))x
ON i.CreationDate = x.resolved);

更新 我只能每天正确返回此输出,因为它不能解决以前仍未解决的问题(Legacy_Open_Issue)并将其添加。

SELECT
created_date,
aOpen_Today + Legacy_Open_Issue - Resolved_Issue as totopen
FROM(
SELECT 
    convert(varchar(10), cast(i.CreationDate as date), 101) as created_date, 
    sum( case when YEAR(i.CompletionDate)=1900 then 1 else 0 end) as aOpen_Today,
    sum( case when (YEAR(i.CompletionDate)<>1900 AND (i.CompletionDate >= i.CreationDate)) then 1 else 0 end ) as Legacy_Open_Issue,
    coalesce(tot,0) as Resolved_Issue
FROM Issues i 
LEFT JOIN (
    SELECT count(IssueID) as tot, CompletionDate as resolved 
    FROM Issues 
    WHERE YEAR(CompletionDate)<>1900 group by CompletionDate
)x ON  x.resolved = i.CreationDate
GROUP BY convert(varchar(10), cast(i.CreationDate as date), 101), coalesce(tot,0)
) AS y;

My Data is
IssueID CreationDate    CompletionDate
1   1/15/2019   1/1/1900
2   1/16/2019   1/17/2019
3   1/16/2019   1/1/1900
4   1/20/2019   1/21/2019
5   1/28/2019   1/1/1900
6   1/30/2019   1/1/1900


My Output is
created_date    totopen
1/15/2019   1
1/16/2019   2
1/20/2019   1
1/28/2019   1
1/30/2019   1


My Output SHOULD be
created_date    totopen
1/15/2019   1
1/16/2019   3
1/20/2019   3
1/28/2019   3
1/30/2019   4

感谢您的帮助

3 个答案:

答案 0 :(得分:0)

派生表需要别名。您需要在查询末尾添加“ AS {alias}”。您还应该对代码进行格式设置和换行以提高可读性。

SELECT
   x.created_date,
   aOpen + Open_Issue - Resolved_Issue as totopen
from(
    select 
       convert(varchar(10), cast(i.CreationDate as date), 101) as created_date, 
       sum( case when YEAR(i.CompletionDate)=1900 then 1 else 0 end) as aOpen,
       sum( case when YEAR(i.CompletionDate)<>1900 AND (i.CompletionDate >= i.CreationDate) then 1 else 0 end ) as Open_Issue,
       coalesce(tot,0) as Resolved_Issue
    FROM Issues i 
    LEFT JOIN (
       SELECT count(IssueID) as tot, CompletionDate as resolved 
       from Issues
       where YEAR(CompletionDate)<>1900
       group by CompletionDate
    )x ON i.CreationDate = x.resolved
    group by convert(varchar(10), cast(i.CreationDate as date), 101)
) AS y;

此外,SO通常不会在每个帖子中回答多个问题。我已经解决了错误/别名问题,但是如果您遇到结果问题,则应该发布一个包含示例数据和预期结果的新问题。

答案 1 :(得分:0)

您需要对派生表进行别名化,例如:

SELECT
x.created_date,
aOpen + Open_Issue - Resolved_Issue as totopen
from(
    select 
    convert(varchar(10), cast(i.CreationDate as date), 101) as created_date, 
    sum( case when YEAR(i.CompletionDate)='1900' then 1 else 0 end) as aOpen,
    sum( case when YEAR(i.CompletionDate)<>'1900' AND (i.CompletionDate >= 
  i.CreationDate) then 1 else 0 end ) as Open_Issue,
coalesce(tot,0) as Resolved_Issue
    FROM Issues i 
LEFT JOIN (SELECT count(IssueID) as tot, CompletionDate as resolved 
    from Issues where YEAR(CompletionDate)<>'1900' group by CompletionDate, 
    count(IssueID))x
ON i.CreationDate = x.resolved) as DT;

答案 2 :(得分:0)

为了能够使用日期范围并确保输出中没有空白(即在未创建票证的日期上),您可能需要考虑使用“日期”事实/参考表,如下所示:

Select d.Date
    , count(i.IssueID) as TotalOpen
    , sum(case when DateDiff(DD, d.Date, cast(i.CreationDate as date)) = 0 then 1 else 0 end) as NewOpened
    , sum(case when DateDiff(DD, d.Date, cast(i.CompletionDate as date)) = 0 then 1 else 0 end) as NewClosed
From Dates d
Left join Issues i 
  on d.Date between convert(varchar(10), cast(i.CreationDate as date), 101) and
     case when YEAR(i.CompletionDate)='1900' then d.Date else i.CompletionDate end
Group by d.Date

用您要显示结果的所有日期(即每天,工作日)填充“日期”表,或使用where子句过滤日期范围/模式。